its-calendar.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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. minQuantity: { // 从父组件接收的购买次数(次数)
  50. type: Number,
  51. default: 1
  52. }
  53. },
  54. data() {
  55. return {
  56. dayArr: [],
  57. day_index: 0,
  58. host_index: '',
  59. nowTimes: new Date().getTime(), // 只保留一个定义
  60. disableAfterTime: null,
  61. selectedTime: null,
  62. selectedDay: null, // 新增:记录选择的日期
  63. selectedTimeSlots: {}, // 存储每个日期的选择状态
  64. upItem: null, // 保留上一个点击的item,外部情况下再刷新状态
  65. filteredSlots: []
  66. }
  67. },
  68. watch: {
  69. timeArr: {
  70. handler(newVal) {
  71. console.log(newVal, '>>>>>>>时间范围');
  72. let dateArr = newVal.map(item => {
  73. let day = new Date(item)
  74. const daysOfWeek = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
  75. return {
  76. weeks: daysOfWeek[day.getDay()],
  77. days: item.slice(5)
  78. }
  79. })
  80. this.dayArr = dateArr
  81. },
  82. immediate: true,
  83. deep: true
  84. },
  85. timeHostArr: {
  86. handler(newVal) {
  87. console.log(newVal, '>>>>>>>时间更新');
  88. // 当timeHostArr更新时,恢复之前的选择状态
  89. if (newVal && newVal.length > 0) {
  90. this.restoreSelections();
  91. }
  92. },
  93. deep: true
  94. }
  95. },
  96. mounted() {
  97. },
  98. methods: {
  99. // 恢复选择状态
  100. restoreSelections() {
  101. console.log('>>>>>>执行了', this.selectedTimeSlots);
  102. if (!this.timeHostArr[this.day_index]) return;
  103. console.log('>>>>>>执行了2', this.selectedTimeSlots);
  104. // 遍历当前日期的时间槽
  105. this.timeHostArr[this.day_index].forEach(slot => {
  106. // 检查这个时间槽是否在之前被选中
  107. const dateKey = this.timeArr[this.day_index];
  108. console.log(dateKey, '>>>>dateKey');
  109. console.log(this.selectedTimeSlots[dateKey], '>>>>this.selectedTimeSlots[dateKey]');
  110. if (this.selectedTimeSlots[dateKey] &&
  111. this.selectedTimeSlots[dateKey].includes(slot.timeStamp)) {
  112. slot.checked = true;
  113. // 计算并禁用后续的时间段
  114. const seconds = this.businessDuration * this.minQuantity * 60;
  115. // const startTimestamp =
  116. const endTimestamp = slot.timeStamp + seconds;
  117. // 禁用后续的时间段
  118. this.timeHostArr[this.day_index].forEach(s => {
  119. // 差一个范围控制变量,是否在可选择范围内
  120. if (s.timeStamp > slot.timeStamp && s.timeStamp <= endTimestamp) {
  121. s.disabled = true
  122. }
  123. });
  124. }
  125. });
  126. },
  127. handleTimeClick(item) {
  128. this.upItem = item
  129. const durationMs = this.businessDuration * this.minQuantity * 60 * 1000;
  130. const itemTime = item.timeStamp > 9999999999 ? item.timeStamp : item.timeStamp * 1000;
  131. const slots = this.timeHostArr[this.day_index] || [];
  132. const currentDate = this.timeArr[this.day_index];
  133. // 1. 判断当前时间是否在任何已预约时间段的服务时长范围内
  134. let inReservedRange = false;
  135. for (let slot of slots) {
  136. if (slot.hasReservation === 1) {
  137. const reservedTime = slot.timeStamp > 9999999999 ? slot.timeStamp : slot.timeStamp * 1000;
  138. const start = reservedTime - durationMs;
  139. const end = reservedTime + durationMs;
  140. if (itemTime >= start && itemTime < end) {
  141. inReservedRange = true;
  142. break;
  143. }
  144. }
  145. }
  146. if (inReservedRange) {
  147. uni.showToast({ title: '服务时长不足,请选择其他时间段', icon: 'none' });
  148. return false;
  149. }
  150. // 2. 判断当前点击时间与所有已选时间段的服务时长有无重叠(多选判断)
  151. const selectedArr = this.selectedTimeSlots[currentDate] || [];
  152. if (!item.checked) { // 只在选中时判断
  153. for (let t of selectedArr) {
  154. if (t === item.timeStamp) continue; // 跳过自己
  155. const tStart = t > 9999999999 ? t : t * 1000;
  156. const tEnd = tStart + durationMs;
  157. if (
  158. (itemTime >= tStart && itemTime < tEnd) ||
  159. (itemTime < tStart && itemTime + durationMs > tStart)
  160. ) {
  161. uni.showToast({ title: '时间段不在服务范围内', icon: 'none' });
  162. return false;
  163. }
  164. }
  165. }
  166. // 3. 其他逻辑保持不变
  167. const seconds = this.businessDuration * this.minQuantity * 60;
  168. const endTimestamp = (item.timeStamp + seconds);
  169. const filteredSlots = this.timeHostArr[this.day_index].filter(i => {
  170. return (i.timeStamp > item.timeStamp &&
  171. i.timeStamp <= endTimestamp)
  172. });
  173. this.filteredSlots = filteredSlots
  174. console.log(filteredSlots, '》》》》》》filteredSlots');
  175. // 时间差值(数组中的最后一项 - 当前点击项)
  176. const timestampDifferenceValue = filteredSlots.length ? (filteredSlots[filteredSlots.length - 1].timeStamp - item.timeStamp) * 1000 : 0
  177. // 选择时间,后续服务时间是否充足,不充足结束逻辑执行 timeStamp
  178. if (timestampDifferenceValue < durationMs) { // 所选时间差值 小于 服务时间值 结束执行
  179. uni.showToast({ title: '所选时间的服务时间不充足!', icon: 'none' });
  180. return false
  181. }
  182. if (!item.checked) {
  183. filteredSlots.forEach(v => {
  184. v.disabled = true
  185. });
  186. item.checked = true;
  187. if (!this.selectedTimeSlots[currentDate]) {
  188. this.selectedTimeSlots[currentDate] = [];
  189. }
  190. this.selectedTimeSlots[currentDate].push(item.timeStamp);
  191. } else {
  192. filteredSlots.forEach(v => {
  193. if (v.hasReservation !== 1) v.disabled = false
  194. });
  195. item.checked = false;
  196. if (this.selectedTimeSlots[currentDate]) {
  197. const index = this.selectedTimeSlots[currentDate].indexOf(item.timeStamp);
  198. if (index > -1) {
  199. this.selectedTimeSlots[currentDate].splice(index, 1);
  200. }
  201. }
  202. }
  203. this.hosts(item);
  204. },
  205. async handleTimeClick2() {
  206. let item = this.upItem
  207. // console.log(this.timeHostArr[this.day_index], '>>>this.timeHostArr[this.day_index]');
  208. // 处理减法时 (选中状态重置)
  209. this.timeHostArr[this.day_index].forEach(s => {
  210. s.disabled = item.hasReservation === 1
  211. });
  212. // this.hosts(item);
  213. // console.log(this.timeArr, '.>>>>item1111');
  214. const durationMs = this.businessDuration * this.minQuantity * 60 * 1000;
  215. const itemTime = item.timeStamp > 9999999999 ? item.timeStamp : item.timeStamp * 1000;
  216. const slots = this.timeHostArr[this.day_index] || [];
  217. const currentDate = this.timeArr[this.day_index];
  218. // 1. 判断当前时间是否在任何已预约时间段的服务时长范围内
  219. let inReservedRange = false;
  220. for (let slot of slots) {
  221. if (slot.hasReservation === 1) {
  222. const reservedTime = slot.timeStamp > 9999999999 ? slot.timeStamp : slot.timeStamp * 1000;
  223. const start = reservedTime - durationMs;
  224. const end = reservedTime + durationMs;
  225. if (itemTime >= start && itemTime < end) {
  226. inReservedRange = true;
  227. break;
  228. }
  229. }
  230. }
  231. if (inReservedRange) {
  232. uni.showToast({ title: '服务时长不足,请选择其他时间段', icon: 'none' });
  233. return false;
  234. }
  235. // 2. 判断当前点击时间与所有已选时间段的服务时长有无重叠(多选判断)
  236. const selectedArr = this.selectedTimeSlots[currentDate] || [];
  237. if (!item.checked) { // 只在选中时判断
  238. for (let t of selectedArr) {
  239. if (t === item.timeStamp) continue; // 跳过自己
  240. const tStart = t > 9999999999 ? t : t * 1000;
  241. const tEnd = tStart + durationMs;
  242. if (
  243. (itemTime >= tStart && itemTime < tEnd) ||
  244. (itemTime < tStart && itemTime + durationMs > tStart)
  245. ) {
  246. uni.showToast({ title: '时间段不在服务范围内', icon: 'none' });
  247. return false;
  248. }
  249. }
  250. }
  251. // 3. 其他逻辑保持不变
  252. const seconds = this.businessDuration * this.minQuantity * 60;
  253. // console.log(this.businessDuration, this.minQuantity, '>>>>>9999');
  254. const endTimestamp = (item.timeStamp + seconds);
  255. // console.log(endTimestamp, '>>>>>>endTimestamp');
  256. const filteredSlots = this.timeHostArr[this.day_index].filter(i => {
  257. return (i.timeStamp > item.timeStamp &&
  258. i.timeStamp <= endTimestamp)
  259. });
  260. this.filteredSlots = filteredSlots
  261. // console.log(filteredSlots, '》》》》》》filteredSlots');
  262. // 时间差值(数组中的最后一项 - 当前点击项)
  263. const timestampDifferenceValue = filteredSlots.length ? (filteredSlots[filteredSlots.length - 1].timeStamp - item.timeStamp) * 1000 : 0
  264. // 选择时间,后续服务时间是否充足,不充足结束逻辑执行 timeStamp
  265. if (timestampDifferenceValue < durationMs) { // 所选时间差值 小于 服务时间值 结束执行
  266. uni.showToast({ title: '所选时间的服务时间不充足!', icon: 'none' });
  267. return false
  268. }
  269. filteredSlots.forEach(v => {
  270. v.disabled = true
  271. });
  272. console.log('>>>>>执行');
  273. if (!this.selectedTimeSlots[currentDate]) {
  274. this.selectedTimeSlots[currentDate] = [];
  275. }
  276. this.selectedTimeSlots[currentDate].push(item.timeStamp);
  277. // this.hosts(item);
  278. },
  279. // 点击日期
  280. dayList(e, index) {
  281. this.day_index = index
  282. this.$emit('getByDate', this.timeArr[index])
  283. },
  284. // 转换时间戳为毫秒
  285. ensureMillisecond(timestamp) {
  286. return timestamp > 9999999999 ? timestamp : timestamp * 1000;
  287. },
  288. shouldDisable(item) {
  289. if (!item) return true;
  290. // 已预约
  291. if (item.hasReservation === 1) return true;
  292. const itemTime = this.ensureMillisecond(item.timeStamp);
  293. // 过去时间
  294. if (this.nowTimes > itemTime) return true;
  295. return false;
  296. },
  297. hosts(item) {
  298. const itemTime = this.ensureMillisecond(item.timeStamp);
  299. this.host_index = item.timeStamp; // 显示用原始值
  300. this.selectedTime = itemTime;
  301. this.disableAfterTime = itemTime + (this.businessDuration * this.minQuantity * 60 * 1000);
  302. this.$emit('getByTime', {
  303. ...item,
  304. timeStamp: itemTime // 传递转换后的值
  305. });
  306. },
  307. // 点击立即预约
  308. sub() {
  309. if (this.host_index == '') {
  310. this.$tool.toast('请选择时间');
  311. } else {
  312. let day = this.dayArr[this.day_index];
  313. let time = this.times(this.host_index);
  314. let comTime = {
  315. days: day.days,
  316. weeks: day.weeks,
  317. hours: this.host_All.hours,
  318. timeStamp: this.host_All.timeStamp,
  319. time: time
  320. };
  321. this.$emit('getTime', comTime);
  322. }
  323. },
  324. // 格式化时间
  325. times(data) {
  326. let date = new Date(data * 1000);
  327. let h = date.getHours();
  328. h = h < 10 ? ('0' + h) : h; // 小时补0
  329. let m = date.getMinutes();
  330. m = m < 10 ? ('0' + m) : m; // 分钟补0
  331. return h + ':' + m;
  332. },
  333. time(data, type) {
  334. let date = new Date(data * 1000);
  335. let y = date.getFullYear();
  336. let MM = date.getMonth() + 1;
  337. MM = MM < 10 ? ('0' + MM) : MM; // 月补0
  338. let d = date.getDate();
  339. d = d < 10 ? ('0' + d) : d; // 天补0
  340. let h = date.getHours();
  341. h = h < 10 ? ('0' + h) : h; // 小时补0
  342. let m = date.getMinutes();
  343. m = m < 10 ? ('0' + m) : m; // 分钟补0
  344. let s = date.getSeconds();
  345. s = s < 10 ? ('0' + s) : s; // 秒补0
  346. if (type == 'yymmdd') {
  347. return y + '-' + MM + '-' + d;
  348. } else if (type == 'hhmmss') {
  349. return h + ':' + m + ':' + s;
  350. } else {
  351. return y + '-' + MM + '-' + d + ' ' + h + ':' + m + ':' + s;
  352. }
  353. }
  354. }
  355. }
  356. </script>
  357. <style lang="scss">
  358. page {
  359. background-color: #F4F4F4;
  360. }
  361. .calendar {
  362. width: 710rpx;
  363. height: 460rpx;
  364. background-color: #FFFFFF;
  365. margin: 20rpx auto 10rpx;
  366. border-radius: 8rpx;
  367. }
  368. .calendar_day_scroll {
  369. width: 100%;
  370. overflow-x: auto;
  371. overflow-y: hidden;
  372. white-space: nowrap;
  373. &::-webkit-scrollbar {
  374. display: none;
  375. }
  376. }
  377. .calendar_day {
  378. display: flex;
  379. flex-direction: row;
  380. width: max-content;
  381. height: 120rpx;
  382. }
  383. .day_x {
  384. display: flex;
  385. flex-direction: column;
  386. justify-content: center;
  387. align-items: center;
  388. width: 100rpx;
  389. height: 100%;
  390. font-size: 30rpx;
  391. color: #333333;
  392. flex-shrink: 0;
  393. margin-right: 10rpx;
  394. background: #f8f8f8;
  395. border-radius: 12rpx;
  396. cursor: pointer;
  397. }
  398. .day_x_a {
  399. font-weight: bold;
  400. margin-bottom: 8rpx;
  401. }
  402. .day_x_b {
  403. font-size: 28rpx;
  404. color: #666;
  405. }
  406. .calendar_time_scroll {
  407. width: 100%;
  408. height: 380rpx;
  409. overflow-y: auto;
  410. background: #fff;
  411. border-radius: 8rpx;
  412. margin-bottom: 20rpx;
  413. }
  414. .calendar_time {
  415. display: flex;
  416. width: 100%;
  417. flex-flow: row wrap;
  418. align-content: flex-start;
  419. margin: 20rpx 0;
  420. overflow-y: auto;
  421. }
  422. .time_x {
  423. display: flex;
  424. flex-flow: row;
  425. justify-content: center;
  426. align-items: center;
  427. width: 20%;
  428. height: 54rpx;
  429. border-radius: 26rpx;
  430. margin: 10rpx 0;
  431. font-size: 30rpx;
  432. color: #333333;
  433. display: flex;
  434. flex-direction: column;
  435. .hasRe-text {
  436. font-size: 20rpx;
  437. color: #999999;
  438. }
  439. }
  440. .time_x_sty {
  441. background-color: #FFE97B;
  442. color: #000000 !important;
  443. }
  444. .sub {
  445. display: flex;
  446. justify-content: center;
  447. align-items: center;
  448. width: 710rpx;
  449. height: 100rpx;
  450. border-radius: 50rpx;
  451. margin: 30rpx auto;
  452. color: #FFFFFF;
  453. font-size: 36rpx;
  454. background-color: #FE3B3C;
  455. }
  456. .time_x {
  457. /* 正常状态样式 */
  458. &.disabled-time {
  459. background-color: #f2f2f2;
  460. color: #999999;
  461. pointer-events: none;
  462. }
  463. &.time_x_sty {
  464. background-color: #FFE97B;
  465. color: #000000;
  466. }
  467. }
  468. .calendar_footer {
  469. width: 100%;
  470. background: #fff;
  471. border-radius: 12rpx;
  472. margin: 0 auto 20rpx auto;
  473. padding: 24rpx 32rpx;
  474. box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.04);
  475. display: flex;
  476. align-items: flex-start;
  477. min-height: 60rpx;
  478. }
  479. .remark_content {
  480. color: #333;
  481. word-break: break-all;
  482. flex: 1;
  483. }
  484. </style>