完善预约详情只读与修改能力
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
<template>
|
||||
<MobileShell title="新建预约">
|
||||
<div v-if="draftSavedAtText" class="card draft-tip">
|
||||
<MobileShell :title="pageTitle">
|
||||
<div v-if="!isEditMode && draftSavedAtText" class="card draft-tip">
|
||||
<span>{{ draftSavedAtText }}</span>
|
||||
<button class="text-btn" type="button" @click="clearDraft">清空</button>
|
||||
</div>
|
||||
@@ -53,7 +53,7 @@
|
||||
</div>
|
||||
</div>
|
||||
<div class="field"><label class="required">预约日期</label><DatePickerField v-model="form.appointmentDate" placeholder="请选择日期" title="日期选择" @confirm="onDateChange" /></div>
|
||||
<div class="field"><label class="required">预约作品</label><button class="field-value picker-value" type="button" @click="productModalVisible = true">{{ selectedProductTitle }}</button></div>
|
||||
<div class="field"><label class="required">预约作品</label><button class="field-value picker-value" type="button" @click="updateProductModalVisible(true)">{{ selectedProductTitle }}</button></div>
|
||||
<div class="field">
|
||||
<label class="required">预计时长</label>
|
||||
<div class="duration-control">
|
||||
@@ -70,8 +70,8 @@
|
||||
|
||||
<div class="fixed-bottom">
|
||||
<div class="fixed-bottom__inner button-row">
|
||||
<button class="secondary-btn" type="button" @click="saveDraft">保存草稿</button>
|
||||
<button class="primary-btn" type="button" :disabled="loading" @click="submit">{{ loading ? '提交中...' : '提交预约' }}</button>
|
||||
<button v-if="!isEditMode" class="secondary-btn" type="button" @click="saveDraft">保存草稿</button>
|
||||
<button class="primary-btn" type="button" :disabled="loading" @click="submit">{{ submitButtonText }}</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -235,7 +235,7 @@ import { managerPlanningApi } from '@/api/managerPlanning'
|
||||
import { managerProductsApi, type ProductCreateRequest } from '@/api/managerProducts'
|
||||
import { managerTagsApi, type ManagerTag } from '@/api/managerTags'
|
||||
import { fileApi } from '@/api/file'
|
||||
import type { AppointmentForm, Artist, Product } from '@/types/manager'
|
||||
import type { Appointment, AppointmentForm, Artist, PlanningCalendar, Product } from '@/types/manager'
|
||||
import type { UserInfo } from '@/types/common'
|
||||
import { useSessionStore } from '@/stores/session'
|
||||
import { useToastStore } from '@/stores/toast'
|
||||
@@ -336,10 +336,11 @@ const route = useRoute()
|
||||
const router = useRouter()
|
||||
const session = useSessionStore()
|
||||
const toast = useToastStore()
|
||||
const baseForm = readDraftForm(session.userInfo)
|
||||
const modalStateDraft = readFormModalDraft<AppointmentModalState>(session.userInfo, APPOINTMENT_MODAL_STATE_DRAFT_NAME)
|
||||
const createCustomerDraft = readFormModalDraft<CustomerCreateForm>(session.userInfo, CUSTOMER_CREATE_DRAFT_NAME)
|
||||
const createProductDraft = readFormModalDraft<ProductCreateForm>(session.userInfo, PRODUCT_CREATE_DRAFT_NAME)
|
||||
const initialEditMode = Boolean(route.params.id)
|
||||
const baseForm = initialEditMode ? null : readDraftForm(session.userInfo)
|
||||
const modalStateDraft = initialEditMode ? null : readFormModalDraft<AppointmentModalState>(session.userInfo, APPOINTMENT_MODAL_STATE_DRAFT_NAME)
|
||||
const createCustomerDraft = initialEditMode ? null : readFormModalDraft<CustomerCreateForm>(session.userInfo, CUSTOMER_CREATE_DRAFT_NAME)
|
||||
const createProductDraft = initialEditMode ? null : readFormModalDraft<ProductCreateForm>(session.userInfo, PRODUCT_CREATE_DRAFT_NAME)
|
||||
const form = reactive<AppointmentForm>({
|
||||
customerUserId: String(route.query.customerUserId || baseForm?.customerUserId || ''),
|
||||
customerName: String(route.query.customerName || baseForm?.customerName || ''),
|
||||
@@ -391,6 +392,15 @@ const conflictLevel = ref<'free' | 'danger'>('free')
|
||||
const loading = ref(false)
|
||||
const creatingProduct = ref(false)
|
||||
const productImageUploading = ref(false)
|
||||
const isEditMode = computed(() => Boolean(route.params.id))
|
||||
const editAppointmentId = computed(() => String(route.params.id || ''))
|
||||
const pageTitle = computed(() => (isEditMode.value ? '修改预约' : '新建预约'))
|
||||
const submitButtonText = computed(() => {
|
||||
if (loading.value) {
|
||||
return isEditMode.value ? '保存中...' : '提交中...'
|
||||
}
|
||||
return isEditMode.value ? '保存修改' : '提交预约'
|
||||
})
|
||||
const selectedCustomerReady = computed(() => {
|
||||
return Boolean(form.customerUserId)
|
||||
})
|
||||
@@ -450,6 +460,9 @@ const appointmentFormChanged = computed(() => {
|
||||
})
|
||||
|
||||
const syncAppointmentDraft = (): void => {
|
||||
if (isEditMode.value) {
|
||||
return
|
||||
}
|
||||
if (!appointmentFormChanged.value) {
|
||||
return
|
||||
}
|
||||
@@ -460,6 +473,9 @@ const syncAppointmentDraft = (): void => {
|
||||
}
|
||||
|
||||
const syncAppointmentModalState = (): void => {
|
||||
if (isEditMode.value) {
|
||||
return
|
||||
}
|
||||
if (!productModalVisible.value && !timeSheetVisible.value) {
|
||||
removeFormModalDraft(session.userInfo, APPOINTMENT_MODAL_STATE_DRAFT_NAME)
|
||||
return
|
||||
@@ -495,6 +511,9 @@ const resetCreateCustomerForm = (): void => {
|
||||
}
|
||||
|
||||
const syncCreateCustomerDraft = (): void => {
|
||||
if (isEditMode.value) {
|
||||
return
|
||||
}
|
||||
if (!createCustomerModalVisible.value) {
|
||||
removeFormModalDraft(session.userInfo, CUSTOMER_CREATE_DRAFT_NAME)
|
||||
return
|
||||
@@ -508,7 +527,9 @@ const syncCreateCustomerDraft = (): void => {
|
||||
const updateCreateCustomerModalVisible = (value: boolean): void => {
|
||||
createCustomerModalVisible.value = value
|
||||
if (!value) {
|
||||
removeFormModalDraft(session.userInfo, CUSTOMER_CREATE_DRAFT_NAME)
|
||||
if (!isEditMode.value) {
|
||||
removeFormModalDraft(session.userInfo, CUSTOMER_CREATE_DRAFT_NAME)
|
||||
}
|
||||
resetCreateCustomerForm()
|
||||
}
|
||||
}
|
||||
@@ -545,6 +566,9 @@ const resetProductCreateForm = (): void => {
|
||||
}
|
||||
|
||||
const syncProductCreateDraft = (): void => {
|
||||
if (isEditMode.value) {
|
||||
return
|
||||
}
|
||||
if (!productCreateModalVisible.value) {
|
||||
removeFormModalDraft(session.userInfo, PRODUCT_CREATE_DRAFT_NAME)
|
||||
return
|
||||
@@ -558,7 +582,9 @@ const syncProductCreateDraft = (): void => {
|
||||
const updateProductCreateModalVisible = (value: boolean): void => {
|
||||
productCreateModalVisible.value = value
|
||||
if (!value) {
|
||||
removeFormModalDraft(session.userInfo, PRODUCT_CREATE_DRAFT_NAME)
|
||||
if (!isEditMode.value) {
|
||||
removeFormModalDraft(session.userInfo, PRODUCT_CREATE_DRAFT_NAME)
|
||||
}
|
||||
resetProductCreateForm()
|
||||
}
|
||||
}
|
||||
@@ -643,6 +669,58 @@ const loadOptions = async (): Promise<void> => {
|
||||
}
|
||||
}
|
||||
|
||||
const resolveAppointmentDateValue = (appointment: Appointment): string => {
|
||||
return appointment.date || String(appointment.startTime || '').slice(0, 10) || formatDate(new Date())
|
||||
}
|
||||
|
||||
const resolveAppointmentTimeValue = (appointment: Appointment): string => {
|
||||
return String(appointment.startTime || '').slice(11, 16)
|
||||
}
|
||||
|
||||
const applyAppointmentToForm = (appointment: Appointment): void => {
|
||||
const customerId = appointment.customerId ?? appointment.customerUserId ?? ''
|
||||
form.customerUserId = String(customerId || '')
|
||||
form.customerName = appointment.customerName || ''
|
||||
form.phone = appointment.phone || appointment.customerPhone || ''
|
||||
form.appointmentDate = resolveAppointmentDateValue(appointment)
|
||||
form.appointmentTime = resolveAppointmentTimeValue(appointment)
|
||||
form.productId = String(appointment.productId || '')
|
||||
form.manualProductTitle = appointment.productTitle || ''
|
||||
form.manualProductPrice = ''
|
||||
form.manualProductDescription = ''
|
||||
form.manualProductTags = ''
|
||||
form.manualProductTargetAudience = ''
|
||||
form.manualProductPrecautions = ''
|
||||
form.manualProductImageFileIds = ''
|
||||
form.manualProductImageUrl = ''
|
||||
form.durationMinutes = String(appointment.durationMinutes || '')
|
||||
form.artistId = normalizeArtistSelectionId(String(appointment.artistId || ''), artists.value)
|
||||
form.remark = appointment.remark || ''
|
||||
selectedProduct.value = products.value.find(item => String(item.id) === String(form.productId)) || null
|
||||
customerSearchText.value = [form.customerName, form.phone].filter(Boolean).join(' ')
|
||||
conflictTip.value = ''
|
||||
}
|
||||
|
||||
const loadAppointmentForEdit = async (): Promise<void> => {
|
||||
const appointment = await managerPlanningApi.appointment(editAppointmentId.value)
|
||||
if (appointment.canEdit === false || appointment.status === 2 || appointment.status === 3) {
|
||||
toast.error('已完成或已取消的订单只可查看')
|
||||
await router.replace(`/appointments/${editAppointmentId.value}`)
|
||||
return
|
||||
}
|
||||
applyAppointmentToForm(appointment)
|
||||
}
|
||||
|
||||
const withoutCurrentAppointment = (calendar: PlanningCalendar): PlanningCalendar => {
|
||||
if (!isEditMode.value) {
|
||||
return calendar
|
||||
}
|
||||
return {
|
||||
...calendar,
|
||||
appointments: (calendar.appointments || []).filter(item => String(item.orderItemId) !== editAppointmentId.value)
|
||||
}
|
||||
}
|
||||
|
||||
const scheduleCustomerSearch = (): void => {
|
||||
if (customerTimer.value !== null) {
|
||||
window.clearTimeout(customerTimer.value)
|
||||
@@ -842,7 +920,7 @@ const refreshAvailableTimes = async (): Promise<void> => {
|
||||
}
|
||||
timeLoading.value = true
|
||||
try {
|
||||
const calendar = await managerPlanningApi.calendar({ startDate: form.appointmentDate, endDate: form.appointmentDate, artistId: form.artistId })
|
||||
const calendar = withoutCurrentAppointment(await managerPlanningApi.calendar({ startDate: form.appointmentDate, endDate: form.appointmentDate, artistId: form.artistId }))
|
||||
availableTimeGroups.value = buildAvailableTimeGroups(calendar, form)
|
||||
timeSelectTip.value = availableTimeGroups.value.length > 0 ? '请选择预约时间' : '当前美甲师当天暂无可约时间'
|
||||
} finally {
|
||||
@@ -872,7 +950,7 @@ const checkAvailability = async (): Promise<{ ok: boolean; tip?: string }> => {
|
||||
}
|
||||
const startMinute = minutesOfDay(form.appointmentTime)
|
||||
const endMinute = startMinute + normalizeDuration(form.durationMinutes)
|
||||
const calendar = await managerPlanningApi.calendar({ startDate: form.appointmentDate, endDate: form.appointmentDate, artistId: form.artistId })
|
||||
const calendar = withoutCurrentAppointment(await managerPlanningApi.calendar({ startDate: form.appointmentDate, endDate: form.appointmentDate, artistId: form.artistId }))
|
||||
const daySummary = (calendar.daySummaries || []).find(item => item.date === form.appointmentDate)
|
||||
const businessStartTime = String(daySummary?.businessStartTime || calendar.businessStartTime || '10:00').slice(0, 5)
|
||||
const businessEndTime = String(daySummary?.businessEndTime || calendar.businessEndTime || '22:00').slice(0, 5)
|
||||
@@ -921,16 +999,20 @@ const submit = async (): Promise<void> => {
|
||||
artistId: form.artistId,
|
||||
durationMinutes,
|
||||
manualProductPrice: hasManualProduct ? Number(form.manualProductPrice) : undefined,
|
||||
clientRequestId: buildClientRequestId('manager-appointment')
|
||||
clientRequestId: buildClientRequestId(isEditMode.value ? 'manager-appointment-update' : 'manager-appointment')
|
||||
}
|
||||
const result = isEditMode.value
|
||||
? await managerPlanningApi.updateAppointment(editAppointmentId.value, payload)
|
||||
: await managerPlanningApi.createAppointment(payload)
|
||||
if (!isEditMode.value) {
|
||||
removeDraftForm(session.userInfo)
|
||||
removeFormModalDraft(session.userInfo, APPOINTMENT_MODAL_STATE_DRAFT_NAME)
|
||||
}
|
||||
const result = await managerPlanningApi.createAppointment(payload)
|
||||
removeDraftForm(session.userInfo)
|
||||
removeFormModalDraft(session.userInfo, APPOINTMENT_MODAL_STATE_DRAFT_NAME)
|
||||
markPlanningRefresh({
|
||||
date: result.date || form.appointmentDate,
|
||||
artistId: String(result.artistId ?? form.artistId)
|
||||
})
|
||||
toast.success('预约已创建')
|
||||
toast.success(isEditMode.value ? '预约已修改' : '预约已创建')
|
||||
await router.replace(`/appointments/${result.orderItemId}`)
|
||||
} finally {
|
||||
loading.value = false
|
||||
@@ -938,12 +1020,18 @@ const submit = async (): Promise<void> => {
|
||||
}
|
||||
|
||||
const saveDraft = (): void => {
|
||||
if (isEditMode.value) {
|
||||
return
|
||||
}
|
||||
writeDraftForm(session.userInfo, { ...form })
|
||||
draftSavedAtText.value = '草稿已保存到本机'
|
||||
toast.success('草稿已保存')
|
||||
}
|
||||
|
||||
const clearDraft = (): void => {
|
||||
if (isEditMode.value) {
|
||||
return
|
||||
}
|
||||
removeDraftForm(session.userInfo)
|
||||
removeFormModalDraft(session.userInfo, APPOINTMENT_MODAL_STATE_DRAFT_NAME)
|
||||
draftSavedAtText.value = ''
|
||||
@@ -951,13 +1039,27 @@ const clearDraft = (): void => {
|
||||
}
|
||||
|
||||
onMounted(async () => {
|
||||
await loadOptions()
|
||||
if (form.customerUserId) {
|
||||
const customer = await managerCustomersApi.detail(form.customerUserId)
|
||||
selectCustomer(customer)
|
||||
loading.value = true
|
||||
try {
|
||||
await loadOptions()
|
||||
if (isEditMode.value) {
|
||||
await loadAppointmentForEdit()
|
||||
}
|
||||
if (form.customerUserId) {
|
||||
const customer = await managerCustomersApi.detail(form.customerUserId)
|
||||
selectCustomer(customer)
|
||||
}
|
||||
await searchCustomers()
|
||||
await refreshAvailableTimes()
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : '预约信息加载失败'
|
||||
toast.error(message)
|
||||
if (isEditMode.value) {
|
||||
await router.replace(`/appointments/${editAppointmentId.value}`)
|
||||
}
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
await searchCustomers()
|
||||
await refreshAvailableTimes()
|
||||
})
|
||||
|
||||
watch(form, syncAppointmentDraft, { deep: true })
|
||||
|
||||
Reference in New Issue
Block a user