select.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <template>
  2. <view class="date-select-page">
  3. <!-- 日历选择组件 -->
  4. <view class="calendar-wrap">
  5. <wd-calendar-view
  6. type="daterange"
  7. v-model="dateRange"
  8. :start-date="new Date('2025-01-01')"
  9. :end-date="new Date()"
  10. placeholder="开始时间 ~ 结束时间"
  11. @change="handleDateChange"
  12. />
  13. </view>
  14. <!-- 确认按钮 -->
  15. <view class="confirm-btn" @click="confirmSelect">
  16. 确认选择
  17. </view>
  18. </view>
  19. </template>
  20. <script setup>
  21. import { ref } from 'vue'
  22. import dayjs from 'dayjs'
  23. // 绑定日历选择的时间范围(数组:[开始时间, 结束时间])
  24. const dateRange = ref([])
  25. // 格式化后的开始/结束时间
  26. const startT = ref('')
  27. const endT = ref('')
  28. // 日历选择变化回调
  29. const handleDateChange = ({ value }) => {
  30. if (Array.isArray(value) && value.length === 2) {
  31. startT.value = dayjs(value[0]).format('YYYY-MM-DD')
  32. endT.value = dayjs(value[1]).format('YYYY-MM-DD')
  33. }
  34. }
  35. // 确认选择并返回
  36. const confirmSelect = () => {
  37. if (!startT.value || !endT.value) {
  38. uni.showToast({ title: '请选择完整的时间范围', icon: 'none' })
  39. return
  40. }
  41. // 方式1:通过uni.setStorageSync传递参数
  42. uni.setStorageSync('selectedDateRange', {
  43. startTime: startT.value,
  44. endTime: endT.value,
  45. showText: `${startT.value} ~ ${endT.value}`
  46. })
  47. // 方式2:通过页面栈传递参数(更推荐)
  48. const pages = getCurrentPages()
  49. const prevPage = pages[pages.length - 2] // 获取上一页实例
  50. prevPage.$vm.selectedDate = {
  51. startTime: startT.value,
  52. endTime: endT.value,
  53. showText: `${startT.value} ~ ${endT.value}`
  54. }
  55. // 返回上一页
  56. uni.navigateBack({ delta: 1 })
  57. }
  58. </script>
  59. <style scoped>
  60. .date-select-page {
  61. min-height: 100vh;
  62. background-color: #f5f5f5;
  63. }
  64. .nav-bar {
  65. height: 44px;
  66. line-height: 44px;
  67. background-color: #fff;
  68. padding: 0 15rpx;
  69. display: flex;
  70. align-items: center;
  71. border-bottom: 1px solid #eee;
  72. }
  73. .back-btn {
  74. color: #007aff;
  75. font-size: 16px;
  76. }
  77. .nav-title {
  78. flex: 1;
  79. text-align: center;
  80. font-size: 18px;
  81. font-weight: 500;
  82. }
  83. .calendar-wrap {
  84. padding: 15rpx;
  85. background-color: #fff;
  86. margin: 10rpx;
  87. border-radius: 10rpx;
  88. }
  89. .confirm-btn {
  90. margin: 20rpx 15rpx;
  91. height: 45px;
  92. line-height: 45px;
  93. background-color: #007aff;
  94. color: #fff;
  95. text-align: center;
  96. border-radius: 8rpx;
  97. font-size: 16px;
  98. }
  99. </style>