176 lines
6.1 KiB
JavaScript
176 lines
6.1 KiB
JavaScript
const planningApi = require('../../api/planning')
|
|
const productsApi = require('../../api/products')
|
|
const mock = require('../../utils/mock')
|
|
const dateUtil = require('../../utils/date')
|
|
|
|
function buildClientRequestId(prefix) {
|
|
return `${prefix}-${Date.now()}-${Math.random().toString(36).slice(2, 10)}`
|
|
}
|
|
|
|
Page({
|
|
data: {
|
|
form: {
|
|
customerName: '',
|
|
phone: '',
|
|
appointmentDate: dateUtil.formatDate(new Date()),
|
|
appointmentTime: '13:00',
|
|
productId: '',
|
|
durationMinutes: '',
|
|
artistId: '',
|
|
remark: ''
|
|
},
|
|
products: [],
|
|
artists: mock.artists,
|
|
selectedProduct: {},
|
|
selectedArtist: {},
|
|
conflictTip: '',
|
|
conflictLevel: 'free',
|
|
loading: false
|
|
},
|
|
|
|
onLoad(options) {
|
|
this.setData({
|
|
'form.appointmentDate': options.date || dateUtil.formatDate(new Date()),
|
|
'form.artistId': options.artistId || ''
|
|
})
|
|
this.loadOptions()
|
|
},
|
|
|
|
async loadOptions() {
|
|
const fallbackProducts = [
|
|
{ id: 1, title: '纯色美甲', approximateDuration: '90分钟' },
|
|
{ id: 2, title: '猫眼延长', approximateDuration: '120分钟' },
|
|
{ id: 3, title: '手部护理', approximateDuration: '60分钟' }
|
|
]
|
|
try {
|
|
const productsData = await productsApi.list({ page: 1, size: 100 })
|
|
const products = productsData.list || productsData || fallbackProducts
|
|
this.setData({ products })
|
|
} catch (error) {
|
|
this.setData({ products: fallbackProducts })
|
|
}
|
|
if (this.data.form.artistId) {
|
|
const selectedArtist = this.data.artists.find(item => String(item.id) === String(this.data.form.artistId)) || {}
|
|
this.setData({ selectedArtist })
|
|
}
|
|
this.checkAvailability()
|
|
},
|
|
|
|
onInput(event) {
|
|
this.setData({
|
|
[`form.${event.currentTarget.dataset.field}`]: event.detail.value
|
|
})
|
|
},
|
|
|
|
onDateChange(event) {
|
|
this.setData({ 'form.appointmentDate': event.detail.value })
|
|
this.checkAvailability()
|
|
},
|
|
|
|
onTimeChange(event) {
|
|
this.setData({ 'form.appointmentTime': event.detail.value })
|
|
this.checkAvailability()
|
|
},
|
|
|
|
onProductChange(event) {
|
|
const selectedProduct = this.data.products[Number(event.detail.value)] || {}
|
|
this.setData({
|
|
selectedProduct,
|
|
'form.productId': selectedProduct.id,
|
|
'form.durationMinutes': String(parseInt(selectedProduct.approximateDuration, 10) || this.data.form.durationMinutes || 60)
|
|
})
|
|
this.checkAvailability()
|
|
},
|
|
|
|
onArtistChange(event) {
|
|
const selectedArtist = this.data.artists[Number(event.detail.value)] || {}
|
|
this.setData({
|
|
selectedArtist,
|
|
'form.artistId': selectedArtist.id
|
|
})
|
|
this.checkAvailability()
|
|
},
|
|
|
|
async checkAvailability() {
|
|
const form = this.data.form
|
|
if (!form.appointmentDate || !form.appointmentTime || !form.artistId) {
|
|
this.setData({ conflictTip: '', conflictLevel: 'free' })
|
|
return { ok: true }
|
|
}
|
|
const durationMinutes = Number(form.durationMinutes || 0) || 60
|
|
const startMinute = dateUtil.minutesOfDay(form.appointmentTime)
|
|
const endMinute = startMinute + durationMinutes
|
|
if (startMinute < dateUtil.minutesOfDay('10:00') || endMinute > dateUtil.minutesOfDay('22:00')) {
|
|
const tip = '该时间超出营业时间 10:00-22:00'
|
|
this.setData({ conflictTip: tip, conflictLevel: 'danger' })
|
|
return { ok: false, tip }
|
|
}
|
|
try {
|
|
let calendar
|
|
try {
|
|
calendar = await planningApi.calendar({
|
|
startDate: form.appointmentDate,
|
|
endDate: form.appointmentDate,
|
|
artistId: form.artistId
|
|
})
|
|
} catch (error) {
|
|
calendar = mock.buildCalendar(form.appointmentDate)
|
|
}
|
|
const conflicts = (calendar.appointments || []).filter(item => {
|
|
if (String(item.artistId) !== String(form.artistId) || item.date !== form.appointmentDate) return false
|
|
if (item.status === 3 || item.statusText === '已取消') return false
|
|
const itemStart = Number(item.startMinute || dateUtil.minutesOfDay((item.startTime || '').slice(11, 16)))
|
|
const itemEnd = itemStart + Number(item.durationMinutes || 60)
|
|
return startMinute < itemEnd && endMinute > itemStart
|
|
})
|
|
if (conflicts.length > 0) {
|
|
const first = conflicts[0]
|
|
const itemStart = Number(first.startMinute || dateUtil.minutesOfDay((first.startTime || '').slice(11, 16)))
|
|
const itemEnd = itemStart + Number(first.durationMinutes || 60)
|
|
const tip = `${this.data.selectedArtist.name || '该美甲师'} ${dateUtil.timeFromMinutes(itemStart)}-${dateUtil.timeFromMinutes(itemEnd)} 已有预约`
|
|
this.setData({ conflictTip: tip, conflictLevel: 'danger' })
|
|
return { ok: false, tip }
|
|
}
|
|
const tip = `${this.data.selectedArtist.name || '该美甲师'} ${form.appointmentTime}-${dateUtil.timeFromMinutes(endMinute)} 可预约`
|
|
this.setData({ conflictTip: tip, conflictLevel: 'free' })
|
|
return { ok: true, tip }
|
|
} catch (error) {
|
|
const tip = '暂未获取到排班,请保存时以系统校验为准'
|
|
this.setData({ conflictTip: tip, conflictLevel: 'warn' })
|
|
return { ok: true, tip }
|
|
}
|
|
},
|
|
|
|
async submit() {
|
|
const form = this.data.form
|
|
if (!form.customerName || !form.phone || !form.productId || !form.artistId) {
|
|
wx.showToast({ title: '请补全预约信息', icon: 'none' })
|
|
return
|
|
}
|
|
const availability = await this.checkAvailability()
|
|
if (!availability.ok) {
|
|
wx.showToast({ title: availability.tip || '该时段已被预约', icon: 'none' })
|
|
return
|
|
}
|
|
this.setData({ loading: true })
|
|
try {
|
|
const result = await planningApi.createAppointment({
|
|
...form,
|
|
productId: Number(form.productId),
|
|
artistId: Number(form.artistId),
|
|
durationMinutes: Number(form.durationMinutes || 0) || undefined,
|
|
clientRequestId: buildClientRequestId('manager-appointment')
|
|
})
|
|
wx.redirectTo({ url: `/pages/appointment-detail/index?id=${result.orderItemId}` })
|
|
} catch (error) {
|
|
wx.showToast({ title: error.message || '保存失败', icon: 'none' })
|
|
} finally {
|
|
this.setData({ loading: false })
|
|
}
|
|
},
|
|
|
|
back() {
|
|
wx.navigateBack()
|
|
}
|
|
})
|