its-calendar - 副本.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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': host_index == item?.timeStamp,
  15. 'disabled-time': shouldDisable(item)
  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. methods: {
  75. handleTimeClick(item) {
  76. if (!this.shouldDisable(item)) {
  77. this.hosts(item);
  78. }
  79. },
  80. // 点击日期
  81. dayList(e, index) {
  82. this.day_index = index
  83. this.$emit('getByDate', this.timeArr[index])
  84. },
  85. // 转换时间戳为毫秒
  86. ensureMillisecond(timestamp) {
  87. return timestamp > 9999999999 ? timestamp : timestamp * 1000;
  88. },
  89. shouldDisable(item) {
  90. if (!item) return true;
  91. const itemTime = this.ensureMillisecond(item.timeStamp);
  92. // 已预约
  93. if (item.hasReservation === 1) return true;
  94. // 过去时间
  95. if (this.nowTimes > itemTime) return true;
  96. // 选择后的时间段
  97. if (this.selectedTime) {
  98. return itemTime > this.selectedTime &&
  99. itemTime <= this.disableAfterTime;
  100. }
  101. return false;
  102. },
  103. hosts(item) {
  104. const itemTime = this.ensureMillisecond(item.timeStamp);
  105. this.host_index = item.timeStamp; // 显示用原始值
  106. this.selectedTime = itemTime;
  107. this.disableAfterTime = itemTime + (this.businessDuration * 60 * 1000);
  108. this.$emit('getByTime', {
  109. ...item,
  110. timeStamp: itemTime // 传递转换后的值
  111. });
  112. },
  113. // 点击立即预约
  114. sub() {
  115. if (this.host_index == '') {
  116. this.$tool.toast('请选择时间');
  117. } else {
  118. let day = this.dayArr[this.day_index];
  119. let time = this.times(this.host_index);
  120. let comTime = {
  121. days: day.days,
  122. weeks: day.weeks,
  123. hours: this.host_All.hours,
  124. timeStamp: this.host_All.timeStamp,
  125. time: time
  126. };
  127. this.$emit('getTime', comTime);
  128. }
  129. },
  130. // 格式化时间
  131. times(data) {
  132. let date = new Date(data * 1000);
  133. let h = date.getHours();
  134. h = h < 10 ? ('0' + h) : h; // 小时补0
  135. let m = date.getMinutes();
  136. m = m < 10 ? ('0' + m) : m; // 分钟补0
  137. return h + ':' + m;
  138. },
  139. time(data, type) {
  140. let date = new Date(data * 1000);
  141. let y = date.getFullYear();
  142. let MM = date.getMonth() + 1;
  143. MM = MM < 10 ? ('0' + MM) : MM; // 月补0
  144. let d = date.getDate();
  145. d = d < 10 ? ('0' + d) : d; // 天补0
  146. let h = date.getHours();
  147. h = h < 10 ? ('0' + h) : h; // 小时补0
  148. let m = date.getMinutes();
  149. m = m < 10 ? ('0' + m) : m; // 分钟补0
  150. let s = date.getSeconds();
  151. s = s < 10 ? ('0' + s) : s; // 秒补0
  152. if (type == 'yymmdd') {
  153. return y + '-' + MM + '-' + d;
  154. } else if (type == 'hhmmss') {
  155. return h + ':' + m + ':' + s;
  156. } else {
  157. return y + '-' + MM + '-' + d + ' ' + h + ':' + m + ':' + s;
  158. }
  159. }
  160. }
  161. }
  162. </script>
  163. <style lang="scss">
  164. page {
  165. background-color: #F4F4F4;
  166. }
  167. .calendar {
  168. width: 710rpx;
  169. height: 460rpx;
  170. background-color: #FFFFFF;
  171. margin: 20rpx auto 10rpx;
  172. border-radius: 8rpx;
  173. }
  174. .calendar_day {
  175. display: flex;
  176. width: 100%;
  177. height: 120rpx;
  178. .day_x {
  179. display: flex;
  180. flex-flow: column nowrap;
  181. justify-content: center;
  182. align-items: center;
  183. width: 20%;
  184. height: 100%;
  185. font-size: 30rpx;
  186. color: #333333;
  187. }
  188. }
  189. .calendar_time {
  190. display: flex;
  191. width: 100%;
  192. height: 448rpx;
  193. flex-flow: row wrap;
  194. align-content: flex-start;
  195. margin: 20rpx 0;
  196. overflow-y: auto;
  197. .time_x {
  198. display: flex;
  199. flex-flow: row;
  200. justify-content: center;
  201. align-items: center;
  202. width: 20%;
  203. height: 54rpx;
  204. border-radius: 26rpx;
  205. margin: 10rpx 0;
  206. font-size: 30rpx;
  207. color: #333333;
  208. display: flex;
  209. flex-direction: column;
  210. .hasRe-text {
  211. font-size: 20rpx;
  212. color: #999999;
  213. }
  214. }
  215. .time_x_sty {
  216. background-color: #FFE97B;
  217. color: #000000 !important;
  218. }
  219. }
  220. .sub {
  221. display: flex;
  222. justify-content: center;
  223. align-items: center;
  224. width: 710rpx;
  225. height: 100rpx;
  226. border-radius: 50rpx;
  227. margin: 30rpx auto;
  228. color: #FFFFFF;
  229. font-size: 36rpx;
  230. background-color: #FE3B3C;
  231. }
  232. .time_x {
  233. /* 正常状态样式 */
  234. &.disabled-time {
  235. background-color: #f2f2f2;
  236. color: #999999;
  237. pointer-events: none;
  238. }
  239. &.time_x_sty {
  240. background-color: #FFE97B;
  241. color: #000000;
  242. }
  243. }
  244. </style>