its-calendar.vue 9.2 KB

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