TimePopup.vue 16 KB

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