its-calendar.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. <template>
  2. <view>
  3. <view class="calendar">
  4. <scroll-view class="calendar_day_scroll" scroll-x="true" show-scrollbar="false">
  5. <view class="calendar_day">
  6. <view class="day_x" :style="{'color': (day_index == index ? '#FE3B3C' : '')}"
  7. v-for="(item, index) in dayArr" :key="index" @click.stop="dayList(item,index)">
  8. <view class="day_x_a">{{item.weeks}}</view>
  9. <view class="day_x_b">{{item.days}}</view>
  10. </view>
  11. </view>
  12. </scroll-view>
  13. <scroll-view class="calendar_time_scroll" scroll-y="true" show-scrollbar="false">
  14. <view class="calendar_time">
  15. <view class="time_x"
  16. :class="{
  17. 'time_x_sty': item?.checked,
  18. 'disabled-time': item?.disabled
  19. }"
  20. v-for="(item, index) in timeHostArr[day_index]"
  21. :key="index"
  22. @click="handleTimeClick(item)">
  23. <text>{{item?.hours}}</text>
  24. <text v-if="item.hasReservation === 1" class="hasRe-text">已预约</text>
  25. </view>
  26. </view>
  27. </scroll-view>
  28. </view>
  29. <view class="calendar_footer">
  30. <view class="remark_content">{{remark}}</view>
  31. </view>
  32. </view>
  33. </template>
  34. <script>
  35. export default {
  36. props: {
  37. timeArr: {
  38. type: Array,
  39. default: () => []
  40. },
  41. timeHostArr: {
  42. type: Array,
  43. default: () => []
  44. },
  45. businessDuration: {
  46. type: Number,
  47. default: 0
  48. } // 从父组件接收的服务时长(分钟)
  49. },
  50. data() {
  51. return {
  52. dayArr: [],
  53. day_index: 0,
  54. host_index: '',
  55. nowTimes: new Date().getTime(), // 只保留一个定义
  56. disableAfterTime: null,
  57. selectedTime: null,
  58. selectedDay: null, // 新增:记录选择的日期
  59. selectedTimeSlots: {}, // 存储每个日期的选择状态
  60. }
  61. },
  62. watch: {
  63. timeArr: {
  64. handler(newVal) {
  65. console.log(newVal, '>>>>>>>时间范围');
  66. let dateArr = newVal.map(item => {
  67. let day = new Date(item)
  68. const daysOfWeek = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
  69. return {
  70. weeks: daysOfWeek[day.getDay()],
  71. days: item.slice(5)
  72. }
  73. })
  74. this.dayArr = dateArr
  75. },
  76. immediate: true,
  77. deep: true
  78. },
  79. timeHostArr: {
  80. handler(newVal) {
  81. console.log(newVal, '>>>>>>>时间更新');
  82. // 当timeHostArr更新时,恢复之前的选择状态
  83. if (newVal && newVal.length > 0) {
  84. this.restoreSelections();
  85. }
  86. },
  87. deep: true
  88. }
  89. },
  90. mounted() {
  91. },
  92. methods: {
  93. // 恢复选择状态
  94. restoreSelections() {
  95. if (!this.timeHostArr[this.day_index]) return;
  96. // 遍历当前日期的时间槽
  97. this.timeHostArr[this.day_index].forEach(slot => {
  98. // 检查这个时间槽是否在之前被选中
  99. const dateKey = this.timeArr[this.day_index];
  100. if (this.selectedTimeSlots[dateKey] &&
  101. this.selectedTimeSlots[dateKey].includes(slot.timeStamp)) {
  102. slot.checked = true;
  103. // 计算并禁用后续的时间段
  104. const seconds = this.businessDuration * 60;
  105. const endTimestamp = slot.timeStamp + seconds;
  106. // 禁用后续的时间段
  107. this.timeHostArr[this.day_index].forEach(s => {
  108. if (s.timeStamp > slot.timeStamp &&
  109. s.timeStamp <= endTimestamp) {
  110. s.disabled = true;
  111. }
  112. });
  113. }
  114. });
  115. },
  116. handleTimeClick(item) {
  117. const durationMs = this.businessDuration * 60 * 1000;
  118. const itemTime = item.timeStamp > 9999999999 ? item.timeStamp : item.timeStamp * 1000;
  119. const slots = this.timeHostArr[this.day_index] || [];
  120. const currentDate = this.timeArr[this.day_index];
  121. // 1. 判断当前时间是否在任何已预约时间段的服务时长范围内
  122. let inReservedRange = false;
  123. for (let slot of slots) {
  124. if (slot.hasReservation === 1) {
  125. const reservedTime = slot.timeStamp > 9999999999 ? slot.timeStamp : slot.timeStamp * 1000;
  126. const start = reservedTime - durationMs;
  127. const end = reservedTime + durationMs;
  128. if (itemTime >= start && itemTime < end) {
  129. inReservedRange = true;
  130. break;
  131. }
  132. }
  133. }
  134. if (inReservedRange) {
  135. uni.showToast({ title: '服务时长不足,请选择其他时间段', icon: 'none' });
  136. return false;
  137. }
  138. // 2. 判断当前点击时间与所有已选时间段的服务时长有无重叠(多选判断)
  139. const selectedArr = this.selectedTimeSlots[currentDate] || [];
  140. if (!item.checked) { // 只在选中时判断
  141. for (let t of selectedArr) {
  142. if (t === item.timeStamp) continue; // 跳过自己
  143. const tStart = t > 9999999999 ? t : t * 1000;
  144. const tEnd = tStart + durationMs;
  145. if (
  146. (itemTime >= tStart && itemTime < tEnd) ||
  147. (itemTime < tStart && itemTime + durationMs > tStart)
  148. ) {
  149. uni.showToast({ title: '时间段不在服务范围内', icon: 'none' });
  150. return false;
  151. }
  152. }
  153. }
  154. // 3. 其他逻辑保持不变
  155. const seconds = this.businessDuration * 60;
  156. const endTimestamp = (item.timeStamp + seconds);
  157. const filteredSlots = this.timeHostArr[this.day_index].filter(i => {
  158. return (i.timeStamp > item.timeStamp &&
  159. i.timeStamp <= endTimestamp)
  160. });
  161. if (!item.checked) {
  162. filteredSlots.forEach(v => {
  163. v.disabled = true
  164. });
  165. item.checked = true;
  166. if (!this.selectedTimeSlots[currentDate]) {
  167. this.selectedTimeSlots[currentDate] = [];
  168. }
  169. this.selectedTimeSlots[currentDate].push(item.timeStamp);
  170. } else {
  171. filteredSlots.forEach(v => {
  172. if (v.hasReservation !== 1) v.disabled = false
  173. });
  174. item.checked = false;
  175. if (this.selectedTimeSlots[currentDate]) {
  176. const index = this.selectedTimeSlots[currentDate].indexOf(item.timeStamp);
  177. if (index > -1) {
  178. this.selectedTimeSlots[currentDate].splice(index, 1);
  179. }
  180. }
  181. }
  182. this.hosts(item);
  183. },
  184. // 点击日期
  185. dayList(e, index) {
  186. this.day_index = index
  187. this.$emit('getByDate', this.timeArr[index])
  188. },
  189. // 转换时间戳为毫秒
  190. ensureMillisecond(timestamp) {
  191. return timestamp > 9999999999 ? timestamp : timestamp * 1000;
  192. },
  193. shouldDisable(item) {
  194. if (!item) return true;
  195. // 已预约
  196. if (item.hasReservation === 1) return true;
  197. const itemTime = this.ensureMillisecond(item.timeStamp);
  198. // 过去时间
  199. if (this.nowTimes > itemTime) return true;
  200. return false;
  201. },
  202. hosts(item) {
  203. const itemTime = this.ensureMillisecond(item.timeStamp);
  204. this.host_index = item.timeStamp; // 显示用原始值
  205. this.selectedTime = itemTime;
  206. this.disableAfterTime = itemTime + (this.businessDuration * 60 * 1000);
  207. this.$emit('getByTime', {
  208. ...item,
  209. timeStamp: itemTime // 传递转换后的值
  210. });
  211. },
  212. // 点击立即预约
  213. sub() {
  214. if (this.host_index == '') {
  215. this.$tool.toast('请选择时间');
  216. } else {
  217. let day = this.dayArr[this.day_index];
  218. let time = this.times(this.host_index);
  219. let comTime = {
  220. days: day.days,
  221. weeks: day.weeks,
  222. hours: this.host_All.hours,
  223. timeStamp: this.host_All.timeStamp,
  224. time: time
  225. };
  226. this.$emit('getTime', comTime);
  227. }
  228. },
  229. // 格式化时间
  230. times(data) {
  231. let date = new Date(data * 1000);
  232. let h = date.getHours();
  233. h = h < 10 ? ('0' + h) : h; // 小时补0
  234. let m = date.getMinutes();
  235. m = m < 10 ? ('0' + m) : m; // 分钟补0
  236. return h + ':' + m;
  237. },
  238. time(data, type) {
  239. let date = new Date(data * 1000);
  240. let y = date.getFullYear();
  241. let MM = date.getMonth() + 1;
  242. MM = MM < 10 ? ('0' + MM) : MM; // 月补0
  243. let d = date.getDate();
  244. d = d < 10 ? ('0' + d) : d; // 天补0
  245. let h = date.getHours();
  246. h = h < 10 ? ('0' + h) : h; // 小时补0
  247. let m = date.getMinutes();
  248. m = m < 10 ? ('0' + m) : m; // 分钟补0
  249. let s = date.getSeconds();
  250. s = s < 10 ? ('0' + s) : s; // 秒补0
  251. if (type == 'yymmdd') {
  252. return y + '-' + MM + '-' + d;
  253. } else if (type == 'hhmmss') {
  254. return h + ':' + m + ':' + s;
  255. } else {
  256. return y + '-' + MM + '-' + d + ' ' + h + ':' + m + ':' + s;
  257. }
  258. }
  259. }
  260. }
  261. </script>
  262. <style lang="scss">
  263. page {
  264. background-color: #F4F4F4;
  265. }
  266. .calendar {
  267. width: 710rpx;
  268. height: 460rpx;
  269. background-color: #FFFFFF;
  270. margin: 20rpx auto 10rpx;
  271. border-radius: 8rpx;
  272. }
  273. .calendar_day_scroll {
  274. width: 100%;
  275. overflow-x: auto;
  276. overflow-y: hidden;
  277. white-space: nowrap;
  278. &::-webkit-scrollbar {
  279. display: none;
  280. }
  281. }
  282. .calendar_day {
  283. display: flex;
  284. flex-direction: row;
  285. width: max-content;
  286. height: 120rpx;
  287. }
  288. .day_x {
  289. display: flex;
  290. flex-direction: column;
  291. justify-content: center;
  292. align-items: center;
  293. width: 100rpx;
  294. height: 100%;
  295. font-size: 30rpx;
  296. color: #333333;
  297. flex-shrink: 0;
  298. margin-right: 10rpx;
  299. background: #f8f8f8;
  300. border-radius: 12rpx;
  301. cursor: pointer;
  302. }
  303. .day_x_a {
  304. font-weight: bold;
  305. margin-bottom: 8rpx;
  306. }
  307. .day_x_b {
  308. font-size: 28rpx;
  309. color: #666;
  310. }
  311. .calendar_time_scroll {
  312. width: 100%;
  313. height: 380rpx;
  314. overflow-y: auto;
  315. background: #fff;
  316. border-radius: 8rpx;
  317. margin-bottom: 20rpx;
  318. }
  319. .calendar_time {
  320. display: flex;
  321. width: 100%;
  322. flex-flow: row wrap;
  323. align-content: flex-start;
  324. margin: 20rpx 0;
  325. overflow-y: auto;
  326. }
  327. .time_x {
  328. display: flex;
  329. flex-flow: row;
  330. justify-content: center;
  331. align-items: center;
  332. width: 20%;
  333. height: 54rpx;
  334. border-radius: 26rpx;
  335. margin: 10rpx 0;
  336. font-size: 30rpx;
  337. color: #333333;
  338. display: flex;
  339. flex-direction: column;
  340. .hasRe-text {
  341. font-size: 20rpx;
  342. color: #999999;
  343. }
  344. }
  345. .time_x_sty {
  346. background-color: #FFE97B;
  347. color: #000000 !important;
  348. }
  349. .sub {
  350. display: flex;
  351. justify-content: center;
  352. align-items: center;
  353. width: 710rpx;
  354. height: 100rpx;
  355. border-radius: 50rpx;
  356. margin: 30rpx auto;
  357. color: #FFFFFF;
  358. font-size: 36rpx;
  359. background-color: #FE3B3C;
  360. }
  361. .time_x {
  362. /* 正常状态样式 */
  363. &.disabled-time {
  364. background-color: #f2f2f2;
  365. color: #999999;
  366. pointer-events: none;
  367. }
  368. &.time_x_sty {
  369. background-color: #FFE97B;
  370. color: #000000;
  371. }
  372. }
  373. .calendar_footer {
  374. width: 100%;
  375. background: #fff;
  376. border-radius: 12rpx;
  377. margin: 0 auto 20rpx auto;
  378. padding: 24rpx 32rpx;
  379. box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.04);
  380. display: flex;
  381. align-items: flex-start;
  382. min-height: 60rpx;
  383. }
  384. .remark_content {
  385. color: #333;
  386. word-break: break-all;
  387. flex: 1;
  388. }
  389. </style>