回溯点:保存注册预约客户流程修复进度

This commit is contained in:
2026-10-30 13:02:38 +08:00
parent 7b40ee1650
commit 53b6fcaa11
45 changed files with 960 additions and 132 deletions
+133 -32
View File
@@ -211,6 +211,11 @@ function buildAvailableTimeGroups(calendar, form, durationMinutes) {
.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 today = dateUtil.formatDate(new Date())
const now = new Date()
const currentMinute = now.getHours() * 60 + now.getMinutes()
const businessStart = dateUtil.minutesOfDay(calendar.businessStartTime || '10:00')
const businessEnd = dateUtil.minutesOfDay(calendar.businessEndTime || '22:00')
const groupMap = {
morning: { key: 'morning', title: '上午', times: [] },
afternoon: { key: 'afternoon', title: '下午', times: [] },
@@ -220,6 +225,8 @@ function buildAvailableTimeGroups(calendar, form, durationMinutes) {
const scheduleStart = dateUtil.minutesOfDay(extractTimeText(schedule.startTime))
const scheduleEnd = dateUtil.minutesOfDay(extractTimeText(schedule.endTime))
for (let minute = scheduleStart; minute + durationMinutes <= scheduleEnd; minute += TIME_SLOT_MINUTES) {
if (minute < businessStart || minute + durationMinutes > businessEnd) continue
if (form.appointmentDate < today || (form.appointmentDate === today && minute <= currentMinute)) continue
const endMinute = minute + durationMinutes
if (!hasBusyConflict(minute, endMinute, busyRanges)) {
const period = getTimePeriod(minute)
@@ -238,19 +245,25 @@ function buildAvailableTimeGroups(calendar, form, durationMinutes) {
.filter(group => group.times.length > 0)
}
function buildUnassignedTimeGroups(durationMinutes) {
function buildUnassignedTimeGroups(durationMinutes, businessStart = '10:00', businessEnd = '22:00', appointmentDate) {
const groupMap = {
morning: { key: 'morning', title: '上午', times: [] },
afternoon: { key: 'afternoon', title: '下午', times: [] },
evening: { key: 'evening', title: '晚上', times: [] }
}
const businessStart = dateUtil.minutesOfDay('10:00')
const businessEnd = dateUtil.minutesOfDay('22:00')
for (let minute = businessStart; minute + durationMinutes <= businessEnd; minute += TIME_SLOT_MINUTES) {
const start = dateUtil.minutesOfDay(businessStart)
const end = dateUtil.minutesOfDay(businessEnd)
const now = new Date()
const today = dateUtil.formatDate(now)
const currentMinute = now.getHours() * 60 + now.getMinutes()
for (let minute = start; minute + durationMinutes <= end; minute += TIME_SLOT_MINUTES) {
if (appointmentDate && appointmentDate < today) continue
const period = getTimePeriod(minute)
groupMap[period.key].times.push({
value: dateUtil.timeFromMinutes(minute),
rangeText: buildTimeRangeLabel(minute, durationMinutes)
rangeText: buildTimeRangeLabel(minute, durationMinutes),
status: appointmentDate === today && minute <= currentMinute ? 'past' : 'available',
statusText: appointmentDate === today && minute <= currentMinute ? '已过时' : ''
})
}
return Object.keys(groupMap).map(key => groupMap[key]).filter(group => group.times.length > 0)
@@ -260,12 +273,16 @@ function flattenTimeGroups(groups) {
return (groups || []).reduce((list, group) => list.concat(group.times || []), [])
}
function buildTimeValues(durationMinutes) {
function buildTimeValues(durationMinutes, businessStart = '10:00', businessEnd = '22:00', appointmentDate) {
const values = []
const open = dateUtil.minutesOfDay('10:00')
const close = dateUtil.minutesOfDay('22:00')
const open = dateUtil.minutesOfDay(businessStart)
const close = dateUtil.minutesOfDay(businessEnd)
const now = new Date()
const today = dateUtil.formatDate(now)
const currentMinute = now.getHours() * 60 + now.getMinutes()
for (let minute = open; minute + durationMinutes <= close; minute += TIME_SLOT_MINUTES) {
values.push(dateUtil.timeFromMinutes(minute))
if (appointmentDate && appointmentDate < today) continue
values.push({ value: dateUtil.timeFromMinutes(minute), past: appointmentDate === today && minute <= currentMinute })
}
return values
}
@@ -273,21 +290,25 @@ function buildTimeValues(durationMinutes) {
function buildTimePickerSlots(calendar, form, durationMinutes) {
const schedules = calendar.schedules || []
const busyRanges = form.artistId ? buildBusyRanges(calendar.appointments || [], form) : []
return buildTimeValues(durationMinutes).map(value => {
const businessHours = String(calendar.businessHours || '10:00-22:00').split('-')
const businessStart = calendar.businessStartTime || businessHours[0] || '10:00'
const businessEnd = calendar.businessEndTime || businessHours[1] || '22:00'
return buildTimeValues(durationMinutes, businessStart, businessEnd, form.appointmentDate).map(time => {
const value = time.value
const startMinute = dateUtil.minutesOfDay(value)
const endMinute = startMinute + durationMinutes
const occupied = hasBusyConflict(startMinute, endMinute, busyRanges)
const scheduled = !form.artistId || schedules.some(schedule =>
isScheduleCoveringTime(schedule, form, startMinute, endMinute)
)
const status = occupied ? 'occupied' : scheduled ? 'available' : 'off-shift'
const status = time.past ? 'past' : occupied ? 'occupied' : scheduled ? 'available' : 'off-shift'
return {
value,
hour: Number(value.slice(0, 2)),
minute: Number(value.slice(3, 5)),
rangeText: buildTimeRangeLabel(startMinute, durationMinutes),
status,
statusText: status === 'occupied' ? '已占用' : status === 'off-shift' ? '无排班' : ''
statusText: status === 'past' ? '已过时' : status === 'occupied' ? '已占用' : status === 'off-shift' ? '无排班' : ''
}
})
}
@@ -484,6 +505,7 @@ Page({
manualProductImageUrl: '',
manualProductImages: [],
durationMinutes: '60',
wechatId: '',
artistId: '',
createCustomerRecord: false,
remark: ''
@@ -491,6 +513,10 @@ Page({
unifiedStyleDescription: '',
customerOptions: [],
customerDirectory: [],
businessStartTime: '10:00',
businessEndTime: '22:00',
defaultAppointmentDurationMinutes: 60,
durationManuallyChanged: false,
customerSheetVisible: false,
customerKeyword: '',
customerViewMode: 'list',
@@ -540,6 +566,12 @@ Page({
},
onLoad(options) {
const today = dateUtil.formatDate(new Date())
if (options && options.date && options.date < today) {
this._pendingHistoricalArtistId = options.artistId || ''
this.showBackfillPrompt(options.date, '所选日期已经过去')
return
}
const creatable = canCreateAppointment()
const draftForm = creatable && !hasPresetOptions(options) ? readDraftForm() : null
const baseForm = draftForm || {}
@@ -559,6 +591,7 @@ Page({
appointmentDate,
artistId: options.artistId || baseForm.artistId || ''
},
durationManuallyChanged: Boolean(baseForm.durationMinutes && !baseForm.productId),
manualProductTitleLength: String(baseForm.manualProductTitle || '').length,
unifiedStyleDescription: String(baseForm.remark || baseForm.manualProductDescription || ''),
draftSavedAtText: draftForm ? '已恢复本机草稿' : '',
@@ -617,6 +650,8 @@ Page({
endDate: this.data.form.appointmentDate
})
let artists = getCalendarArtists(calendar)
const hours = String(calendar.businessHours || '10:00-22:00').split('-')
const defaultDuration = Number(calendar.defaultAppointmentDurationMinutes) || 60
const requestedId = preferredArtistId || this.data.form.artistId
if (fallbackEmployee && requestedId && !findArtist(artists, requestedId).id) {
artists = artists.concat({
@@ -629,6 +664,12 @@ Page({
? findArtist(artists, requestedId)
: unassignedSelection
this.setData({
businessStartTime: calendar.businessStartTime || hours[0] || '10:00',
businessEndTime: calendar.businessEndTime || hours[1] || '22:00',
defaultAppointmentDurationMinutes: defaultDuration,
...(!this.data.durationManuallyChanged && !this.data.form.productId && !this.data.form.appointmentTime
? { 'form.durationMinutes': String(defaultDuration) }
: {}),
artists,
artistSelectorItems: this.filterArtistItems(artists, this.data.artistKeyword),
selectedArtist,
@@ -657,7 +698,7 @@ Page({
let page = 1
let total = Number.POSITIVE_INFINITY
while (rows.length < total) {
const result = await customersApi.list({ page, size })
const result = await customersApi.listForAppointment({ page, size })
const currentRows = Array.isArray(result) ? result : (result.list || [])
rows.push(...currentRows)
if (!Array.isArray(result) && Number.isFinite(Number(result.total))) {
@@ -697,6 +738,7 @@ Page({
}
this.setData(patch)
if (field === 'durationMinutes') {
this.setData({ durationManuallyChanged: true })
this.refreshAvailableTimes()
this.checkAvailability()
}
@@ -763,7 +805,7 @@ Page({
clearTimeout(this._suggestionTimer)
this._suggestionTimer = setTimeout(async () => {
try {
const result = await customersApi.list({ page: 1, size: 50, keyword: name || phone })
const result = await customersApi.listForAppointment({ page: 1, size: 50, keyword: name || phone })
if (requestSeq !== this._customerSuggestionSeq) return
const rows = Array.isArray(result) ? result : (result.list || [])
const remoteSuggestions = rows.map(normalizeCustomer)
@@ -786,7 +828,7 @@ Page({
this.setData({
'form.customerUserId': customer.id,
'form.customerName': customer.name,
'form.phone': customer.phone,
'form.phone': customer.phone || '',
'form.createCustomerRecord': false,
customerSuggestions: [],
customerSheetVisible: false,
@@ -847,7 +889,7 @@ Page({
this.setData({
'form.customerUserId': customer.id,
'form.customerName': customer.name,
'form.phone': customer.phone,
'form.phone': customer.phone || '',
'form.createCustomerRecord': false,
customerSheetVisible: false,
customerKeyword: '',
@@ -1021,7 +1063,8 @@ Page({
'form.manualProductImageUrl': '',
'form.manualProductImages': [],
'form.amount': selectedProduct.price === undefined || selectedProduct.price === null ? '' : String(selectedProduct.price),
'form.durationMinutes': String(selectedDuration || this.data.form.durationMinutes || '')
'form.durationMinutes': String(selectedDuration || this.data.defaultAppointmentDurationMinutes || 60),
durationManuallyChanged: false
})
this.closeProductModal()
this.refreshAvailableTimes()
@@ -1219,17 +1262,30 @@ Page({
return
}
if (!form.artistId) {
const availableTimeGroups = buildUnassignedTimeGroups(durationMinutes)
const timePickerSlots = buildTimePickerSlots({ schedules: [], appointments: [] }, form, durationMinutes)
this.setData({
availableTimeGroups,
timePickerSlots,
timePickerGroups: buildTimePickerGroups(timePickerSlots),
...buildTimeWheelState(timePickerSlots, form.appointmentTime),
timeError: '',
timeLoading: false,
timeSelectTip: '请选择预约时间,美甲师可稍后分配'
})
this.setData({ timeLoading: true, timeError: '', timeSelectTip: '正在加载营业时间...' })
try {
const calendar = await planningApi.calendar({ startDate: form.appointmentDate, endDate: form.appointmentDate })
if (requestSeq !== this._availableTimeRequestSeq) return
const hours = String(calendar.businessHours || `${this.data.businessStartTime}-${this.data.businessEndTime}`).split('-')
const businessStartTime = calendar.businessStartTime || hours[0] || '10:00'
const businessEndTime = calendar.businessEndTime || hours[1] || '22:00'
this.setData({ businessStartTime, businessEndTime })
const availableTimeGroups = buildUnassignedTimeGroups(durationMinutes, businessStartTime, businessEndTime, form.appointmentDate)
const timePickerSlots = buildTimePickerSlots(calendar, form, durationMinutes)
this.setData({
availableTimeGroups,
timePickerSlots,
timePickerGroups: buildTimePickerGroups(timePickerSlots),
...buildTimeWheelState(timePickerSlots, form.appointmentTime),
timeError: '',
timeLoading: false,
timeSelectTip: availableTimeGroups.length ? '请选择预约时间,美甲师可稍后分配' : '当前日期没有可选时段'
})
} catch (error) {
if (requestSeq === this._availableTimeRequestSeq) {
this.setData({ availableTimeGroups: [], timePickerSlots: [], timePickerGroups: [], timeError: error.message || '营业时间加载失败', timeLoading: false, timeSelectTip: '营业时间加载失败' })
}
}
return
}
this.setData({
@@ -1293,6 +1349,10 @@ Page({
const value = event.currentTarget.dataset.value
const slot = this.data.timePickerSlots.find(item => item.value === value)
if (!slot || slot.status !== 'available') {
if (slot && slot.status === 'past') {
this.showBackfillPrompt(this.data.form.appointmentDate, '所选时段已经过去')
return
}
wx.showToast({ title: slot && slot.status === 'occupied' ? '该时间已被占用' : '该时间不可预约', icon: 'none' })
return
}
@@ -1326,6 +1386,10 @@ Page({
const candidate = this.data.selectedTimeCandidate
const slot = this.data.timePickerSlots.find(item => item.value === candidate)
if (!slot || slot.status !== 'available') {
if (slot && slot.status === 'past') {
this.showBackfillPrompt(this.data.form.appointmentDate, '所选时段已经过去')
return
}
wx.showToast({ title: slot && slot.status === 'occupied' ? '该时间已被占用' : '该时间不可预约', icon: 'none' })
return
}
@@ -1342,6 +1406,22 @@ Page({
})
},
async showBackfillPrompt(serviceDate, reason = '该日期或时段已过去') {
const result = await new Promise(resolve => wx.showModal({
title: '转为历史补单',
content: `${reason},普通预约不能使用过去时间。可以前往补录历史订单,或取消继续选择未来时间。`,
cancelText: '取消',
confirmText: '去补录',
success: resolve,
fail: () => resolve({ confirm: false })
}))
if (!result.confirm) return
const query = [`serviceDate=${encodeURIComponent(serviceDate || dateUtil.formatDate(new Date()))}`]
const artistId = this.data.form.artistId || this._pendingHistoricalArtistId
if (artistId) query.push(`artistId=${encodeURIComponent(artistId)}`)
wx.navigateTo({ url: `/pages/backfill-order/index?${query.join('&')}` })
},
async checkAvailability(candidateTime) {
const form = this.data.form
const appointmentTime = candidateTime || form.appointmentTime
@@ -1356,8 +1436,15 @@ Page({
}
const startMinute = dateUtil.minutesOfDay(appointmentTime)
const endMinute = startMinute + durationMinutes
if (startMinute < dateUtil.minutesOfDay('10:00') || endMinute > dateUtil.minutesOfDay('22:00')) {
const tip = '该时间超出营业时间 10:00-22:00'
if (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())) {
const tip = '该时间已过,请改选未来时段或使用历史补单'
this.setData({ conflictTip: tip, conflictLevel: 'danger' })
return { ok: false, tip }
}
@@ -1406,7 +1493,13 @@ Page({
async ensureCustomerBeforeSubmit() {
const name = String(this.data.form.customerName || '').trim()
const phone = String(this.data.form.phone || '').trim()
if (!name && !phone) {
const wechatId = String(this.data.form.wechatId || '').trim()
if (!name && (phone || wechatId)) {
this.setData({ 'form.createCustomerRecord': false })
wx.showToast({ title: '填写联系方式时请同时填写客户姓名', icon: 'none' })
return false
}
if (!name) {
this.setData({ 'form.createCustomerRecord': false })
return true
}
@@ -1425,7 +1518,7 @@ Page({
this.setData({ loading: true })
try {
const matches = await customersApi.matchIdentity(name, phone || undefined)
const matches = await customersApi.matchIdentityForAppointment(name, wechatId || undefined, phone || undefined)
if (matches.length > 1) {
const choice = await new Promise(resolve => {
wx.showModal({
@@ -1506,6 +1599,13 @@ Page({
return
}
let form = this.data.form
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())) {
await this.showBackfillPrompt(form.appointmentDate, form.appointmentDate < today ? '所选日期已经过去' : '所选时段已经过去')
return
}
const manualProductTitle = String(form.manualProductTitle || '').trim()
const manualProductImageFileIds = String(form.manualProductImageFileIds || '').trim()
const hasManualProduct = Boolean(manualProductTitle || manualProductImageFileIds || String(form.customerName || '').trim())
@@ -1561,6 +1661,7 @@ Page({
customerUserId: form.customerUserId ? String(form.customerUserId) : undefined,
artistId: form.artistId ? String(form.artistId) : undefined,
customerName: String(form.customerName || '').trim() || undefined,
wechatId: String(form.wechatId || '').trim() || undefined,
phone: String(form.phone || '').trim() || undefined,
amount,
createCustomerRecord: Boolean(form.createCustomerRecord),