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

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>