小程序预约详情支持图片预览、编辑预约与完成收款拆分
This commit is contained in:
@@ -236,8 +236,9 @@ function isCancelledAppointment(item) {
|
||||
return item.status === 3 || item.statusText === '已取消'
|
||||
}
|
||||
|
||||
function buildBusyRanges(appointments, form) {
|
||||
function buildBusyRanges(appointments, form, excludeOrderItemId) {
|
||||
return (appointments || [])
|
||||
.filter(item => !excludeOrderItemId || String(item.orderItemId) !== String(excludeOrderItemId))
|
||||
.filter(item => String(item.artistId) === String(form.artistId))
|
||||
.filter(item => normalizeAppointmentDate(item) === form.appointmentDate)
|
||||
.filter(item => !isCancelledAppointment(item))
|
||||
@@ -278,12 +279,12 @@ function resolveBusinessHours(calendar, targetDate) {
|
||||
return { start, end }
|
||||
}
|
||||
|
||||
function buildAvailableTimeGroups(calendar, form, durationMinutes) {
|
||||
function buildAvailableTimeGroups(calendar, form, durationMinutes, excludeOrderItemId) {
|
||||
const schedules = (calendar.schedules || [])
|
||||
.filter(item => String(item.artistId) === String(form.artistId))
|
||||
.filter(item => item.date === form.appointmentDate)
|
||||
.filter(item => item.status === 1 && Number(item.capacity === undefined ? 1 : item.capacity) >= 1)
|
||||
const busyRanges = buildBusyRanges(calendar.appointments || [], form)
|
||||
const busyRanges = buildBusyRanges(calendar.appointments || [], form, excludeOrderItemId)
|
||||
const today = dateUtil.formatDate(new Date())
|
||||
const now = new Date()
|
||||
const currentMinute = now.getHours() * 60 + now.getMinutes()
|
||||
@@ -366,9 +367,9 @@ function buildTimeValues(durationMinutes, businessStart, businessEnd, appointmen
|
||||
return values
|
||||
}
|
||||
|
||||
function buildTimePickerSlots(calendar, form, durationMinutes) {
|
||||
function buildTimePickerSlots(calendar, form, durationMinutes, excludeOrderItemId) {
|
||||
const schedules = calendar.schedules || []
|
||||
const busyRanges = form.artistId ? buildBusyRanges(calendar.appointments || [], form) : []
|
||||
const busyRanges = form.artistId ? buildBusyRanges(calendar.appointments || [], form, excludeOrderItemId) : []
|
||||
const businessHours = resolveBusinessHours(calendar, form.appointmentDate)
|
||||
if (!businessHours.start) return []
|
||||
const businessStart = businessHours.start
|
||||
@@ -648,10 +649,33 @@ Page({
|
||||
appointmentDateBadge: '',
|
||||
appointmentDateLong: '',
|
||||
availableTimeGroups: [],
|
||||
loading: false
|
||||
loading: false,
|
||||
editMode: false
|
||||
},
|
||||
|
||||
onLoad(options = {}) {
|
||||
const editId = options && options.mode === 'edit' ? String(options.id || '') : ''
|
||||
if (editId) {
|
||||
if (!canCreateAppointment()) {
|
||||
wx.showToast({ title: '当前账号仅支持查看预约', icon: 'none' })
|
||||
wx.navigateBack()
|
||||
return
|
||||
}
|
||||
this._editMode = true
|
||||
this._editAppointmentId = editId
|
||||
this._draftReady = false
|
||||
this._draftPersistenceDisabled = true
|
||||
this.setData({
|
||||
editMode: true,
|
||||
canCreateAppointment: canCreateAppointment(),
|
||||
canManageShopSettings: isShopManager(),
|
||||
'form.durationMinutes': '60',
|
||||
draftSavedAtText: ''
|
||||
})
|
||||
wx.setNavigationBarTitle({ title: '编辑预约' })
|
||||
this.loadEditAppointment(editId)
|
||||
return
|
||||
}
|
||||
const manualDateMode = options && options.dateMode === 'manual'
|
||||
const today = dateUtil.formatDate(new Date())
|
||||
if (options && options.date && options.date < today) {
|
||||
@@ -748,6 +772,88 @@ Page({
|
||||
wx.navigateTo({ url: '/pages/shop-settings/index' })
|
||||
},
|
||||
|
||||
async loadEditAppointment(id) {
|
||||
this._editMode = true
|
||||
this._editAppointmentId = String(id || '')
|
||||
this._draftPersistenceDisabled = true
|
||||
this.setData({ loading: true, editMode: true })
|
||||
try {
|
||||
const appointment = await planningApi.appointment(id)
|
||||
const startTime = String(appointment.startTime || '')
|
||||
const appointmentDate = startTime ? startTime.slice(0, 10) : String(appointment.date || '')
|
||||
const appointmentTime = startTime ? startTime.slice(11, 16) : ''
|
||||
const manualProduct = String(appointment.productSourceType || '') === 'MANAGER_APPOINTMENT'
|
||||
const manualImages = String(appointment.productImageFileIds || '')
|
||||
.split(',')
|
||||
.map(fileId => fileId.trim())
|
||||
.filter(Boolean)
|
||||
.slice(0, 9)
|
||||
.map((fileId, index) => ({
|
||||
fileId,
|
||||
fileUrl: index === 0 && appointment.coverImages && appointment.coverImages[0] ? String(appointment.coverImages[0]) : '',
|
||||
previewUrl: index === 0 && appointment.coverImages && appointment.coverImages[0] ? String(appointment.coverImages[0]) : ''
|
||||
}))
|
||||
const form = {
|
||||
...this.data.form,
|
||||
customerUserId: appointment.customerUserId ? String(appointment.customerUserId) : '',
|
||||
customerName: appointment.customerName || '',
|
||||
phone: appointment.customerPhoneAvailable ? String(appointment.customerPhone || '') : '',
|
||||
wechatId: '',
|
||||
appointmentDate,
|
||||
appointmentTime,
|
||||
amount: appointment.amount === null || appointment.amount === undefined ? '' : String(appointment.amount),
|
||||
productId: manualProduct ? '' : (appointment.productId ? String(appointment.productId) : ''),
|
||||
manualProductTitle: manualProduct ? String(appointment.productTitle || '') : '',
|
||||
manualProductPrice: manualProduct ? (appointment.amount === null || appointment.amount === undefined ? '' : String(appointment.amount)) : '',
|
||||
manualProductDescription: manualProduct ? String(appointment.productDescription || '') : '',
|
||||
manualProductTags: manualProduct ? String(appointment.productTags || '') : '',
|
||||
manualProductTargetAudience: manualProduct ? String(appointment.productTargetAudience || '') : '',
|
||||
manualProductPrecautions: manualProduct ? String(appointment.productPrecautions || '') : '',
|
||||
manualProductImageFileIds: manualImages.map(image => image.fileId).join(','),
|
||||
manualProductImageUrl: manualImages[0] ? manualImages[0].previewUrl || manualImages[0].fileUrl : '',
|
||||
manualProductImages: manualImages,
|
||||
durationMinutes: appointment.durationMinutes ? String(appointment.durationMinutes) : '60',
|
||||
artistId: appointment.artistId ? String(appointment.artistId) : '',
|
||||
createCustomerRecord: false,
|
||||
remark: appointment.remark || ''
|
||||
}
|
||||
this._editOriginalSchedule = {
|
||||
appointmentDate,
|
||||
appointmentTime,
|
||||
artistId: form.artistId,
|
||||
durationMinutes: form.durationMinutes
|
||||
}
|
||||
const dateLabels = buildAppointmentDateLabels(appointmentDate)
|
||||
this.setData({
|
||||
form,
|
||||
unifiedStyleDescription: String(appointment.remark || appointment.productDescription || ''),
|
||||
manualProductTitleLength: String(form.manualProductTitle).length,
|
||||
selectedArtist: appointment.artistId
|
||||
? { id: String(appointment.artistId), name: appointment.artistName || '' }
|
||||
: { id: '__unassigned__', name: '暂不分配美甲师' },
|
||||
draftSavedAtText: '',
|
||||
appointmentDayLabel: dateLabels.day,
|
||||
appointmentDateBadge: dateLabels.badge,
|
||||
appointmentDateLong: dateLabels.long,
|
||||
durationManuallyChanged: true,
|
||||
canCreateAppointment: canCreateAppointment()
|
||||
})
|
||||
await this.loadOptions()
|
||||
} catch (error) {
|
||||
this.setData({ loading: false })
|
||||
wx.showToast({ title: toUserMessage(error, '预约加载失败'), icon: 'none' })
|
||||
return
|
||||
}
|
||||
this.setData({ loading: false })
|
||||
},
|
||||
|
||||
isEditOriginalSchedule(date, time) {
|
||||
const origin = this._editOriginalSchedule
|
||||
return Boolean(this._editMode && origin
|
||||
&& String(date || '') === String(origin.appointmentDate || '')
|
||||
&& String(time || '') === String(origin.appointmentTime || ''))
|
||||
},
|
||||
|
||||
async loadOptions() {
|
||||
this.setData({ loadingProducts: true })
|
||||
try {
|
||||
@@ -1099,6 +1205,7 @@ Page({
|
||||
},
|
||||
|
||||
persistDraft(showToast) {
|
||||
if (this._editMode) return
|
||||
try {
|
||||
if (!hasDraftContent(
|
||||
this.data.form,
|
||||
@@ -1595,7 +1702,7 @@ Page({
|
||||
timePickerHours: [],
|
||||
timePickerMinutes: [],
|
||||
selectedTimeCandidate: '',
|
||||
'form.appointmentTime': '',
|
||||
'form.appointmentTime': this.isEditOriginalSchedule(form.appointmentDate, form.appointmentTime) ? form.appointmentTime : '',
|
||||
timeError: message,
|
||||
businessHoursMissing: !closed,
|
||||
timeLoading: false,
|
||||
@@ -1665,7 +1772,7 @@ Page({
|
||||
timePickerHours: [],
|
||||
timePickerMinutes: [],
|
||||
selectedTimeCandidate: '',
|
||||
'form.appointmentTime': '',
|
||||
'form.appointmentTime': this.isEditOriginalSchedule(form.appointmentDate, form.appointmentTime) ? form.appointmentTime : '',
|
||||
timeError: message,
|
||||
businessHoursMissing: !closed,
|
||||
timeLoading: false,
|
||||
@@ -1673,9 +1780,9 @@ Page({
|
||||
})
|
||||
return
|
||||
}
|
||||
const availableTimeGroups = buildAvailableTimeGroups(calendar, form, durationMinutes)
|
||||
const availableTimeGroups = buildAvailableTimeGroups(calendar, form, durationMinutes, this._editAppointmentId)
|
||||
const availableTimes = flattenTimeGroups(availableTimeGroups).map(item => item.value)
|
||||
const timePickerSlots = buildTimePickerSlots(calendar, form, durationMinutes)
|
||||
const timePickerSlots = buildTimePickerSlots(calendar, form, durationMinutes, this._editAppointmentId)
|
||||
const patch = {
|
||||
availableTimeGroups,
|
||||
timePickerSlots,
|
||||
@@ -1685,7 +1792,8 @@ Page({
|
||||
businessHoursMissing: false,
|
||||
timeSelectTip: availableTimes.length > 0 ? '请选择预约时间' : '当前美甲师当天暂无可约时间'
|
||||
}
|
||||
if (form.appointmentTime && !availableTimes.includes(form.appointmentTime)) {
|
||||
if (form.appointmentTime && !availableTimes.includes(form.appointmentTime)
|
||||
&& !this.isEditOriginalSchedule(form.appointmentDate, form.appointmentTime)) {
|
||||
patch['form.appointmentTime'] = ''
|
||||
}
|
||||
this.setData(patch)
|
||||
@@ -1816,21 +1924,28 @@ Page({
|
||||
}
|
||||
const startMinute = dateUtil.minutesOfDay(appointmentTime)
|
||||
const endMinute = startMinute + durationMinutes
|
||||
const editOriginalSchedule = this.isEditOriginalSchedule(form.appointmentDate, appointmentTime)
|
||||
if (!this.data.businessStartTime || !this.data.businessEndTime) {
|
||||
if (editOriginalSchedule) {
|
||||
this.setData({ conflictTip: '沿用当前预约时间', conflictLevel: 'free' })
|
||||
return { ok: true }
|
||||
}
|
||||
const tip = this.data.businessHoursMissing
|
||||
? '门店营业时间尚未设置,请先完成设置'
|
||||
: '营业时间暂不可用,请稍后重试'
|
||||
this.setData({ conflictTip: tip, conflictLevel: 'danger' })
|
||||
return { ok: false, tip }
|
||||
}
|
||||
if (startMinute < dateUtil.minutesOfDay(this.data.businessStartTime) || endMinute > dateUtil.minutesOfDay(this.data.businessEndTime)) {
|
||||
if (!editOriginalSchedule
|
||||
&& (startMinute < dateUtil.minutesOfDay(this.data.businessStartTime) || endMinute > dateUtil.minutesOfDay(this.data.businessEndTime))) {
|
||||
const tip = `该时间超出营业时间 ${this.data.businessStartTime}-${this.data.businessEndTime}`
|
||||
this.setData({ conflictTip: tip, conflictLevel: 'danger' })
|
||||
return { ok: false, tip }
|
||||
}
|
||||
const now = new Date()
|
||||
const today = dateUtil.formatDate(now)
|
||||
if (form.appointmentDate < today || (form.appointmentDate === today && startMinute <= now.getHours() * 60 + now.getMinutes())) {
|
||||
if (!editOriginalSchedule && (form.appointmentDate < today
|
||||
|| (form.appointmentDate === today && startMinute <= now.getHours() * 60 + now.getMinutes()))) {
|
||||
const tip = '该时间已过,请改选未来时段或使用历史补单'
|
||||
this.setData({ conflictTip: tip, conflictLevel: 'danger' })
|
||||
return { ok: false, tip }
|
||||
@@ -1853,6 +1968,7 @@ Page({
|
||||
return { ok: false, tip }
|
||||
}
|
||||
const conflicts = (calendar.appointments || []).filter(item => {
|
||||
if (this._editMode && String(item.orderItemId) === String(this._editAppointmentId)) return false
|
||||
if (String(item.artistId) !== String(form.artistId) || normalizeAppointmentDate(item) !== form.appointmentDate) return false
|
||||
if (isCancelledAppointment(item)) return false
|
||||
const itemStart = resolveAppointmentStartMinute(item)
|
||||
@@ -1968,7 +2084,7 @@ Page({
|
||||
wx.showToast({ title: '当前账号仅支持查看预约', icon: 'none' })
|
||||
return
|
||||
}
|
||||
if (isShopBusinessExpired()) {
|
||||
if (!this._editMode && isShopBusinessExpired()) {
|
||||
await showBusinessExpiryNotice(this)
|
||||
return
|
||||
}
|
||||
@@ -1976,7 +2092,9 @@ Page({
|
||||
const today = dateUtil.formatDate(new Date())
|
||||
const now = new Date()
|
||||
const selectedMinute = form.appointmentTime ? dateUtil.minutesOfDay(form.appointmentTime) : -1
|
||||
if (form.appointmentDate < today || (form.appointmentDate === today && selectedMinute <= now.getHours() * 60 + now.getMinutes())) {
|
||||
const editOriginalSchedule = this.isEditOriginalSchedule(form.appointmentDate, form.appointmentTime)
|
||||
if (!editOriginalSchedule && (form.appointmentDate < today
|
||||
|| (form.appointmentDate === today && selectedMinute <= now.getHours() * 60 + now.getMinutes()))) {
|
||||
await this.showBackfillPrompt(form.appointmentDate, form.appointmentDate < today ? '所选日期已经过去' : '所选时段已经过去')
|
||||
return
|
||||
}
|
||||
@@ -2059,7 +2177,15 @@ Page({
|
||||
payload.manualProductPrecautions = String(form.manualProductPrecautions || '').trim() || undefined
|
||||
payload.manualProductImageFileIds = String(form.manualProductImageFileIds || '').trim() || undefined
|
||||
}
|
||||
const result = await planningApi.createAppointment(payload)
|
||||
const result = this._editMode
|
||||
? await planningApi.updateAppointment(this._editAppointmentId, payload)
|
||||
: await planningApi.createAppointment(payload)
|
||||
if (this._editMode) {
|
||||
markPlanningRefresh()
|
||||
wx.showToast({ title: '已保存修改', icon: 'success' })
|
||||
wx.navigateBack()
|
||||
return
|
||||
}
|
||||
this.cancelDraftSave()
|
||||
this._draftReady = false
|
||||
this._draftPersistenceDisabled = true
|
||||
@@ -2074,7 +2200,7 @@ Page({
|
||||
if ((error.message || '').includes('有效期') || (error.message || '').includes('到期')) {
|
||||
await showBusinessExpiryNotice(this)
|
||||
} else {
|
||||
showErrorToast(error, '提交失败')
|
||||
showErrorToast(error, this._editMode ? '保存失败' : '提交失败')
|
||||
}
|
||||
} finally {
|
||||
this.setData({ loading: false })
|
||||
|
||||
Reference in New Issue
Block a user