its-calendar.vue 7.1 KB

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