修复:时间输入改为选择器

This commit is contained in:
xiaowang
2026-03-21 13:16:39 +08:00
parent 4d5ea2996b
commit 3b8965a168
3 changed files with 179 additions and 26 deletions
+60 -24
View File
@@ -111,7 +111,18 @@
<div class="modal-section">
<div class="section-title">预约时间</div>
<input v-model="draftAppointmentTime" class="time-input" type="datetime-local" :min="minAppointmentTime" step="1800" />
<a-date-picker
v-model:value="draftAppointmentTime"
class="time-picker"
style="width: 100%"
format="YYYY-MM-DD HH:mm:ss"
value-format="YYYY-MM-DD HH:mm:ss"
input-read-only
:disabled-date="disablePastAppointmentDate"
:disabled-time="disablePastAppointmentTime"
:show-time="{ minuteStep: 30, secondStep: 60, hideDisabledOptions: true }"
placeholder="请选择预约时间"
/>
<div class="helper">{{ bookingHelperText }}</div>
</div>
</a-modal>
@@ -187,25 +198,22 @@ const bookingHelperText = computed(() => {
const texts: string[] = []
if (currentShopHours.value) texts.push('门店营业时间:' + currentShopHours.value)
if (currentItem.value?.approximateDuration) texts.push('预计时长:' + currentItem.value.approximateDuration)
return texts.join(' · ') || '请选择晚于当前时间的预约时间'
})
const minAppointmentTime = computed(() => {
const d = new Date()
d.setSeconds(0, 0)
d.setMinutes(d.getMinutes() + 5)
return [
d.getFullYear(),
'-',
String(d.getMonth() + 1).padStart(2, '0'),
'-',
String(d.getDate()).padStart(2, '0'),
'T',
String(d.getHours()).padStart(2, '0'),
':',
String(d.getMinutes()).padStart(2, '0')
].join('')
return texts.join(' · ') || '请选择至少晚于当前 5 分钟的预约时间'
})
const getMinAppointmentDate = () => {
const date = new Date()
date.setSeconds(0, 0)
date.setMinutes(date.getMinutes() + 5)
return date
}
const getDayBoundary = (date: Date) => {
const boundary = new Date(date)
boundary.setHours(0, 0, 0, 0)
return boundary.getTime()
}
const getCouponKey = (coupon: UserCoupon) => `${coupon.sourceType || 'USER_COUPON'}:${coupon.id}`
const calculateCouponDiscount = (coupon: UserCoupon | null, amount: number) => {
@@ -356,7 +364,7 @@ const removeItem = async (item: CartItem) => {
const openBookingModal = async (item: CartItem) => {
currentItem.value = item
draftArtistId.value = item.artistId
draftAppointmentTime.value = item.appointmentTime ? item.appointmentTime.replace(' ', 'T').slice(0, 16) : ''
draftAppointmentTime.value = item.appointmentTime || ''
showBookingModal.value = true
await Promise.all([loadArtists(item), loadShopHours(item.shopId)])
}
@@ -390,7 +398,36 @@ const loadShopHours = async (shopId?: number) => {
}
const formatAppointmentDisplay = (value?: string) => value ? value.replace('T', ' ').slice(0, 16) : '未选择预约时间'
const isPastAppointment = (value?: string) => !!value && new Date(value.replace(' ', 'T')).getTime() <= Date.now()
const isPastAppointment = (value?: string) => !!value && new Date(value.replace(' ', 'T')).getTime() < getMinAppointmentDate().getTime()
const disablePastAppointmentDate = (current: any) => {
if (!current) {
return false
}
return current.valueOf() < getDayBoundary(new Date())
}
const disablePastAppointmentTime = (current: any) => {
if (!current || typeof current.format !== 'function') {
return {}
}
const minDate = getMinAppointmentDate()
const minDateText = [
minDate.getFullYear(),
String(minDate.getMonth() + 1).padStart(2, '0'),
String(minDate.getDate()).padStart(2, '0')
].join('-')
if (current.format('YYYY-MM-DD') !== minDateText) {
return {}
}
const minHour = minDate.getHours()
const minMinute = minDate.getMinutes()
return {
disabledHours: () => Array.from({ length: minHour }, (_, index) => index),
disabledMinutes: (selectedHour: number) => selectedHour === minHour
? Array.from({ length: minMinute }, (_, index) => index)
: [],
disabledSeconds: () => []
}
}
const closeBookingModal = () => {
showBookingModal.value = false
@@ -405,11 +442,10 @@ const confirmBooking = () => {
if (!currentItem.value) return closeBookingModal()
if (!draftArtistId.value) return message.warning('请先选择美甲师')
if (!draftAppointmentTime.value) return message.warning('请选择预约时间')
const normalizedTime = draftAppointmentTime.value.replace('T', ' ') + ':00'
if (isPastAppointment(normalizedTime)) return message.warning('预约时间必须晚于当前时间')
if (isPastAppointment(draftAppointmentTime.value)) return message.warning('预约时间至少需要晚于当前 5 分钟')
currentItem.value.artistId = draftArtistId.value
currentItem.value.artist = availableArtists.value.find(artist => artist.id === draftArtistId.value)
currentItem.value.appointmentTime = normalizedTime
currentItem.value.appointmentTime = draftAppointmentTime.value
closeBookingModal()
}
@@ -502,5 +538,5 @@ onMounted(async () => {
.artist-option { display: inline-flex; align-items: center; gap: 8px; padding: 8px 12px; border: 1px solid #d9d9d9; border-radius: 999px; background: #fff; cursor: pointer; }
.artist-option.active { border-color: #1890ff; color: #1890ff; background: #f0f9ff; }
.artist-option img { width: 24px; height: 24px; border-radius: 50%; object-fit: cover; }
.time-input { width: 100%; padding: 10px 12px; border: 1px solid #d9d9d9; border-radius: 8px; }
.time-picker { border-radius: 8px; }
</style>
+71 -2
View File
@@ -116,7 +116,29 @@
<a-input v-model:value="formState.address" placeholder="请输入店铺地址" />
</a-form-item>
<a-form-item label="营业时间" name="businessHours">
<a-input v-model:value="formState.businessHours" placeholder="例如:09:00-21:00" />
<div class="business-hours-picker">
<a-date-picker
v-model:value="businessStartTime"
picker="time"
value-format="HH:mm"
format="HH:mm"
placeholder="开始时间"
input-read-only
style="width: calc(50% - 14px)"
@change="handleBusinessHoursChange"
/>
<span class="business-hours-separator">至</span>
<a-date-picker
v-model:value="businessEndTime"
picker="time"
value-format="HH:mm"
format="HH:mm"
placeholder="结束时间"
input-read-only
style="width: calc(50% - 14px)"
@change="handleBusinessHoursChange"
/>
</div>
</a-form-item>
<a-form-item label="Banner图片">
<div class="upload-tip">首页轮播图,建议尺寸 750x300 像素</div>
@@ -267,6 +289,8 @@ const viewImageUrls = ref<string[]>([])
const viewBannerUrls = ref<string[]>([])
const previewVisible = ref(false)
const previewImage = ref('')
const businessStartTime = ref('')
const businessEndTime = ref('')
// 查看店铺相关状态
const viewFormState = reactive({
@@ -300,7 +324,18 @@ const formRules: any = {
shopName: [{ required: true, message: '请输入店铺名称', trigger: 'blur' }],
description: [{ required: true, message: '请输入店铺描述', trigger: 'blur' }],
address: [{ required: true, message: '请输入店铺地址', trigger: 'blur' }],
businessHours: [{ required: true, message: '请输入营业时间', trigger: 'blur' }]
businessHours: [{
validator: async () => {
if (!businessStartTime.value || !businessEndTime.value) {
return Promise.reject('请选择营业开始和结束时间')
}
if (businessStartTime.value >= businessEndTime.value) {
return Promise.reject('营业结束时间必须晚于开始时间')
}
return Promise.resolve()
},
trigger: 'change'
}]
}
const columns = [
@@ -540,6 +575,24 @@ const handleRemove = (file: any) => {
}
}
const syncBusinessHoursValue = () => {
formState.businessHours = businessStartTime.value && businessEndTime.value
? `${businessStartTime.value}-${businessEndTime.value}`
: ''
}
const setBusinessHoursValue = (value?: string) => {
const matched = (value || '').match(/(\d{2}:\d{2})\s*-\s*(\d{2}:\d{2})/)
businessStartTime.value = matched?.[1] || ''
businessEndTime.value = matched?.[2] || ''
syncBusinessHoursValue()
}
const handleBusinessHoursChange = async () => {
syncBusinessHoursValue()
await formRef.value?.validateFields?.(['businessHours']).catch(() => undefined)
}
const showAddModal = () => {
isEdit.value = false
Object.assign(formState, {
@@ -550,6 +603,7 @@ const showAddModal = () => {
coverImageFileId: '',
imageFileIds: ''
})
setBusinessHoursValue('')
bannerFileList.value = []
uploadedBannerIds.value = []
imageFileList.value = []
@@ -568,6 +622,7 @@ const showEditModal = async (record: Shop | Record<string, any>) => {
coverImageFileId: 'coverImageFileId' in record ? record.coverImageFileId : (record as any).coverImageFileId,
imageFileIds: 'imageFileIds' in record ? record.imageFileIds : (record as any).imageFileIds
})
setBusinessHoursValue(formState.businessHours)
// 加载Banner图片
const coverImageFileId = 'coverImageFileId' in record ? record.coverImageFileId : (record as any).coverImageFileId
@@ -715,6 +770,7 @@ const handleViewModalCancel = () => {
const handleModalOk = async () => {
try {
syncBusinessHoursValue()
await formRef.value?.validate()
modalLoading.value = true
@@ -738,6 +794,7 @@ const handleModalOk = async () => {
const handleModalCancel = () => {
modalVisible.value = false
formRef.value?.resetFields()
setBusinessHoursValue('')
bannerFileList.value = []
uploadedBannerIds.value = []
imageFileList.value = []
@@ -790,4 +847,16 @@ onMounted(async () => {
font-size: 12px;
margin-bottom: 8px;
}
.business-hours-picker {
display: flex;
align-items: center;
gap: 12px;
width: 100%;
}
.business-hours-separator {
color: #666;
flex: 0 0 auto;
}
</style>
@@ -0,0 +1,48 @@
# 2026-03-21 前端时间输入改为选择器
## 本轮目标
- 将后台“新增/编辑店铺”的营业时间从手输文本改为时间选择器。
- 将购物车中预约时间从原生文本时间输入改为统一的日期时间选择器。
- 保持后端接口字段格式不变,避免联动改动后端。
## 本轮修改
### 1. 店铺营业时间改为开始/结束时间选择器
- 更新 [ShopsView.vue](/E:/code/xiaowang/Frontend/src/views/admin/ShopsView.vue)
- 调整内容:
- 原 `businessHours` 文本框替换为“开始时间 + 结束时间”两个时间选择器
- 选择器使用只读输入模式,避免继续手敲时间文本
- 表单提交前仍回写为后端原有格式:`HH:mm-HH:mm`
- 编辑店铺时会把已有营业时间自动回填到两个选择器
- 新增营业时间校验:必须同时选择开始/结束时间,且结束时间必须晚于开始时间
- 实现上使用 `a-date-picker` 的 `picker="time"` 模式,兼容项目当前的按需样式构建链路
### 2. 购物车预约时间改为日期时间选择器
- 更新 [CartView.vue](/E:/code/xiaowang/Frontend/src/views/CartView.vue)
- 调整内容:
- 原生 `datetime-local` 输入框替换为 `a-date-picker`
- 使用 `value-format="YYYY-MM-DD HH:mm:ss"`,继续兼容后端当前预约时间格式
- 禁止手动输入,统一通过面板选择
- 禁选今天之前的日期
- 同日会禁选当前时间前 5 分钟以内的时段
- 下单前与确认预约时继续保留兜底校验,避免提交过期预约时间
## 范围说明
- [CouponsView.vue](/E:/code/xiaowang/Frontend/src/views/admin/CouponsView.vue) 原本已经使用日期时间选择器,本轮未重复改动。
- [ProductsView.vue](/E:/code/xiaowang/Frontend/src/views/admin/ProductsView.vue) 的 `预计时长` 仍保留分钟数输入。
- 原因:该字段表示“时长”,不是具体时刻或日期时间。
- 如果后续要优化体验,更适合做成“分钟/时长档位选择”,而不是时钟选择器。
## 结果
- 前端当前直接手输时间的两个主要入口已统一改为选择器。
- 店铺营业时间和购物车预约时间的展示、回填、校验、提交格式保持一致。
- 后端接口和数据库字段本轮无需变更。
## 验证
- `Frontend` 执行 `npm run build` 通过