TimePicker.vue 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <template>
  2. <uni-popup ref="popup" type="bottom" :safe-area="false">
  3. <view class="time-picker-container" :style="{ marginBottom: bottomOffset + 'rpx' }">
  4. <view class="picker-header">
  5. <text class="title">期望取件时间</text>
  6. <view class="close-btn" @click="close">
  7. <zui-svg-icon icon="general-close" :width="18" :height="18" color="#666" />
  8. </view>
  9. </view>
  10. <picker-view v-if="visible" class="picker-view" :indicator-style="indicatorStyle" :value="pickerValue" @change="handleChange" :immediate-change="true">
  11. <picker-view-column>
  12. <view class="picker-item" v-for="(item, index) in days" :key="index">{{ item.label }}</view>
  13. </picker-view-column>
  14. <picker-view-column>
  15. <view class="picker-item" v-for="(item, index) in hours" :key="index">{{ item.label }}</view>
  16. </picker-view-column>
  17. <picker-view-column>
  18. <view class="picker-item" v-for="(item, index) in minutes" :key="index">{{ item.label }}</view>
  19. </picker-view-column>
  20. </picker-view>
  21. <view class="confirm-button-wrapper">
  22. <button class="confirm-btn" @click="handleConfirm">确认</button>
  23. </view>
  24. </view>
  25. </uni-popup>
  26. </template>
  27. <script>
  28. import dayjs from 'dayjs';
  29. const minutes = [0, 10, 20, 30, 40, 50];
  30. export default {
  31. name: 'TimePicker',
  32. props: {
  33. bottomOffset: {
  34. type: [Number, String],
  35. default: 0
  36. }
  37. },
  38. data() {
  39. return {
  40. visible: false, // 控制picker-view的渲染
  41. pickerValue: [0, 0, 0],
  42. tempPickerValue: [0, 0, 0], // 临时存储滚动过程中的值
  43. debounceTimer: null,
  44. days: [],
  45. hours: [],
  46. minutes: [],
  47. indicatorStyle: `height: 80rpx;`,
  48. };
  49. },
  50. created() {
  51. this.initTime();
  52. },
  53. methods: {
  54. open() {
  55. this.visible = true;
  56. this.initTime();
  57. this.$refs.popup.open();
  58. },
  59. close() {
  60. this.visible = false;
  61. this.$refs.popup.close();
  62. },
  63. initTime() {
  64. const now = dayjs(); // Use current time
  65. this.days = this.generateDays();
  66. const isToday = true;
  67. let availableHoursToday = this.generateHours(now, isToday);
  68. // If no hours available today, switch to tomorrow
  69. if (availableHoursToday.length <= 1) { // Only "立即"
  70. const tomorrow = dayjs().add(1, 'day').startOf('day');
  71. this.days = this.generateDays(); // Regenerate days if needed
  72. this.hours = this.generateHours(tomorrow, false);
  73. this.minutes = this.generateMinutes(tomorrow, false, this.hours[0].value);
  74. this.pickerValue = [1, 0, 0]; // Select "Tomorrow", first hour, first minute
  75. this.tempPickerValue = [1, 0, 0];
  76. return;
  77. }
  78. this.hours = availableHoursToday;
  79. // Default to "立即"
  80. const initialHourIndex = 0;
  81. const selectedHour = this.hours[initialHourIndex].value;
  82. this.minutes = this.generateMinutes(now, isToday, selectedHour);
  83. const initialMinuteIndex = 0;
  84. this.pickerValue = [0, initialHourIndex, initialMinuteIndex];
  85. this.tempPickerValue = [0, initialHourIndex, initialMinuteIndex];
  86. },
  87. generateDays() {
  88. return [
  89. { label: '今天', value: dayjs().format('YYYY-MM-DD') },
  90. { label: '明天', value: dayjs().add(1, 'day').format('YYYY-MM-DD') },
  91. ];
  92. },
  93. generateHours(now, isToday) {
  94. const hours = [];
  95. if (isToday) {
  96. hours.push({ label: '立即', value: -1 });
  97. let startHour = now.hour() + 1; // 当前小时的下一个小时
  98. // Find if any minutes are available in the current hour
  99. const firstAvailableMinute = minutes.find(m => m > now.minute());
  100. // If no minutes available in current hour, or if the last minute has passed, start from the next hour
  101. if (firstAvailableMinute === undefined) {
  102. startHour++;
  103. }
  104. for (let i = startHour; i <= 23; i++) {
  105. hours.push({ label: `${i}时`, value: i });
  106. }
  107. } else {
  108. for (let i = 0; i <= 23; i++) {
  109. hours.push({ label: `${i}时`, value: i });
  110. }
  111. }
  112. return hours;
  113. },
  114. generateMinutes(now, isToday, selectedHour) {
  115. if (selectedHour === -1) {
  116. return [{ label: '', value: -1 }];
  117. }
  118. let availableMinutes = minutes;
  119. if (isToday && selectedHour === now.hour()) {
  120. const currentMinute = now.minute();
  121. availableMinutes = minutes.filter(m => m > currentMinute);
  122. }
  123. return availableMinutes.map(m => ({ label: `${m}分`, value: m }));
  124. },
  125. handleChange(e) {
  126. const val = e.detail.value;
  127. this.tempPickerValue = val;
  128. if (this.debounceTimer) {
  129. clearTimeout(this.debounceTimer);
  130. }
  131. this.debounceTimer = setTimeout(() => {
  132. this.updatePickers(val);
  133. }, 100);
  134. },
  135. updatePickers(val, isSync = false) {
  136. const [dayIndex, hourIndex, minuteIndex] = val;
  137. const oldPickerValue = this.pickerValue;
  138. const now = dayjs();
  139. const isToday = dayIndex === 0;
  140. let newHours = this.hours;
  141. let newMinutes = this.minutes;
  142. let finalHourIndex = hourIndex;
  143. let finalMinuteIndex = minuteIndex;
  144. // Day changed
  145. if (oldPickerValue[0] !== dayIndex) {
  146. newHours = this.generateHours(now, isToday);
  147. // 修正越界,尽量保持用户的选择,而不是强制归零
  148. if (finalHourIndex >= newHours.length) {
  149. finalHourIndex = 0;
  150. }
  151. }
  152. // Calculate selected hour based on current indices
  153. const selectedHour = newHours[finalHourIndex] ? newHours[finalHourIndex].value : -1;
  154. // Always regenerate minutes to ensure correctness (e.g. today/tomorrow switch, or hour switch)
  155. // Optimization: we could check if generation parameters actually changed, but it's fast enough.
  156. newMinutes = this.generateMinutes(now, isToday, selectedHour);
  157. // 修正越界
  158. if (finalMinuteIndex >= newMinutes.length) {
  159. finalMinuteIndex = 0;
  160. }
  161. this.hours = newHours;
  162. this.minutes = newMinutes;
  163. this.debounceTimer = null;
  164. const newValue = [dayIndex, finalHourIndex, finalMinuteIndex];
  165. if (isSync) {
  166. this.pickerValue = newValue;
  167. } else {
  168. // Use setTimeout to avoid visual glitch on picker reset
  169. setTimeout(() => {
  170. this.pickerValue = newValue;
  171. }, 0);
  172. }
  173. },
  174. handleConfirm() {
  175. if (this.debounceTimer) {
  176. clearTimeout(this.debounceTimer);
  177. this.updatePickers(this.tempPickerValue, true);
  178. }
  179. const [dayIndex, hourIndex, minuteIndex] = this.pickerValue;
  180. const selectedDay = this.days[dayIndex];
  181. const selectedHour = this.hours[hourIndex];
  182. const selectedMinute = this.minutes[minuteIndex];
  183. let result = {};
  184. if (selectedHour && selectedHour.value === -1) {
  185. result = {
  186. label: '立即取件',
  187. value: dayjs().format('YYYY-MM-DD HH:mm'),
  188. };
  189. } else {
  190. const date = selectedDay.value;
  191. const hour = selectedHour?.value ?? 0;
  192. const minute = selectedMinute?.value ?? 0;
  193. const finalDate = dayjs(`${date} ${hour}:${minute}`).toDate();
  194. result = {
  195. label: dayjs(finalDate).format('YYYY-MM-DD HH:mm'),
  196. value: dayjs(finalDate).format('YYYY-MM-DD HH:mm'),
  197. };
  198. }
  199. this.$emit('confirm', result);
  200. this.close();
  201. },
  202. },
  203. };
  204. </script>
  205. <style lang="scss" scoped>
  206. .time-picker-container {
  207. background-color: #fff;
  208. border-top-left-radius: 24rpx;
  209. border-top-right-radius: 24rpx;
  210. height: 45vh;
  211. display: flex;
  212. flex-direction: column;
  213. }
  214. .picker-header {
  215. position: relative;
  216. text-align: center;
  217. padding: 30rpx;
  218. flex-shrink: 0;
  219. .title {
  220. font-size: 44rpx;
  221. font-weight: 500;
  222. }
  223. .close-btn {
  224. position: absolute;
  225. right: 30rpx;
  226. top: 50%;
  227. transform: translateY(-50%);
  228. padding: 10rpx;
  229. }
  230. }
  231. .picker-view {
  232. width: 100%;
  233. height: 100%;
  234. }
  235. .picker-item {
  236. display: flex;
  237. align-items: center;
  238. justify-content: center;
  239. font-size: 42rpx;
  240. }
  241. .confirm-button-wrapper {
  242. padding: 20rpx 30rpx;
  243. padding-bottom: calc(20rpx + constant(safe-area-inset-bottom));
  244. padding-bottom: calc(20rpx + env(safe-area-inset-bottom));
  245. flex-shrink: 0;
  246. }
  247. .confirm-btn {
  248. background: linear-gradient(to right, #409eff, #67c2ff);
  249. color: white;
  250. border-radius: 50rpx;
  251. font-size: 42rpx;
  252. height: 88rpx;
  253. line-height: 88rpx;
  254. border: none;
  255. &::after {
  256. border: none;
  257. }
  258. }
  259. </style>