204 lines
7.3 KiB
JavaScript
204 lines
7.3 KiB
JavaScript
const planningApi = require('../../api/planning')
|
|
const productsApi = require('../../api/products')
|
|
const dateUtil = require('../../utils/date')
|
|
|
|
function buildClientRequestId(prefix) {
|
|
return `${prefix}-${Date.now()}-${Math.random().toString(36).slice(2, 10)}`
|
|
}
|
|
|
|
function getCalendarArtists(calendar) {
|
|
return calendar && Array.isArray(calendar.artists) ? calendar.artists : []
|
|
}
|
|
|
|
function findArtist(artists, artistId) {
|
|
return artists.find(item => String(item.id) === String(artistId)) || {}
|
|
}
|
|
|
|
function isScheduleCoveringTime(schedule, form, startMinute, endMinute) {
|
|
if (!schedule || String(schedule.artistId) !== String(form.artistId) || schedule.date !== form.appointmentDate) {
|
|
return false
|
|
}
|
|
if (schedule.status !== 1 || Number(schedule.capacity || 0) < 1) {
|
|
return false
|
|
}
|
|
const scheduleStartMinute = dateUtil.minutesOfDay(String(schedule.startTime || '').slice(0, 5))
|
|
const scheduleEndMinute = dateUtil.minutesOfDay(String(schedule.endTime || '').slice(0, 5))
|
|
return scheduleStartMinute <= startMinute && scheduleEndMinute >= endMinute
|
|
}
|
|
|
|
Page({
|
|
data: {
|
|
form: {
|
|
customerName: '',
|
|
phone: '',
|
|
appointmentDate: dateUtil.formatDate(new Date()),
|
|
appointmentTime: '13:00',
|
|
productId: '',
|
|
durationMinutes: '',
|
|
artistId: '',
|
|
remark: ''
|
|
},
|
|
products: [],
|
|
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 })
|
|
}
|
|
await this.loadArtists()
|
|
this.checkAvailability()
|
|
},
|
|
|
|
async loadArtists() {
|
|
const calendar = await planningApi.calendar({
|
|
startDate: this.data.form.appointmentDate,
|
|
endDate: this.data.form.appointmentDate
|
|
})
|
|
const artists = getCalendarArtists(calendar)
|
|
const selectedArtist = this.data.form.artistId ? findArtist(artists, this.data.form.artistId) : {}
|
|
this.setData({ artists, selectedArtist })
|
|
},
|
|
|
|
onInput(event) {
|
|
this.setData({
|
|
[`form.${event.currentTarget.dataset.field}`]: event.detail.value
|
|
})
|
|
},
|
|
|
|
async onDateChange(event) {
|
|
this.setData({ 'form.appointmentDate': event.detail.value })
|
|
await this.loadArtists()
|
|
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 {
|
|
const calendar = await planningApi.calendar({
|
|
startDate: form.appointmentDate,
|
|
endDate: form.appointmentDate,
|
|
artistId: form.artistId
|
|
})
|
|
const schedule = (calendar.schedules || []).find(item => isScheduleCoveringTime(item, form, startMinute, endMinute))
|
|
if (!schedule) {
|
|
const tip = `${this.data.selectedArtist.name || '该美甲师'} 当前时段无可预约排班`
|
|
this.setData({ conflictTip: tip, conflictLevel: 'danger' })
|
|
return { ok: false, tip }
|
|
}
|
|
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: String(form.productId),
|
|
artistId: String(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()
|
|
}
|
|
})
|