FilterTime.vue 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. <script setup lang="ts">
  2. import dayjs from 'dayjs'
  3. import { reactive, ref, watch } from 'vue'
  4. // 定义组件属性
  5. const props = defineProps<{
  6. // 初始时间区间,格式为[开始时间, 结束时间]
  7. value?: [string, string]
  8. // 占位符文本
  9. placeholder?: string
  10. // 是否禁用
  11. disabled?: boolean
  12. }>()
  13. // 定义事件
  14. const emit = defineEmits<{
  15. // 选择时间后触发的事件
  16. (e: 'update:value', value: [string, string]): void
  17. // 确定按钮点击事件
  18. (e: 'confirm', value: [string, string]): void
  19. // 重置按钮点击事件
  20. (e: 'reset'): void
  21. }>()
  22. // 内部状态管理
  23. const state = reactive({
  24. // 时间选择器弹窗是否显示
  25. popupVisible: false,
  26. // 日期选择器弹窗是否显示
  27. datePopupVisible: false,
  28. // 当前正在选择的时间类型(start或end)
  29. currentDateType: 'start' as 'start' | 'end',
  30. // 开始时间
  31. startTime: '',
  32. // 结束时间
  33. endTime: '',
  34. // 格式化后的开始时间显示
  35. startText: '开始时间',
  36. // 格式化后的结束时间显示
  37. endText: '结束时间',
  38. // 选择器显示文本
  39. displayText: props.placeholder || '选择时间区间',
  40. })
  41. // 日期选择器配置
  42. const datePickerConfig = reactive({
  43. // 日期格式
  44. format: 'yyyy-MM-dd',
  45. // 当前选择的日期(时间戳)
  46. value: new Date().getTime(),
  47. // 最小日期(可选,时间戳)
  48. minDate: 0,
  49. // 最大日期(可选,时间戳)
  50. maxDate: 0,
  51. })
  52. // 监听传入的value变化,更新内部状态
  53. watch(() => props.value, (newValue) => {
  54. if (newValue && Array.isArray(newValue) && newValue.length === 2) {
  55. state.startTime = newValue[0]
  56. state.endTime = newValue[1]
  57. updateDisplayText()
  58. }
  59. }, { immediate: true })
  60. // 打开时间选择器弹窗
  61. function openPopup() {
  62. if (props.disabled) {
  63. return
  64. }
  65. state.popupVisible = true
  66. }
  67. // 关闭时间选择器弹窗
  68. function closePopup() {
  69. state.popupVisible = false
  70. }
  71. // 打开日期选择器
  72. function openDatePicker(type: 'start' | 'end') {
  73. state.currentDateType = type
  74. // 设置当前选择的日期
  75. if (type === 'start') {
  76. // 确保日期格式正确,无论是字符串还是时间戳
  77. datePickerConfig.value = state.startTime ? new Date(state.startTime).getTime() : new Date().getTime()
  78. // 结束时间之后的日期不可选
  79. datePickerConfig.maxDate = state.endTime ? new Date(state.endTime).getTime() : 0
  80. }
  81. else {
  82. // 确保日期格式正确,无论是字符串还是时间戳
  83. datePickerConfig.value = state.endTime ? new Date(state.endTime).getTime() : new Date().getTime()
  84. // 开始时间之前的日期不可选
  85. datePickerConfig.minDate = state.startTime ? new Date(state.startTime).getTime() : 0
  86. }
  87. state.datePopupVisible = true
  88. }
  89. // 关闭日期选择器
  90. function closeDatePicker() {
  91. state.datePopupVisible = false
  92. }
  93. // 选择日期确认
  94. function confirmDate() {
  95. // 将时间戳转换为 YYYY-MM-DD 格式
  96. const selectedDate = dayjs(datePickerConfig.value).format('YYYY-MM-DD')
  97. if (state.currentDateType === 'start') {
  98. state.startTime = selectedDate
  99. state.startText = selectedDate
  100. // 如果结束时间早于开始时间,自动调整结束时间
  101. if (state.endTime && dayjs(state.endTime).isBefore(dayjs(selectedDate))) {
  102. state.endTime = ''
  103. state.endText = '结束时间'
  104. }
  105. }
  106. else {
  107. state.endTime = selectedDate
  108. state.endText = selectedDate
  109. // 如果开始时间晚于结束时间,自动调整开始时间
  110. if (state.startTime && dayjs(state.startTime).isAfter(dayjs(selectedDate))) {
  111. state.startTime = ''
  112. state.startText = '开始时间'
  113. }
  114. }
  115. closeDatePicker()
  116. updateDisplayText()
  117. }
  118. // 更新显示文本
  119. function updateDisplayText() {
  120. if (state.startTime && state.endTime) {
  121. state.displayText = `${state.startTime} 至 ${state.endTime}`
  122. }
  123. else {
  124. state.displayText = props.placeholder || '选择时间区间'
  125. }
  126. }
  127. // 确认选择
  128. function confirmSelection() {
  129. if (!state.startTime || !state.endTime) {
  130. uni.showToast({
  131. title: '请选择完整的时间区间',
  132. icon: 'none',
  133. })
  134. return
  135. }
  136. const result: [string, string] = [state.startTime, state.endTime]
  137. emit('update:value', result)
  138. emit('confirm', result)
  139. closePopup()
  140. }
  141. // 重置选择
  142. // function resetSelection() {
  143. // state.startTime = ''
  144. // state.endTime = ''
  145. // state.startText = '开始时间'
  146. // state.endText = '结束时间'
  147. // state.displayText = props.placeholder || '选择时间区间'
  148. // emit('update:value', ['', ''])
  149. // emit('reset')
  150. // }
  151. // 暴露方法给父组件
  152. defineExpose({
  153. openPopup
  154. })
  155. </script>
  156. <template>
  157. <view class="filter-time-component">
  158. <!-- 时间选择器触发按钮 -->
  159. <!-- <view
  160. class="filter-time-trigger"
  161. :class="{ disabled }"
  162. @click="openPopup"
  163. >
  164. <text>{{ state.displayText }}</text>
  165. <text class="iconfont icon-down" />
  166. </view> -->
  167. <!-- 时间选择器弹窗(顶部弹出) -->
  168. <up-popup
  169. v-model:show="state.popupVisible"
  170. mode="top"
  171. @close="closePopup"
  172. >
  173. <view class="time-picker-container">
  174. <!-- 标题栏 -->
  175. <view class="time-picker-header">
  176. <text class="title">时间筛选</text>
  177. <!-- <text class="reset-btn" @click="resetSelection">重置</text> -->
  178. </view>
  179. <!-- 时间选择区域 -->
  180. <view class="time-picker-body">
  181. <view class="time-item">
  182. <text class="label">开始时间</text>
  183. <view class="time-selector" @click="openDatePicker('start')">
  184. <text class="time-text">{{ state.startText }}</text>
  185. <text class="iconfont icon-down" />
  186. </view>
  187. </view>
  188. <view class="time-item">
  189. <text class="label">结束时间</text>
  190. <view class="time-selector" @click="openDatePicker('end')">
  191. <text class="time-text">{{ state.endText }}</text>
  192. <text class="iconfont icon-down" />
  193. </view>
  194. </view>
  195. </view>
  196. <!-- 操作按钮 -->
  197. <view class="time-picker-footer">
  198. <view class="btn cancel-btn" @click="closePopup">
  199. 取消
  200. </view>
  201. <view class="btn confirm-btn" @click="confirmSelection">
  202. 确定
  203. </view>
  204. </view>
  205. </view>
  206. </up-popup>
  207. <!-- 日期选择器 -->
  208. <up-datetime-picker
  209. v-model="datePickerConfig.value"
  210. mode="date"
  211. :show="state.datePopupVisible"
  212. @confirm="confirmDate"
  213. @cancel="closeDatePicker"
  214. />
  215. </view>
  216. </template>
  217. <style scoped>
  218. .filter-time-component {
  219. position: relative;
  220. display: inline-block;
  221. }
  222. .filter-time-trigger {
  223. display: flex;
  224. align-items: center;
  225. justify-content: center;
  226. height: 80rpx;
  227. padding: 0 30rpx;
  228. background-color: #f5f5f5;
  229. border-radius: 10rpx;
  230. font-size: 28rpx;
  231. color: #333333;
  232. cursor: pointer;
  233. }
  234. .filter-time-trigger.disabled {
  235. opacity: 0.5;
  236. pointer-events: none;
  237. }
  238. .iconfont {
  239. margin-left: 10rpx;
  240. font-size: 24rpx;
  241. color: #999999;
  242. }
  243. /* 时间选择器弹窗样式 */
  244. .time-picker-container {
  245. background-color: #ffffff;
  246. border-radius: 0 0 20rpx 20rpx;
  247. padding-bottom: 30rpx;
  248. }
  249. .time-picker-header {
  250. display: flex;
  251. align-items: center;
  252. justify-content: space-between;
  253. padding: 30rpx;
  254. border-bottom: 1rpx solid #eeeeee;
  255. }
  256. .time-picker-header .title {
  257. font-size: 32rpx;
  258. font-weight: 500;
  259. color: #333333;
  260. }
  261. .time-picker-header .reset-btn {
  262. font-size: 28rpx;
  263. color: #64a6ff;
  264. cursor: pointer;
  265. }
  266. .time-picker-body {
  267. padding: 30rpx;
  268. }
  269. .time-item {
  270. display: flex;
  271. align-items: center;
  272. justify-content: space-between;
  273. margin-bottom: 30rpx;
  274. }
  275. .time-item:last-child {
  276. margin-bottom: 0;
  277. }
  278. .time-item .label {
  279. font-size: 30rpx;
  280. color: #333333;
  281. }
  282. .time-selector {
  283. display: flex;
  284. align-items: center;
  285. justify-content: flex-end;
  286. padding: 20rpx 30rpx;
  287. background-color: #f5f5f5;
  288. border-radius: 10rpx;
  289. cursor: pointer;
  290. }
  291. .time-selector .time-text {
  292. font-size: 28rpx;
  293. color: #666666;
  294. margin-right: 10rpx;
  295. }
  296. .time-picker-footer {
  297. display: flex;
  298. align-items: center;
  299. justify-content: space-between;
  300. padding: 0 30rpx;
  301. }
  302. .btn {
  303. flex: 1;
  304. height: 80rpx;
  305. line-height: 80rpx;
  306. text-align: center;
  307. border-radius: 10rpx;
  308. font-size: 30rpx;
  309. font-weight: 500;
  310. cursor: pointer;
  311. }
  312. .btn.cancel-btn {
  313. background-color: #f5f5f5;
  314. color: #666666;
  315. margin-right: 20rpx;
  316. }
  317. .btn.confirm-btn {
  318. background-color: #64a6ff;
  319. color: #ffffff;
  320. margin-left: 20rpx;
  321. }
  322. /* 日期选择器弹窗样式 */
  323. .date-picker-container {
  324. background-color: #ffffff;
  325. border-radius: 20rpx 20rpx 0 0;
  326. }
  327. .date-picker-header {
  328. display: flex;
  329. align-items: center;
  330. justify-content: space-between;
  331. padding: 30rpx;
  332. border-bottom: 1rpx solid #eeeeee;
  333. }
  334. .date-picker-header .title {
  335. font-size: 32rpx;
  336. font-weight: 500;
  337. color: #333333;
  338. }
  339. .date-picker-body {
  340. padding: 20rpx 0;
  341. }
  342. </style>