its-calendar.vue 8.0 KB

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