142 lines
4.6 KiB
JavaScript
142 lines
4.6 KiB
JavaScript
const planningApi = require('../../api/planning')
|
|
const { markPlanningRefresh } = require('../../utils/planning-refresh')
|
|
|
|
function displayValue(value, fallback) {
|
|
if (value === null || value === undefined) return fallback
|
|
const text = String(value).trim()
|
|
return !text || text === 'null' || text === 'undefined' ? fallback : text
|
|
}
|
|
|
|
function formatAmount(value) {
|
|
const amount = displayValue(value, '')
|
|
return amount ? `¥${amount}` : '未记录'
|
|
}
|
|
|
|
Page({
|
|
data: {
|
|
id: '',
|
|
appointment: {},
|
|
avatarText: '',
|
|
timeText: '',
|
|
isUnassigned: false,
|
|
showTransfer: false,
|
|
showConfirm: false,
|
|
showComplete: false,
|
|
showCancel: false,
|
|
updating: false,
|
|
loadError: ''
|
|
},
|
|
|
|
onLoad(options) {
|
|
this.setData({ id: options.id || '' })
|
|
this.loadDetail()
|
|
},
|
|
|
|
async loadDetail() {
|
|
try {
|
|
const appointment = await planningApi.appointment(this.data.id)
|
|
this.applyAppointment(appointment)
|
|
} catch (error) {
|
|
this.setData({
|
|
appointment: {},
|
|
avatarText: '',
|
|
timeText: '',
|
|
loadError: error.message || '预约详情加载失败'
|
|
})
|
|
}
|
|
},
|
|
|
|
applyAppointment(appointment) {
|
|
const safeAppointment = appointment || {}
|
|
const status = Number(safeAppointment.status)
|
|
const historicalBackfill = Boolean(safeAppointment.historicalBackfill)
|
|
const isUnassigned = safeAppointment.assignmentStatus === 'UNASSIGNED' || !safeAppointment.artistId
|
|
const startTime = displayValue(safeAppointment.startTime, '')
|
|
const endTime = displayValue(safeAppointment.endTime, '')
|
|
const timeText = historicalBackfill && !startTime
|
|
? '时间未记录'
|
|
: startTime
|
|
? `${startTime.slice(0, 16)}${endTime ? ` - ${endTime.slice(11, 16)}` : ''}`
|
|
: displayValue(safeAppointment.date, '时间未记录')
|
|
const normalizedAppointment = {
|
|
...safeAppointment,
|
|
customerNameText: displayValue(safeAppointment.customerName, '散客'),
|
|
customerPhoneText: displayValue(safeAppointment.customerPhone, '未留手机号'),
|
|
orderNoText: displayValue(safeAppointment.orderNo, '未记录'),
|
|
productTitleText: displayValue(safeAppointment.productTitle, '未记录'),
|
|
artistNameText: isUnassigned ? '待分配' : displayValue(safeAppointment.artistName, '未记录'),
|
|
durationText: Number(safeAppointment.durationMinutes) > 0 ? `${safeAppointment.durationMinutes} 分钟` : '未记录',
|
|
amountText: formatAmount(safeAppointment.amount),
|
|
statusText: displayValue(safeAppointment.statusText, '状态未知'),
|
|
remarkText: displayValue(safeAppointment.remark, '暂无备注'),
|
|
cancelReasonText: displayValue(safeAppointment.cancelReason, '')
|
|
}
|
|
this.setData({
|
|
appointment: normalizedAppointment,
|
|
isUnassigned,
|
|
avatarText: normalizedAppointment.customerNameText.slice(0, 1),
|
|
timeText,
|
|
showTransfer: Boolean(safeAppointment.canEdit) && !historicalBackfill,
|
|
showConfirm: Boolean(safeAppointment.canEdit) && !historicalBackfill && !isUnassigned && status === 0,
|
|
showComplete: !isUnassigned && (status === 1 || status === 4),
|
|
showCancel: Boolean(safeAppointment.canCancel),
|
|
loadError: ''
|
|
})
|
|
},
|
|
|
|
async updateStatus(status) {
|
|
if (this.data.updating || !this.data.appointment.orderId) return
|
|
if (status !== 3 && this.data.isUnassigned) {
|
|
wx.showToast({ title: '请先分配美甲师', icon: 'none' })
|
|
return
|
|
}
|
|
this.setData({ updating: true })
|
|
try {
|
|
await planningApi.updateStatus(this.data.appointment.orderId, status, this._cancelReason || '')
|
|
markPlanningRefresh()
|
|
wx.showToast({ title: '已更新' })
|
|
this.loadDetail()
|
|
} catch (error) {
|
|
wx.showToast({ title: error.message || '操作失败', icon: 'none' })
|
|
} finally {
|
|
this.setData({ updating: false })
|
|
}
|
|
},
|
|
|
|
confirm() {
|
|
if (!this.data.showConfirm || this.data.updating) return
|
|
this.updateStatus(1)
|
|
},
|
|
|
|
complete() {
|
|
if (!this.data.showComplete || this.data.updating) return
|
|
this.updateStatus(2)
|
|
},
|
|
|
|
cancel() {
|
|
if (!this.data.showCancel || this.data.updating) return
|
|
this._cancelReason = ''
|
|
wx.showModal({
|
|
title: '取消预约',
|
|
editable: true,
|
|
placeholderText: '可填写取消原因',
|
|
content: '',
|
|
confirmText: '确认取消',
|
|
success: result => {
|
|
if (!result.confirm) return
|
|
this._cancelReason = result.content || ''
|
|
this.updateStatus(3)
|
|
}
|
|
})
|
|
},
|
|
|
|
goTransfer() {
|
|
if (!this.data.showTransfer) return
|
|
wx.navigateTo({ url: `/pages/artist-transfer/index?id=${this.data.id}` })
|
|
},
|
|
|
|
back() {
|
|
wx.navigateBack()
|
|
}
|
|
})
|