its-calendar.vue 9.3 KB

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