diff --git a/src/components/common/Search.vue b/src/components/common/Search.vue index 3aa9ce2..5c06c1e 100644 --- a/src/components/common/Search.vue +++ b/src/components/common/Search.vue @@ -25,8 +25,8 @@ :type="item.dateType || 'date'" :range-separator="item.rangeSeparator || '至'" :placeholder="item.placeholder" :start-placeholder="item.startPlaceholder || '开始日期'" :end-placeholder="item.endPlaceholder || '结束日期'" - :value-format="item.valueFormat || 'YYYY-MM-DD'" :clearable="item.clearable ?? true" - :disabled-date="item.disabledDate" :locale="'zhCn'" /> + :value-format="item.valueFormat || 'YYYY-MM-DD'" :format="item.format || item.valueFormat || 'YYYY-MM-DD'" :clearable="item.clearable ?? true" + :disabled-date="item.disabledDate" :locale="'zhCn'" @change="(val: any) => handleDateChange(item, val)" /> @@ -73,10 +73,12 @@ interface SearchOption { startPlaceholder?: string // 日期范围开始占位符 endPlaceholder?: string // 日期范围结束占位符 valueFormat?: string // 日期格式 + format?: string // 显示格式 disabledDate?: (date: Date) => boolean // 禁用日期函数 width?: string // 宽度 minWidth?: string // 最小宽度 - auth?: string // 最小宽度 + auth?: string // 权限 + defaultTime?: 'start' | 'end' // 默认时间:start=00:00:00, end=23:59:59 } // 按钮配置类型 @@ -111,6 +113,34 @@ const emit = defineEmits(['search', 'reset', 'blur-input']) const handleBlur = () => { emit('blur-input') } + +// 日期选择变化处理:自动设置开始/结束时间 +const handleDateChange = (item: SearchOption, val: any) => { + if (!val || !item.defaultTime) return + const date = new Date(val) + if (item.defaultTime === 'start') { + date.setHours(0, 0, 0, 0) + } else if (item.defaultTime === 'end') { + date.setHours(23, 59, 59, 999) + } + // 重新格式化日期 + const formatDate = (d: Date, format: string): string => { + const year = d.getFullYear() + const month = String(d.getMonth() + 1).padStart(2, '0') + const day = String(d.getDate()).padStart(2, '0') + const hours = String(d.getHours()).padStart(2, '0') + const minutes = String(d.getMinutes()).padStart(2, '0') + const seconds = String(d.getSeconds()).padStart(2, '0') + return format.replace(/YYYY|MM|DD|HH|mm|ss/g, (match) => { + const map: Record = { + YYYY: year, MM: month, DD: day, HH: hours, mm: minutes, ss: seconds + } + return String(map[match]) + }) + } + const format = item.valueFormat || 'YYYY-MM-DD HH:mm:ss' + searchForm.value[item.prop] = formatDate(date, format) +}