its-calendar.vue 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. <template>
  2. <view>
  3. <view class="calendar">
  4. <view class="calendar_day">
  5. <view class="day_x" :style="{'color': (day_index == index ? '#FE3B3C' : '')}"
  6. v-for="(item, index) in dayArr" :key="index" @click.stop="dayList(item,index)">
  7. <view class="day_x_a">{{item.weeks}}</view>
  8. <view class="day_x_b">{{item.days}}</view>
  9. </view>
  10. </view>
  11. <view class="calendar_time">
  12. <view class="time_x"
  13. :class="{
  14. 'time_x_sty': item?.checked,
  15. 'disabled-time': item?.disabled
  16. }"
  17. v-for="(item, index) in timeHostArr[day_index]"
  18. :key="index"
  19. @click="handleTimeClick(item)">
  20. <text>{{item?.hours}}</text>
  21. <text v-if="item.hasReservation === 1" class="hasRe-text">已预约</text>
  22. </view>
  23. </view>
  24. </view>
  25. <!-- <view class="sub" @click="sub()">
  26. 立即预约
  27. </view> -->
  28. </view>
  29. </template>
  30. <script>
  31. export default {
  32. props: {
  33. timeArr: {
  34. type: Array,
  35. default: () => []
  36. },
  37. timeHostArr: {
  38. type: Array,
  39. default: () => []
  40. },
  41. businessDuration: {
  42. type: Number,
  43. default: 0
  44. } // 从父组件接收的服务时长(分钟)
  45. },
  46. data() {
  47. return {
  48. dayArr: [],
  49. day_index: 0,
  50. host_index: '',
  51. nowTimes: new Date().getTime(), // 只保留一个定义
  52. disableAfterTime: null,
  53. selectedTime: null,
  54. selectedDay: null, // 新增:记录选择的日期
  55. selectedTimeSlots: {}, // 存储每个日期的选择状态
  56. }
  57. },
  58. watch: {
  59. timeArr: {
  60. handler(newVal) {
  61. console.log(newVal, '>>>>>>>时间范围');
  62. let dateArr = newVal.map(item => {
  63. let day = new Date(item)
  64. const daysOfWeek = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
  65. return {
  66. weeks: daysOfWeek[day.getDay()],
  67. days: item.slice(5)
  68. }
  69. })
  70. this.dayArr = dateArr
  71. },
  72. immediate: true,
  73. deep: true
  74. },
  75. timeHostArr: {
  76. handler(newVal) {
  77. console.log(newVal, '>>>>>>>时间更新');
  78. // 当timeHostArr更新时,恢复之前的选择状态
  79. if (newVal && newVal.length > 0) {
  80. this.restoreSelections();
  81. }
  82. },
  83. deep: true
  84. }
  85. },
  86. mounted() {
  87. },
  88. methods: {
  89. // 恢复选择状态
  90. restoreSelections() {
  91. if (!this.timeHostArr[this.day_index]) return;
  92. // 遍历当前日期的时间槽
  93. this.timeHostArr[this.day_index].forEach(slot => {
  94. // 检查这个时间槽是否在之前被选中
  95. const dateKey = this.timeArr[this.day_index];
  96. if (this.selectedTimeSlots[dateKey] &&
  97. this.selectedTimeSlots[dateKey].includes(slot.timeStamp)) {
  98. slot.checked = true;
  99. // 计算并禁用后续的时间段
  100. const seconds = this.businessDuration * 60;
  101. const endTimestamp = slot.timeStamp + seconds;
  102. // 禁用后续的时间段
  103. this.timeHostArr[this.day_index].forEach(s => {
  104. if (s.timeStamp > slot.timeStamp &&
  105. s.timeStamp <= endTimestamp) {
  106. s.disabled = true;
  107. }
  108. });
  109. }
  110. });
  111. },
  112. handleTimeClick(item) {
  113. console.log(this.businessDuration, '>>>>>>businessDuration');
  114. // 禁用点击,取消点击触发
  115. if (item.disabled) return false
  116. // 计算服务时间秒
  117. const seconds = this.businessDuration * 60
  118. // 计算最终服务时间
  119. const endTimestamp = (item.timeStamp + seconds)
  120. // 禁用区域/释放区域数据
  121. const filteredSlots = this.timeHostArr[this.day_index].filter(i => {
  122. return (i.timeStamp > item.timeStamp &&
  123. i.timeStamp <= endTimestamp)
  124. })
  125. console.log(filteredSlots, '>>>>>filteredSlots');
  126. // 当前日期
  127. const currentDate = this.timeArr[this.day_index];
  128. if (!item.checked) {
  129. // 计算禁用时间区域
  130. filteredSlots.forEach(v => {
  131. v.disabled = true
  132. })
  133. item.checked = true
  134. // 保存选择状态
  135. if (!this.selectedTimeSlots[currentDate]) {
  136. this.selectedTimeSlots[currentDate] = [];
  137. }
  138. this.selectedTimeSlots[currentDate].push(item.timeStamp);
  139. } else {
  140. // 释放禁用区域
  141. filteredSlots.forEach(v => {
  142. if (v.hasReservation !== 1) v.disabled = false
  143. })
  144. item.checked = false
  145. // 移除选择状态
  146. if (this.selectedTimeSlots[currentDate]) {
  147. const index = this.selectedTimeSlots[currentDate].indexOf(item.timeStamp);
  148. if (index > -1) {
  149. this.selectedTimeSlots[currentDate].splice(index, 1);
  150. }
  151. }
  152. }
  153. this.hosts(item);
  154. },
  155. // 点击日期
  156. dayList(e, index) {
  157. this.day_index = index
  158. this.$emit('getByDate', this.timeArr[index])
  159. },
  160. // 转换时间戳为毫秒
  161. ensureMillisecond(timestamp) {
  162. return timestamp > 9999999999 ? timestamp : timestamp * 1000;
  163. },
  164. shouldDisable(item) {
  165. if (!item) return true;
  166. // 已预约
  167. if (item.hasReservation === 1) return true;
  168. const itemTime = this.ensureMillisecond(item.timeStamp);
  169. // 过去时间
  170. if (this.nowTimes > itemTime) return true;
  171. return false;
  172. },
  173. hosts(item) {
  174. const itemTime = this.ensureMillisecond(item.timeStamp);
  175. this.host_index = item.timeStamp; // 显示用原始值
  176. this.selectedTime = itemTime;
  177. this.disableAfterTime = itemTime + (this.businessDuration * 60 * 1000);
  178. this.$emit('getByTime', {
  179. ...item,
  180. timeStamp: itemTime // 传递转换后的值
  181. });
  182. },
  183. // 点击立即预约
  184. sub() {
  185. if (this.host_index == '') {
  186. this.$tool.toast('请选择时间');
  187. } else {
  188. let day = this.dayArr[this.day_index];
  189. let time = this.times(this.host_index);
  190. let comTime = {
  191. days: day.days,
  192. weeks: day.weeks,
  193. hours: this.host_All.hours,
  194. timeStamp: this.host_All.timeStamp,
  195. time: time
  196. };
  197. this.$emit('getTime', comTime);
  198. }
  199. },
  200. // 格式化时间
  201. times(data) {
  202. let date = new Date(data * 1000);
  203. let h = date.getHours();
  204. h = h < 10 ? ('0' + h) : h; // 小时补0
  205. let m = date.getMinutes();
  206. m = m < 10 ? ('0' + m) : m; // 分钟补0
  207. return h + ':' + m;
  208. },
  209. time(data, type) {
  210. let date = new Date(data * 1000);
  211. let y = date.getFullYear();
  212. let MM = date.getMonth() + 1;
  213. MM = MM < 10 ? ('0' + MM) : MM; // 月补0
  214. let d = date.getDate();
  215. d = d < 10 ? ('0' + d) : d; // 天补0
  216. let h = date.getHours();
  217. h = h < 10 ? ('0' + h) : h; // 小时补0
  218. let m = date.getMinutes();
  219. m = m < 10 ? ('0' + m) : m; // 分钟补0
  220. let s = date.getSeconds();
  221. s = s < 10 ? ('0' + s) : s; // 秒补0
  222. if (type == 'yymmdd') {
  223. return y + '-' + MM + '-' + d;
  224. } else if (type == 'hhmmss') {
  225. return h + ':' + m + ':' + s;
  226. } else {
  227. return y + '-' + MM + '-' + d + ' ' + h + ':' + m + ':' + s;
  228. }
  229. }
  230. }
  231. }
  232. </script>
  233. <style lang="scss">
  234. page {
  235. background-color: #F4F4F4;
  236. }
  237. .calendar {
  238. width: 710rpx;
  239. height: 460rpx;
  240. background-color: #FFFFFF;
  241. margin: 20rpx auto 10rpx;
  242. border-radius: 8rpx;
  243. }
  244. .calendar_day {
  245. display: flex;
  246. width: 100%;
  247. height: 120rpx;
  248. .day_x {
  249. display: flex;
  250. flex-flow: column nowrap;
  251. justify-content: center;
  252. align-items: center;
  253. width: 20%;
  254. height: 100%;
  255. font-size: 30rpx;
  256. color: #333333;
  257. }
  258. }
  259. .calendar_time {
  260. display: flex;
  261. width: 100%;
  262. height: 448rpx;
  263. flex-flow: row wrap;
  264. align-content: flex-start;
  265. margin: 20rpx 0;
  266. overflow-y: auto;
  267. .time_x {
  268. display: flex;
  269. flex-flow: row;
  270. justify-content: center;
  271. align-items: center;
  272. width: 20%;
  273. height: 54rpx;
  274. border-radius: 26rpx;
  275. margin: 10rpx 0;
  276. font-size: 30rpx;
  277. color: #333333;
  278. display: flex;
  279. flex-direction: column;
  280. .hasRe-text {
  281. font-size: 20rpx;
  282. color: #999999;
  283. }
  284. }
  285. .time_x_sty {
  286. background-color: #FFE97B;
  287. color: #000000 !important;
  288. }
  289. }
  290. .sub {
  291. display: flex;
  292. justify-content: center;
  293. align-items: center;
  294. width: 710rpx;
  295. height: 100rpx;
  296. border-radius: 50rpx;
  297. margin: 30rpx auto;
  298. color: #FFFFFF;
  299. font-size: 36rpx;
  300. background-color: #FE3B3C;
  301. }
  302. .time_x {
  303. /* 正常状态样式 */
  304. &.disabled-time {
  305. background-color: #f2f2f2;
  306. color: #999999;
  307. pointer-events: none;
  308. }
  309. &.time_x_sty {
  310. background-color: #FFE97B;
  311. color: #000000;
  312. }
  313. }
  314. </style>