its-calendar.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. <template>
  2. <view>
  3. <view class="calendar">
  4. <scroll-view
  5. class="calendar_day_scroll"
  6. scroll-x="true"
  7. show-scrollbar="false"
  8. >
  9. <view class="calendar_day">
  10. <view
  11. class="day_x"
  12. :style="{ color: day_index == index ? '#FE3B3C' : '' }"
  13. v-for="(item, index) in dayArr"
  14. :key="index"
  15. @click.stop="dayList(item, index)"
  16. >
  17. <view class="day_x_a">{{ item.weeks }}</view>
  18. <view class="day_x_b">{{ item.days }}</view>
  19. </view>
  20. </view>
  21. </scroll-view>
  22. <scroll-view
  23. class="calendar_time_scroll"
  24. scroll-y="true"
  25. show-scrollbar="false"
  26. >
  27. <view class="calendar_time">
  28. <view
  29. class="time_x"
  30. :class="{
  31. time_x_sty: item?.checked,
  32. 'disabled-time': item?.disabled || item?.hasReservation === 1,
  33. }"
  34. v-for="(item, index) in timeHostArr[day_index]"
  35. :key="index"
  36. @click="handleTimeClick(item)"
  37. >
  38. <text>{{ item?.hours }}</text>
  39. <text v-if="item.hasReservation === 1" class="hasRe-text"
  40. >已预约</text
  41. >
  42. </view>
  43. </view>
  44. </scroll-view>
  45. </view>
  46. <view class="calendar_footer">
  47. <view class="selected-time-info">
  48. {{ selectRange.join('\n') }}
  49. </view>
  50. </view>
  51. </view>
  52. </template>
  53. <script>
  54. export default {
  55. props: {
  56. timeArr: {
  57. type: Array,
  58. default: () => [],
  59. },
  60. timeHostArr: {
  61. type: Array,
  62. default: () => [],
  63. },
  64. businessDuration: {
  65. // 从父组件接收的服务时长(分钟)
  66. type: Number,
  67. default: 0,
  68. },
  69. minQuantity: {
  70. // 从父组件接收的购买次数(次数)
  71. type: Number,
  72. default: 1,
  73. },
  74. businessTierName: {
  75. type: String,
  76. default: '',
  77. },
  78. },
  79. data() {
  80. return {
  81. dayArr: [],
  82. day_index: 0,
  83. host_index: '',
  84. nowTimes: new Date().getTime(), // 只保留一个定义
  85. disableAfterTime: null,
  86. selectedTime: null,
  87. selectedDay: null, // 新增:记录选择的日期
  88. selectedTimeSlots: {}, // 存储每个日期的选择状态
  89. upItem: null, // 保留上一个点击的item,外部情况下再刷新状态
  90. filteredSlots: [],
  91. }
  92. },
  93. watch: {
  94. timeArr: {
  95. handler(newVal) {
  96. // console.log(newVal, '>>>>>>>时间范围');
  97. let dateArr = newVal.map((item) => {
  98. let day = new Date(item)
  99. const daysOfWeek = [
  100. '周日',
  101. '周一',
  102. '周二',
  103. '周三',
  104. '周四',
  105. '周五',
  106. '周六',
  107. ]
  108. return {
  109. weeks: daysOfWeek[day.getDay()],
  110. days: item.slice(5),
  111. }
  112. })
  113. this.dayArr = dateArr
  114. },
  115. immediate: true,
  116. deep: true,
  117. },
  118. timeHostArr: {
  119. handler(newVal) {
  120. // console.log(newVal, '>>>>>>>时间更新');
  121. // 当timeHostArr更新时,恢复之前的选择状态
  122. if (newVal && newVal.length > 0) {
  123. this.restoreSelections()
  124. }
  125. },
  126. deep: true,
  127. },
  128. },
  129. mounted() {
  130. console.log(this.businessTierName, "businessTierName from parent component")
  131. },
  132. computed: {
  133. // 计算选中区域
  134. selectRange() {
  135. const reservations = this.timeHostArr[this.day_index]
  136. // console.log(reservations, '>>>>>reservations')
  137. // console.log(JSON.stringify(current), '>>>>>current');
  138. // return current
  139. const result = []
  140. let currentGroup = null
  141. for (const item of reservations) {
  142. if (item.checked) {
  143. // 遇到新的checked项,创建新组
  144. if (currentGroup) {
  145. result.push(currentGroup)
  146. }
  147. currentGroup = [item]
  148. } else if (currentGroup && item.disabled && item.hasReservation === 0) {
  149. // 如果当前有活跃的组,且符合disabled和hasReservation条件,加入当前组
  150. currentGroup.push(item)
  151. } else if (currentGroup) {
  152. // 不符合连续条件,结束当前组
  153. result.push(currentGroup)
  154. currentGroup = null
  155. }
  156. }
  157. // 添加最后一组(如果有)
  158. if (currentGroup) {
  159. result.push(currentGroup)
  160. }
  161. const timeRangeArr = result.map((arr) => {
  162. // 获取选中的日期
  163. const selectedDate = this.timeArr[this.day_index]
  164. const timeRange = arr[0].hours + '-' + arr[arr.length - 1].hours
  165. return `${this.businessTierName}\n预约上门时间:${selectedDate} ${timeRange}`
  166. })
  167. return timeRangeArr
  168. },
  169. },
  170. methods: {
  171. // 恢复选择状态
  172. restoreSelections() {
  173. // console.log('>>>>>>执行了', this.selectedTimeSlots);
  174. if (!this.timeHostArr[this.day_index]) return
  175. // console.log('>>>>>>执行了2', this.selectedTimeSlots);
  176. // 遍历当前日期的时间槽
  177. this.timeHostArr[this.day_index].forEach((slot) => {
  178. // 检查这个时间槽是否在之前被选中
  179. const dateKey = this.timeArr[this.day_index]
  180. // console.log(dateKey, '>>>>dateKey');
  181. // console.log(this.selectedTimeSlots[dateKey], '>>>>this.selectedTimeSlots[dateKey]');
  182. if (
  183. this.selectedTimeSlots[dateKey] &&
  184. this.selectedTimeSlots[dateKey].includes(slot.timeStamp)
  185. ) {
  186. slot.checked = true
  187. // 计算并禁用后续的时间段
  188. const seconds = this.businessDuration * this.minQuantity * 60
  189. // const startTimestamp =
  190. const endTimestamp = slot.timeStamp + seconds
  191. // 禁用后续的时间段
  192. this.timeHostArr[this.day_index].forEach((s) => {
  193. // 差一个范围控制变量,是否在可选择范围内
  194. if (s.timeStamp > slot.timeStamp && s.timeStamp <= endTimestamp) {
  195. s.disabled = true
  196. }
  197. })
  198. }
  199. })
  200. },
  201. handleTimeClick(item) {
  202. this.upItem = item
  203. const durationMs = this.businessDuration * this.minQuantity * 60 * 1000
  204. const itemTime =
  205. item.timeStamp > 9999999999 ? item.timeStamp : item.timeStamp * 1000
  206. const slots = this.timeHostArr[this.day_index] || []
  207. const currentDate = this.timeArr[this.day_index]
  208. // 1. 判断当前时间是否在任何已预约时间段的服务时长范围内
  209. let inReservedRange = false
  210. for (let slot of slots) {
  211. if (slot.hasReservation === 1) {
  212. const reservedTime =
  213. slot.timeStamp > 9999999999 ? slot.timeStamp : slot.timeStamp * 1000
  214. const start = reservedTime - durationMs
  215. const end = reservedTime + durationMs
  216. if (itemTime >= start && itemTime < end) {
  217. inReservedRange = true
  218. break
  219. }
  220. }
  221. }
  222. if (inReservedRange) {
  223. uni.showToast({ title: '服务时长不足,请选择其他时间段', icon: 'none' })
  224. return false
  225. }
  226. // 2. 判断当前点击时间与所有已选时间段的服务时长有无重叠(多选判断)
  227. const selectedArr = this.selectedTimeSlots[currentDate] || []
  228. if (!item.checked) {
  229. // 只在选中时判断
  230. for (let t of selectedArr) {
  231. if (t === item.timeStamp) continue // 跳过自己
  232. const tStart = t > 9999999999 ? t : t * 1000
  233. const tEnd = tStart + durationMs
  234. if (
  235. (itemTime >= tStart && itemTime < tEnd) ||
  236. (itemTime < tStart && itemTime + durationMs > tStart)
  237. ) {
  238. uni.showToast({ title: '时间段不在服务范围内', icon: 'none' })
  239. return false
  240. }
  241. }
  242. }
  243. // 3. 检查当前时间到结束是否有足够的时间段
  244. const seconds = this.businessDuration * this.minQuantity * 60
  245. const endTimestamp = item.timeStamp + seconds
  246. // 找出当前时间到服务结束时间之间的所有时间段
  247. const filteredSlots = this.timeHostArr[this.day_index].filter((i) => {
  248. return i.timeStamp > item.timeStamp && i.timeStamp <= endTimestamp
  249. })
  250. this.filteredSlots = filteredSlots
  251. // 检查这些时间段中是否有已预约的时间
  252. const hasReservedSlot = filteredSlots.some(
  253. (slot) => slot.hasReservation === 1
  254. )
  255. if (hasReservedSlot) {
  256. uni.showToast({
  257. title: '所选时间段内有已预约时间,请选择其他时间',
  258. icon: 'none',
  259. })
  260. return false
  261. }
  262. // 时间差值(数组中的最后一项 - 当前点击项)
  263. const timestampDifferenceValue = filteredSlots.length
  264. ? (filteredSlots[filteredSlots.length - 1].timeStamp - item.timeStamp) *
  265. 1000
  266. : 0
  267. // 选择时间,后续服务时间是否充足,不充足结束逻辑执行 timeStamp
  268. if (timestampDifferenceValue < durationMs) {
  269. // 所选时间差值 小于 服务时间值 结束执行
  270. uni.showToast({ title: '所选时间的服务时间不充足!', icon: 'none' })
  271. return false
  272. }
  273. if (!item.checked) {
  274. filteredSlots.forEach((v) => {
  275. v.disabled = true
  276. })
  277. item.checked = true
  278. if (!this.selectedTimeSlots[currentDate]) {
  279. this.selectedTimeSlots[currentDate] = []
  280. }
  281. this.selectedTimeSlots[currentDate].push(item.timeStamp)
  282. } else {
  283. filteredSlots.forEach((v) => {
  284. if (v.hasReservation !== 1) v.disabled = false
  285. })
  286. item.checked = false
  287. if (this.selectedTimeSlots[currentDate]) {
  288. const index = this.selectedTimeSlots[currentDate].indexOf(
  289. item.timeStamp
  290. )
  291. if (index > -1) {
  292. this.selectedTimeSlots[currentDate].splice(index, 1)
  293. }
  294. }
  295. }
  296. this.hosts(item)
  297. },
  298. async handleTimeClick2() {
  299. if (!this.upItem) return
  300. let item = this.upItem
  301. const currentDate = this.timeArr[this.day_index]
  302. const durationMs = this.businessDuration * this.minQuantity * 60 * 1000
  303. const itemTime =
  304. item.timeStamp > 9999999999 ? item.timeStamp : item.timeStamp * 1000
  305. const slots = this.timeHostArr[this.day_index] || []
  306. // 保留之前的选择状态,只重置disabled状态
  307. this.timeHostArr[this.day_index].forEach((s) => {
  308. if (s.hasReservation !== 1 && !s.checked) {
  309. s.disabled = false
  310. }
  311. })
  312. // 1. 检查是否与已预约时间冲突
  313. let inReservedRange = false
  314. for (let slot of slots) {
  315. if (slot.hasReservation === 1) {
  316. const reservedTime =
  317. slot.timeStamp > 9999999999 ? slot.timeStamp : slot.timeStamp * 1000
  318. const start = reservedTime - durationMs
  319. const end = reservedTime + durationMs
  320. if (itemTime >= start && itemTime < end) {
  321. inReservedRange = true
  322. break
  323. }
  324. }
  325. }
  326. if (inReservedRange) {
  327. uni.showToast({ title: '服务时长不足,请选择其他时间段', icon: 'none' })
  328. return false
  329. }
  330. // 2. 检查是否有足够的后续时间段
  331. const seconds = this.businessDuration * this.minQuantity * 60
  332. const endTimestamp = item.timeStamp + seconds
  333. // 找出从当前时间到结束时间之间的所有时间段
  334. const filteredSlots = this.timeHostArr[this.day_index].filter((i) => {
  335. return i.timeStamp > item.timeStamp && i.timeStamp <= endTimestamp
  336. })
  337. this.filteredSlots = filteredSlots
  338. // 检查这些时间段中是否有已预约的时间
  339. const hasReservedSlot = filteredSlots.some(
  340. (slot) => slot.hasReservation === 1
  341. )
  342. if (hasReservedSlot) {
  343. uni.showToast({
  344. title: '所选时间段内有已预约时间,请选择其他时间',
  345. icon: 'none',
  346. })
  347. return false
  348. }
  349. // 检查时间是否足够
  350. const timestampDifferenceValue = filteredSlots.length
  351. ? (filteredSlots[filteredSlots.length - 1].timeStamp - item.timeStamp) *
  352. 1000
  353. : 0
  354. if (timestampDifferenceValue < durationMs) {
  355. uni.showToast({ title: '所选时间的服务时间不充足!', icon: 'none' })
  356. return false
  357. }
  358. // 所有检查通过,更新disabled状态
  359. // filteredSlots.forEach(v => {
  360. // if (v.hasReservation !== 1 && !v.checked) {
  361. // v.disabled = true;
  362. // }
  363. // });
  364. this.hosts(item)
  365. },
  366. // 点击日期
  367. dayList(e, index) {
  368. this.day_index = index
  369. this.$emit('getByDate', this.timeArr[index])
  370. },
  371. // 转换时间戳为毫秒
  372. ensureMillisecond(timestamp) {
  373. return timestamp > 9999999999 ? timestamp : timestamp * 1000
  374. },
  375. shouldDisable(item) {
  376. if (!item) return true
  377. // 已预约
  378. if (item.hasReservation === 1) return true
  379. const itemTime = this.ensureMillisecond(item.timeStamp)
  380. // 过去时间
  381. if (this.nowTimes > itemTime) return true
  382. return false
  383. },
  384. hosts(item) {
  385. const itemTime = this.ensureMillisecond(item.timeStamp)
  386. this.host_index = item.timeStamp // 显示用原始值
  387. this.selectedTime = itemTime
  388. this.disableAfterTime =
  389. itemTime + this.businessDuration * this.minQuantity * 60 * 1000
  390. this.$emit('getByTime', {
  391. ...item,
  392. timeStamp: itemTime, // 传递转换后的值
  393. })
  394. },
  395. // 点击立即预约
  396. sub() {
  397. if (this.host_index == '') {
  398. this.$tool.toast('请选择时间')
  399. } else {
  400. let day = this.dayArr[this.day_index]
  401. let time = this.times(this.host_index)
  402. let comTime = {
  403. days: day.days,
  404. weeks: day.weeks,
  405. hours: this.host_All.hours,
  406. timeStamp: this.host_All.timeStamp,
  407. time: time,
  408. }
  409. this.$emit('getTime', comTime)
  410. }
  411. },
  412. // 格式化时间
  413. times(data) {
  414. let date = new Date(data * 1000)
  415. let h = date.getHours()
  416. h = h < 10 ? '0' + h : h // 小时补0
  417. let m = date.getMinutes()
  418. m = m < 10 ? '0' + m : m // 分钟补0
  419. return h + ':' + m
  420. },
  421. time(data, type) {
  422. let date = new Date(data * 1000)
  423. let y = date.getFullYear()
  424. let MM = date.getMonth() + 1
  425. MM = MM < 10 ? '0' + MM : MM // 月补0
  426. let d = date.getDate()
  427. d = d < 10 ? '0' + d : d // 天补0
  428. let h = date.getHours()
  429. h = h < 10 ? '0' + h : h // 小时补0
  430. let m = date.getMinutes()
  431. m = m < 10 ? '0' + m : m // 分钟补0
  432. let s = date.getSeconds()
  433. s = s < 10 ? '0' + s : s // 秒补0
  434. if (type == 'yymmdd') {
  435. return y + '-' + MM + '-' + d
  436. } else if (type == 'hhmmss') {
  437. return h + ':' + m + ':' + s
  438. } else {
  439. return y + '-' + MM + '-' + d + ' ' + h + ':' + m + ':' + s
  440. }
  441. },
  442. },
  443. }
  444. </script>
  445. <style lang="scss">
  446. page {
  447. background-color: #f4f4f4;
  448. }
  449. .calendar {
  450. width: 710rpx;
  451. height: 460rpx;
  452. background-color: #ffffff;
  453. margin: 20rpx auto 10rpx;
  454. border-radius: 8rpx;
  455. }
  456. .calendar_day_scroll {
  457. width: 100%;
  458. overflow-x: auto;
  459. overflow-y: hidden;
  460. white-space: nowrap;
  461. &::-webkit-scrollbar {
  462. display: none;
  463. }
  464. }
  465. .calendar_day {
  466. display: flex;
  467. flex-direction: row;
  468. width: max-content;
  469. height: 120rpx;
  470. }
  471. .day_x {
  472. display: flex;
  473. flex-direction: column;
  474. justify-content: center;
  475. align-items: center;
  476. width: 100rpx;
  477. height: 100%;
  478. font-size: 30rpx;
  479. color: #333333;
  480. flex-shrink: 0;
  481. margin-right: 10rpx;
  482. background: #f8f8f8;
  483. border-radius: 12rpx;
  484. cursor: pointer;
  485. }
  486. .day_x_a {
  487. font-weight: bold;
  488. margin-bottom: 8rpx;
  489. }
  490. .day_x_b {
  491. font-size: 28rpx;
  492. color: #666;
  493. }
  494. .calendar_time_scroll {
  495. width: 100%;
  496. height: 380rpx;
  497. overflow-y: auto;
  498. background: #fff;
  499. border-radius: 8rpx;
  500. margin-bottom: 20rpx;
  501. }
  502. .calendar_time {
  503. display: flex;
  504. width: 100%;
  505. flex-flow: row wrap;
  506. align-content: flex-start;
  507. margin: 20rpx 0;
  508. overflow-y: auto;
  509. }
  510. .time_x {
  511. display: flex;
  512. flex-flow: row;
  513. justify-content: center;
  514. align-items: center;
  515. width: 20%;
  516. height: 54rpx;
  517. border-radius: 26rpx;
  518. margin: 10rpx 0;
  519. font-size: 30rpx;
  520. color: #333333;
  521. display: flex;
  522. flex-direction: column;
  523. .hasRe-text {
  524. font-size: 20rpx;
  525. color: #999999;
  526. }
  527. }
  528. .time_x_sty {
  529. background-color: #ffe97b;
  530. color: #000000 !important;
  531. }
  532. .sub {
  533. display: flex;
  534. justify-content: center;
  535. align-items: center;
  536. width: 710rpx;
  537. height: 100rpx;
  538. border-radius: 50rpx;
  539. margin: 30rpx auto;
  540. color: #ffffff;
  541. font-size: 36rpx;
  542. background-color: #fe3b3c;
  543. }
  544. .time_x {
  545. /* 正常状态样式 */
  546. &.disabled-time {
  547. background-color: #f2f2f2;
  548. color: #999999;
  549. pointer-events: none;
  550. }
  551. &.time_x_sty {
  552. background-color: #ffe97b;
  553. color: #000000;
  554. }
  555. }
  556. .calendar_footer {
  557. width: 100%;
  558. background: #fff;
  559. border-radius: 12rpx;
  560. margin: 0 auto 20rpx auto;
  561. padding: 24rpx 32rpx;
  562. box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.1);
  563. display: flex;
  564. align-items: center;
  565. min-height: 60rpx;
  566. white-space: pre-line;
  567. font-size: 26rpx;
  568. color: #333;
  569. line-height: 1.8;
  570. border-left: 6rpx solid #FE3B3C;
  571. }
  572. .selected-time-info {
  573. color: #333;
  574. word-break: break-all;
  575. flex: 1;
  576. font-weight: 500;
  577. padding-left: 15rpx;
  578. line-height: 48rpx;
  579. }
  580. .selected-time-info .business-name {
  581. color: #FE3B3C;
  582. font-weight: bold;
  583. }
  584. </style>