完善店务预约状态与详情流程

This commit is contained in:
2026-07-23 01:34:35 +08:00
parent c347e59b50
commit aeaead1648
18 changed files with 1360 additions and 139 deletions
+164 -7
View File
@@ -3,6 +3,7 @@ const shopApi = require('../../api/shop')
const dateUtil = require('../../utils/date')
const { markPlanningRefresh } = require('../../utils/planning-refresh')
const PLANNING_RETURN_TARGET_STORAGE_KEY = 'planningReturnTarget'
const CALENDAR_ERROR_FALLBACK_MESSAGE = '规划数据暂时不可用,请稍后重试'
const TECHNICAL_ERROR_PATTERNS = [
/###\s*Error/i,
@@ -28,6 +29,42 @@ function resolveCalendarErrorMessage(error) {
return hasTechnicalMessage ? CALENDAR_ERROR_FALLBACK_MESSAGE : message
}
function readPlanningReturnTarget() {
try {
const target = wx.getStorageSync(PLANNING_RETURN_TARGET_STORAGE_KEY)
return target && typeof target === 'object' ? target : null
} catch (error) {
throw new Error(`读取规划页返回目标失败:${error.message || error}`)
}
}
function removePlanningReturnTarget() {
try {
wx.removeStorageSync(PLANNING_RETURN_TARGET_STORAGE_KEY)
} catch (error) {
throw new Error(`清理规划页返回目标失败:${error.message || error}`)
}
}
function confirmPlanningAction(title, content, confirmText, confirmColor) {
return new Promise((resolve, reject) => {
wx.showModal({
title,
content,
cancelText: '取消',
confirmText,
confirmColor,
success: result => resolve(Boolean(result.confirm)),
fail: error => reject(new Error(`打开确认弹窗失败:${error.errMsg || error.message || error}`))
})
})
}
function normalizeReturnTargetDate(value) {
const dateText = String(value || '').slice(0, 10)
return /^\d{4}-\d{2}-\d{2}$/.test(dateText) ? dateText : dateUtil.formatDate(new Date())
}
function toLocalDate(value) {
return value instanceof Date ? new Date(value.getTime()) : new Date(String(value).replace(/-/g, '/'))
}
@@ -119,6 +156,24 @@ function appointmentStatusClass(status) {
return ''
}
function resolveAppointmentItemId(item) {
if (!item) {
return ''
}
return item.orderItemId || item.id ? String(item.orderItemId || item.id) : ''
}
function resolveAppointmentEventId(event) {
const currentDataset = event && event.currentTarget && event.currentTarget.dataset ? event.currentTarget.dataset : {}
const targetDataset = event && event.target && event.target.dataset ? event.target.dataset : {}
return currentDataset.orderItemId || currentDataset.id || targetDataset.orderItemId || targetDataset.id || ''
}
function hasAppointmentDataset(dataset) {
return Object.prototype.hasOwnProperty.call(dataset, 'orderItemId')
|| Object.prototype.hasOwnProperty.call(dataset, 'id')
}
function completedOrCancelledClass(status) {
if (isCompletedStatus(status)) return 'is-completed'
if (isCancelledStatus(status)) return 'is-cancelled'
@@ -233,6 +288,31 @@ Page({
onShow() {
const app = getApp()
try {
const returnTarget = readPlanningReturnTarget()
if (returnTarget) {
removePlanningReturnTarget()
const targetDate = normalizeReturnTargetDate(returnTarget.date)
this._needRefresh = false
app.globalData.planningNeedRefresh = false
wx.removeStorageSync('planningNeedRefresh')
this.setData({
viewMode: 'month',
currentDate: toLocalDate(targetDate),
currentDateText: targetDate,
emptyDateActionVisible: false,
pendingEmptyDate: '',
pendingEmptyDateTitle: '',
pendingEmptyDateLabel: '',
savingSpecialDate: false
})
this.prepareView()
this.loadCalendar()
return
}
} catch (error) {
wx.showToast({ title: error.message || '读取规划返回目标失败', icon: 'none' })
}
const storageRefresh = wx.getStorageSync('planningNeedRefresh') === '1'
if (this._needRefresh || app.globalData.planningNeedRefresh || storageRefresh) {
this._needRefresh = false
@@ -267,6 +347,10 @@ Page({
},
selectDate(event) {
const targetDataset = event && event.target && event.target.dataset ? event.target.dataset : {}
if (hasAppointmentDataset(targetDataset)) {
return
}
const currentDate = dateUtil.formatDate(event.currentTarget.dataset.date)
if (this.data.viewMode === 'month') {
const monthDay = this.data.monthDays.find(item => item.date === currentDate)
@@ -384,6 +468,8 @@ Page({
async loadCalendar() {
const range = this.resolveRange()
const requestSeq = (this._calendarRequestSeq || 0) + 1
this._calendarRequestSeq = requestSeq
this.setData({ loading: true })
try {
const data = await planningApi.calendar({
@@ -392,9 +478,15 @@ Page({
artistId: this.data.selectedArtistId || undefined
})
const shopSchedule = await this.loadShopSchedule(range)
if (requestSeq !== this._calendarRequestSeq) {
return
}
this.applyCalendar(data, shopSchedule)
this.setData({ calendarError: '' })
} catch (error) {
if (requestSeq !== this._calendarRequestSeq) {
return
}
this.clearCalendarView()
this.setData({
calendarError: resolveCalendarErrorMessage(error),
@@ -406,7 +498,9 @@ Page({
icon: 'none'
})
} finally {
this.setData({ loading: false })
if (requestSeq === this._calendarRequestSeq) {
this.setData({ loading: false })
}
}
},
@@ -522,8 +616,8 @@ Page({
dayStatus: summary.dayStatus || 'free',
shopRest: shopRestDates.includes(day.date),
artistCounts,
expandedBlocks: this.buildExpandedBlocks(artistCounts),
rowBlocks: this.buildRowBlocks(artistCounts),
expandedBlocks: dayAppointments.length ? this.buildAppointmentBlocks(dayAppointments, 'block') : this.buildExpandedBlocks(artistCounts),
rowBlocks: dayAppointments.length ? this.buildAppointmentBlocks(dayAppointments, 'row') : this.buildRowBlocks(artistCounts),
statusClass: resolveDayStatusClass(dayAppointments)
}
})
@@ -575,8 +669,11 @@ Page({
.filter(item => String(item.artistId) === String(artist.id))
.map(item => {
const itemStart = resolveAppointmentStartMinute(item)
const orderItemId = resolveAppointmentItemId(item)
return {
...item,
id: orderItemId,
orderItemId,
top: Math.max(0, itemStart - startMinute) + laneHeaderHeight,
height: Math.max(82, item.durationMinutes || 60),
timeText: resolveAppointmentTimeText(item),
@@ -611,6 +708,28 @@ Page({
return (this.data.appointments || []).filter(item => resolveAppointmentDate(item) === date)
},
buildAppointmentBlocks(appointments, mode) {
const artistMap = (this.data.artists || []).reduce((map, artist) => {
map[String(artist.id)] = artist
return map
}, {})
return (appointments || []).map((item, index) => {
const artist = artistMap[String(item.artistId)] || {}
const artistName = item.artistName || artist.name || ''
const orderItemId = resolveAppointmentItemId(item)
return {
key: `${mode}-${orderItemId || item.artistId || 'appointment'}-${index}`,
id: orderItemId,
orderItemId,
artistId: item.artistId,
color: item.color || artist.color || '#B8B0A6',
shortName: artist.shortName || String(artistName || '?').slice(0, 1),
artistName: artistName || '?',
statusClass: completedOrCancelledClass(item.status)
}
})
},
appointmentCountsByDate(date) {
const artistMap = (this.data.artists || []).reduce((map, artist) => {
map[String(artist.id)] = artist
@@ -621,15 +740,17 @@ Page({
const artist = artistMap[String(item.artistId)] || {}
const artistName = item.artistName || artist.name
const statusClass = completedOrCancelledClass(item.status)
const key = `${String(item.artistId || item.orderItemId)}-${statusClass || 'active'}`
const orderItemId = resolveAppointmentItemId(item)
const key = `${String(item.artistId || orderItemId)}-${statusClass || 'active'}`
const existing = grouped.get(key)
if (existing) {
grouped.set(key, { ...existing, count: existing.count + 1 })
} else {
grouped.set(key, {
key: `${key}-${index}`,
orderItemId: item.orderItemId,
artistId: item.artistId || item.orderItemId,
id: orderItemId,
orderItemId,
artistId: item.artistId || orderItemId,
artistName,
shortName: artist.shortName || String(artistName || '?').slice(0, 1),
color: artist.color || '#B8B0A6',
@@ -649,8 +770,11 @@ Page({
}, {})
return (counts || []).map(count => {
const artist = colorMap[count.artistId] || {}
const orderItemId = count.orderItemId || count.id || ''
return {
...count,
id: orderItemId,
orderItemId,
key: `${String(count.artistId)}-${count.status || 'summary'}`,
color: count.color || artist.color || '#B8B0A6',
shortName: count.shortName || artist.shortName || '?',
@@ -662,8 +786,11 @@ Page({
buildExpandedBlocks(artistCounts) {
return (artistCounts || []).reduce((blocks, count) => {
for (let index = 0; index < Number(count.count || 0); index += 1) {
const orderItemId = count.orderItemId || count.id || ''
blocks.push({
key: count.key ? `${count.key}-${index}` : `${count.artistId || count.shortName}-${index}`,
id: orderItemId,
orderItemId,
artistId: count.artistId,
color: count.color,
shortName: count.shortName,
@@ -677,8 +804,11 @@ Page({
buildRowBlocks(artistCounts) {
return (artistCounts || []).reduce((rows, count) => {
for (let index = 0; index < Number(count.count || 0); index += 1) {
const orderItemId = count.orderItemId || count.id || ''
rows.push({
key: count.key ? `${count.key}-row-${index}` : `${count.artistId || count.shortName}-row-${index}`,
id: orderItemId,
orderItemId,
artistId: count.artistId,
color: count.color,
shortName: count.shortName,
@@ -730,6 +860,16 @@ Page({
wx.showToast({ title: '请设置正确营业时间', icon: 'none' })
return
}
let confirmed
try {
confirmed = await confirmPlanningAction('保存特殊日期', `确认将 ${this.data.pendingEmptyDate} 设置为${option.label}?`, '保存', '#6f8f83')
} catch (error) {
wx.showToast({ title: error.message || '确认保存失败', icon: 'none' })
return
}
if (!confirmed) {
return
}
this.setData({ savingSpecialDate: true })
try {
await shopApi.saveScheduleException({
@@ -751,8 +891,25 @@ Page({
},
openAppointment(event) {
const currentDataset = event && event.currentTarget && event.currentTarget.dataset ? event.currentTarget.dataset : {}
const count = Number(currentDataset.count || 1)
if (this.data.viewMode === 'month' && this.data.monthDisplayMode === 'count' && count > 1) {
const date = normalizeReturnTargetDate(currentDataset.date)
this.setData({
currentDate: toLocalDate(date),
currentDateText: date,
monthDisplayMode: 'blocks'
})
wx.showToast({ title: '请点击具体预约块查看详情', icon: 'none' })
return
}
const id = resolveAppointmentEventId(event)
if (!id) {
wx.showToast({ title: '缺少预约详情ID', icon: 'none' })
return
}
wx.navigateTo({
url: `/pages/appointment-detail/index?id=${event.currentTarget.dataset.id}`
url: `/pages/appointment-detail/index?id=${encodeURIComponent(id)}`
})
}
})