TimePopup.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. <template>
  2. <u-popup
  3. :show="visible"
  4. mode="bottom"
  5. round="20"
  6. :closeable="false"
  7. @close="close"
  8. :safeAreaInsetBottom="true"
  9. customStyle="height: 80vh;"
  10. >
  11. <view class="time-picker-container">
  12. <!-- 标题栏 -->
  13. <view class="picker-header">
  14. <text class="picker-title">期望上门时间</text>
  15. <text class="close-btn" @tap="close">×</text>
  16. </view>
  17. <!-- 主要内容区域:左侧日期 + 右侧时间 -->
  18. <view class="main-content">
  19. <!-- 左侧日期选择 -->
  20. <scroll-view class="date-sidebar" scroll-y>
  21. <view
  22. v-for="date in dateList"
  23. :key="date.value"
  24. class="date-item"
  25. :class="{
  26. active: selectedDate === date.value,
  27. today: date.isToday
  28. }"
  29. @tap="selectDate(date.value)"
  30. >
  31. <view class="date-content">
  32. <text class="date-name"
  33. :class="{
  34. active: selectedDate === date.value,
  35. today: date.isToday
  36. }">{{ date.label }}</text>
  37. <text class="date-info">{{ date.month }}月{{ date.day }}日</text>
  38. <text class="date-week">{{ date.week }}</text>
  39. </view>
  40. </view>
  41. </scroll-view>
  42. <!-- 右侧时间选择 -->
  43. <scroll-view class="time-content" scroll-y>
  44. <!-- 推荐时间段(仅今天显示) -->
  45. <view v-if="selectedDate === todayDate && withinOneHourSlot" class="recommend-section">
  46. <view class="recommend-header">
  47. <text class="recommend-title">一小时内</text>
  48. </view>
  49. <view
  50. class="recommend-time"
  51. :class="{ active: selectedTime === withinOneHourSlot.value }"
  52. @tap="selectTime(withinOneHourSlot.value, true)"
  53. >
  54. <text class="time-value">{{ withinOneHourSlot.label }}</text>
  55. </view>
  56. </view>
  57. <!-- 普通时间段列表 -->
  58. <view class="time-section">
  59. <view class="section-title">可选时间段</view>
  60. <view v-if="loading" class="loading">加载中...</view>
  61. <view v-else class="time-list">
  62. <view
  63. v-for="timeSlot in timeSlots"
  64. :key="timeSlot.value"
  65. class="time-item"
  66. :class="{
  67. active: selectedTime === timeSlot.value,
  68. disabled: timeSlot.disabled,
  69. night: timeSlot.night
  70. }"
  71. @tap="!timeSlot.disabled && selectTime(timeSlot.value, false)"
  72. >
  73. <text class="time-text">{{ timeSlot.label }}</text>
  74. <text v-if="timeSlot.tag" class="time-tag">{{ timeSlot.tag }}</text>
  75. </view>
  76. </view>
  77. </view>
  78. <!-- 提示信息 -->
  79. <view class="notice-section">
  80. <text class="notice-text">
  81. <!-- 部分区域支持夜间上门,具体以快递员联系为准 -->
  82. </text>
  83. </view>
  84. </scroll-view>
  85. </view>
  86. <!-- 操作按钮 -->
  87. <view class="action-bar">
  88. <button
  89. class="confirm-btn"
  90. :class="{ active: selectedTime }"
  91. @tap="confirm"
  92. >
  93. {{ buttonText }}
  94. </button>
  95. </view>
  96. </view>
  97. </u-popup>
  98. </template>
  99. <script setup>
  100. import { ref, computed, watch, onMounted, onUnmounted } from 'vue'
  101. const props = defineProps({
  102. visible: { type: Boolean, default: false },
  103. days: { type: Number, default: 3 },
  104. startTime: { type: Number, default: 8 },
  105. endTime: { type: Number, default: 20 },
  106. autoSelect: { type: Boolean, default: true }
  107. })
  108. const emit = defineEmits(['update:visible', 'close', 'confirm'])
  109. // 当前时间(每分钟更新)
  110. const currentTime = ref(new Date())
  111. const selectedDate = ref('')
  112. const selectedTime = ref('')
  113. const loading = ref(false)
  114. const isWithinOneHour = ref(false)
  115. // ----- 精确时间计算 -----
  116. const oneHourLater = computed(() => {
  117. const now = currentTime.value
  118. return now.getHours() + now.getMinutes() / 60 + 1
  119. })
  120. const twentyMinutesLater = computed(() => {
  121. const now = new Date(currentTime.value)
  122. now.setMinutes(now.getMinutes() + 10)
  123. return now
  124. })
  125. // ----- 推荐时段(唯一 value 前缀,不与普通时段冲突)-----
  126. const withinOneHourSlot = computed(() => {
  127. if (!dateList.value.length || !dateList.value[0].isToday) return null
  128. const later = twentyMinutesLater.value
  129. let startHour = later.getHours()
  130. let startMinute = Math.ceil(later.getMinutes() / 10) * 10
  131. if (startMinute >= 60) {
  132. startHour += 1
  133. startMinute = 0
  134. }
  135. if (startHour >= props.endTime) return null
  136. let endHour = startHour + 1
  137. let endMinute = startMinute
  138. if (endHour > props.endTime) {
  139. endHour = props.endTime
  140. endMinute = 0
  141. }
  142. const duration = (endHour - startHour) * 60 + (endMinute - startMinute)
  143. if (duration < 30) return null
  144. const startStr = formatTime(startHour, startMinute)
  145. const endStr = formatTime(endHour, endMinute)
  146. return {
  147. start: startHour + startMinute / 60,
  148. end: endHour + endMinute / 60,
  149. label: `(${startStr}-${endStr})`,
  150. value: `immediate:${startStr}-${endStr}`, // ✅ 唯一标识
  151. startHour, startMinute, endHour, endMinute
  152. }
  153. })
  154. // ----- 日期列表 -----
  155. const todayDate = computed(() => formatDate(new Date()))
  156. const dateList = computed(() => {
  157. const days = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
  158. const list = []
  159. const today = new Date()
  160. for (let i = 0; i < props.days; i++) {
  161. const date = new Date(today)
  162. date.setDate(date.getDate() + i)
  163. const month = date.getMonth() + 1
  164. const day = date.getDate()
  165. const week = days[date.getDay()]
  166. const dateStr = formatDate(date)
  167. list.push({
  168. value: dateStr,
  169. label: i === 0 ? '今天' : i === 1 ? '明天' : '后天',
  170. week, month, day,
  171. isToday: i === 0,
  172. displayDate: dateStr
  173. })
  174. }
  175. return list
  176. })
  177. // ----- 基础整点时段 -----
  178. const baseTimeSlots = computed(() => {
  179. const slots = []
  180. for (let hour = props.startTime; hour < props.endTime; hour++) {
  181. slots.push({
  182. start: hour,
  183. end: hour + 1,
  184. label: `${padZero(hour)}:00-${padZero(hour + 1)}:00`,
  185. value: `${padZero(hour)}:00-${padZero(hour + 1)}:00`,
  186. startHour: hour,
  187. endHour: hour + 1
  188. })
  189. }
  190. return slots
  191. })
  192. // ----- 根据所选日期过滤可用时段(精确到小时+分钟)-----
  193. const timeSlots = computed(() => {
  194. const slots = []
  195. const isToday = selectedDate.value === todayDate.value
  196. if (isToday) {
  197. const threshold = oneHourLater.value
  198. baseTimeSlots.value.forEach(slot => {
  199. if (slot.start >= threshold) {
  200. slots.push({
  201. ...slot,
  202. disabled: false,
  203. night: slot.start >= 22,
  204. tag: ''
  205. })
  206. }
  207. })
  208. } else {
  209. baseTimeSlots.value.forEach(slot => {
  210. slots.push({
  211. ...slot,
  212. disabled: false,
  213. night: slot.start >= 22,
  214. tag: ''
  215. })
  216. })
  217. }
  218. return slots
  219. })
  220. // ----- 按钮文字 -----
  221. const buttonText = computed(() => selectedTime.value ? '确定' : '请选择上门时间')
  222. // ----- 核心:切换日期(自动根据 autoSelect 选中第一个可用时段)-----
  223. const selectDate = (date) => {
  224. selectedDate.value = date
  225. selectedTime.value = ''
  226. isWithinOneHour.value = false
  227. // 若开启自动选择,立即为该日期选择一个合适的时间
  228. if (props.autoSelect) {
  229. // 1. 如果是今天且有推荐时段,优先选中推荐
  230. if (date === todayDate.value && withinOneHourSlot.value) {
  231. selectedTime.value = withinOneHourSlot.value.value
  232. isWithinOneHour.value = true
  233. } else {
  234. // 2. 否则选中该日期下第一个可用普通时段
  235. const firstSlot = timeSlots.value[0]
  236. if (firstSlot) {
  237. selectedTime.value = firstSlot.value
  238. isWithinOneHour.value = false
  239. }
  240. }
  241. }
  242. }
  243. // ----- 选择时间(普通时段 / 推荐时段)-----
  244. const selectTime = (time, isImmediate = false) => {
  245. selectedTime.value = time
  246. isWithinOneHour.value = isImmediate
  247. }
  248. // ----- 初始化弹窗(打开时)-----
  249. const initSelection = () => {
  250. if (dateList.value.length) {
  251. // 直接调用 selectDate,复用自动选择逻辑
  252. selectDate(dateList.value[0].value)
  253. }
  254. }
  255. // ----- 确认选择 -----
  256. const confirm = () => {
  257. if (!selectedTime.value) return
  258. const selectedDateObj = dateList.value.find(d => d.value === selectedDate.value)
  259. let selectedTimeSlot
  260. if (isWithinOneHour.value) {
  261. selectedTimeSlot = withinOneHourSlot.value
  262. } else {
  263. selectedTimeSlot = baseTimeSlots.value.find(s => s.value === selectedTime.value)
  264. }
  265. if (!selectedDateObj || !selectedTimeSlot) return
  266. const startHour = isWithinOneHour.value
  267. ? withinOneHourSlot.value.start
  268. : selectedTimeSlot.start
  269. const endHour = isWithinOneHour.value
  270. ? withinOneHourSlot.value.end
  271. : selectedTimeSlot.end
  272. const startTimeStr = formatDateTime(selectedDate.value, startHour)
  273. const endTimeStr = formatDateTime(selectedDate.value, endHour)
  274. const displayText = isWithinOneHour.value
  275. ? `${selectedDateObj.label} 一小时内`
  276. : `${selectedDateObj.label} ${selectedTime.value}`
  277. emit('confirm', {
  278. date: selectedDate.value,
  279. time: selectedTime.value,
  280. dateLabel: selectedDateObj.label,
  281. timeLabel: selectedTime.value,
  282. fullLabel: displayText,
  283. displayText,
  284. startTime: startTimeStr,
  285. endTime: endTimeStr,
  286. isToday: selectedDateObj.isToday,
  287. isImmediate: isWithinOneHour.value,
  288. isNight: selectedTimeSlot.start >= 18,
  289. startHour,
  290. endHour
  291. })
  292. close()
  293. }
  294. // ----- 关闭弹窗 -----
  295. const close = () => {
  296. emit('close')
  297. emit('update:visible', false)
  298. }
  299. // ----- 工具函数 -----
  300. const formatDate = (date) => {
  301. const y = date.getFullYear()
  302. const m = String(date.getMonth() + 1).padStart(2, '0')
  303. const d = String(date.getDate()).padStart(2, '0')
  304. return `${y}-${m}-${d}`
  305. }
  306. const formatTime = (hour, minute) => {
  307. return `${String(hour).padStart(2, '0')}:${String(minute).padStart(2, '0')}`
  308. }
  309. const padZero = (num) => String(num).padStart(2, '0')
  310. const formatDateTime = (dateStr, hourDecimal) => {
  311. const date = new Date(dateStr)
  312. const wholeHour = Math.floor(hourDecimal)
  313. const minutes = Math.round((hourDecimal - wholeHour) * 60)
  314. date.setHours(wholeHour, minutes, 0, 0)
  315. const y = date.getFullYear()
  316. const m = String(date.getMonth() + 1).padStart(2, '0')
  317. const d = String(date.getDate()).padStart(2, '0')
  318. const hh = String(date.getHours()).padStart(2, '0')
  319. const mm = String(date.getMinutes()).padStart(2, '0')
  320. return `${y}-${m}-${d} ${hh}:${mm}:00`
  321. }
  322. // ----- 监听弹窗显示,初始化 -----
  323. watch(() => props.visible, (newVal) => {
  324. if (newVal) {
  325. currentTime.value = new Date()
  326. initSelection()
  327. }
  328. })
  329. // ----- 每分钟刷新当前时间(仅弹窗可见时)-----
  330. let timer = null
  331. onMounted(() => {
  332. timer = setInterval(() => {
  333. if (props.visible) {
  334. currentTime.value = new Date()
  335. }
  336. }, 60000)
  337. })
  338. onUnmounted(() => {
  339. if (timer) clearInterval(timer)
  340. })
  341. </script>
  342. <style scoped lang="less">
  343. .time-picker-container {
  344. height: 80vh;
  345. display: flex;
  346. flex-direction: column;
  347. }
  348. .picker-header {
  349. display: flex;
  350. justify-content: space-between;
  351. align-items: center;
  352. padding: 32rpx 32rpx 24rpx;
  353. border-bottom: 1rpx solid #f0f0f0;
  354. flex-shrink: 0;
  355. }
  356. .picker-title {
  357. font-size: 36rpx;
  358. font-weight: 600;
  359. color: #333;
  360. }
  361. .close-btn {
  362. font-size: 50rpx;
  363. color: #999;
  364. line-height: 40rpx;
  365. padding: 10rpx;
  366. }
  367. .main-content {
  368. flex: 1;
  369. display: flex;
  370. overflow: hidden;
  371. }
  372. .date-sidebar {
  373. width: 220rpx;
  374. background-color: #f8f8f8;
  375. border-right: 1rpx solid #f0f0f0;
  376. }
  377. .date-item {
  378. padding: 32rpx 24rpx;
  379. border-bottom: 1rpx solid #f0f0f0;
  380. }
  381. .date-item.active {
  382. background-color: #fff;
  383. position: relative;
  384. }
  385. .date-item.active::before {
  386. content: '';
  387. position: absolute;
  388. left: 0;
  389. top: 50%;
  390. transform: translateY(-50%);
  391. width: 6rpx;
  392. height: 40rpx;
  393. background-color: #1B64F0;
  394. border-radius: 0 4rpx 4rpx 0;
  395. }
  396. .date-item.today .date-name {
  397. // color: #1B64F0;
  398. color: #333;
  399. }
  400. .date-item.active .date-content {
  401. color: #1B64F0;
  402. font-weight: 600;
  403. }
  404. .date-content {
  405. display: flex;
  406. flex-direction: column;
  407. align-items: center;
  408. text-align: center;
  409. }
  410. .date-name {
  411. font-size: 32rpx;
  412. color: #333;
  413. margin-bottom: 8rpx;
  414. &.active{
  415. color: #1B64F0;
  416. }
  417. }
  418. .date-info {
  419. font-size: 24rpx;
  420. color: #666;
  421. margin-bottom: 4rpx;
  422. }
  423. .date-week {
  424. font-size: 22rpx;
  425. color: #999;
  426. }
  427. .time-content {
  428. flex: 1;
  429. padding: 0 32rpx;
  430. overflow-y: auto;
  431. }
  432. .recommend-section {
  433. margin-top: 32rpx;
  434. padding-bottom: 24rpx;
  435. border-bottom: 1rpx solid #f0f0f0;
  436. }
  437. .recommend-header {
  438. display: flex;
  439. justify-content: space-between;
  440. align-items: center;
  441. margin-bottom: 20rpx;
  442. }
  443. .recommend-title {
  444. font-size: 28rpx;
  445. font-weight: 500;
  446. color: #333;
  447. }
  448. /* 推荐时段:未选中时灰色边框,无背景 */
  449. .recommend-time {
  450. display: flex;
  451. align-items: center;
  452. justify-content: space-between;
  453. padding: 28rpx 24rpx;
  454. border-radius: 16rpx;
  455. border: 2rpx solid #e8e8e8;
  456. transition: all 0.2s;
  457. }
  458. .recommend-time .time-value {
  459. font-size: 32rpx;
  460. font-weight: 500;
  461. color: #333;
  462. }
  463. /* 推荐时段:选中时蓝色背景 + 白色文字 */
  464. .recommend-time.active {
  465. // background-color: #1B64F0;
  466. border-color: #1B64F0;
  467. }
  468. .recommend-time.active .time-value {
  469. // color: white;
  470. }
  471. .time-section {
  472. margin-top: 32rpx;
  473. margin-bottom: 32rpx;
  474. }
  475. .section-title {
  476. font-size: 28rpx;
  477. font-weight: 500;
  478. color: #333;
  479. margin-bottom: 24rpx;
  480. }
  481. .time-list {
  482. display: flex;
  483. flex-direction: column;
  484. gap: 20rpx;
  485. }
  486. .time-item {
  487. padding: 28rpx 24rpx;
  488. border: 2rpx solid #e8e8e8;
  489. border-radius: 12rpx;
  490. display: flex;
  491. align-items: center;
  492. justify-content: space-between;
  493. transition: all 0.2s ease;
  494. }
  495. .time-item.active {
  496. border-color: #1B64F0;
  497. background-color: #fff;
  498. }
  499. .time-item.night {
  500. border-color: #4a5cff;
  501. }
  502. .time-item:not(.disabled):active {
  503. transform: scale(0.98);
  504. opacity: 0.8;
  505. }
  506. .time-item.disabled {
  507. opacity: 0.5;
  508. }
  509. .time-text {
  510. font-size: 28rpx;
  511. color: #333;
  512. }
  513. .time-item.active .time-text {
  514. color: #1B64F0;
  515. font-weight: 500;
  516. }
  517. .time-item.night .time-text {
  518. color: #4a5cff;
  519. }
  520. .time-item .time-tag {
  521. font-size: 20rpx;
  522. padding: 4rpx 12rpx;
  523. border-radius: 12rpx;
  524. background: #f5f5f5;
  525. color: #666;
  526. }
  527. .time-item.active .time-tag {
  528. background: #1B64F0;
  529. color: white;
  530. }
  531. .time-item.night .time-tag {
  532. background: #4a5cff;
  533. color: white;
  534. }
  535. .notice-section {
  536. padding: 24rpx;
  537. margin-bottom: 32rpx;
  538. background: #f8f8f8;
  539. border-radius: 12rpx;
  540. }
  541. .notice-text {
  542. font-size: 24rpx;
  543. color: #666;
  544. line-height: 1.4;
  545. }
  546. .action-bar {
  547. padding: 24rpx 32rpx;
  548. padding-top: 0;
  549. background: #fff;
  550. border-top: 1rpx solid #f0f0f0;
  551. flex-shrink: 0;
  552. }
  553. .confirm-btn {
  554. width: 100%;
  555. height: 88rpx;
  556. background: #f5f5f5;
  557. color: #999;
  558. border-radius: 44rpx;
  559. font-size: 32rpx;
  560. font-weight: 500;
  561. display: flex;
  562. align-items: center;
  563. justify-content: center;
  564. border: none;
  565. transition: all 0.2s ease;
  566. }
  567. .confirm-btn.active {
  568. background: #1B64F0;
  569. color: white;
  570. }
  571. .confirm-btn.active:active {
  572. opacity: 0.9;
  573. transform: scale(0.99);
  574. }
  575. .loading {
  576. text-align: center;
  577. padding: 60rpx;
  578. font-size: 28rpx;
  579. color: #999;
  580. }
  581. </style>