197 lines
6.6 KiB
JavaScript
197 lines
6.6 KiB
JavaScript
const planningApi = require('../../api/planning')
|
|
const productsApi = require('../../api/products')
|
|
const config = require('../../utils/config')
|
|
const mock = require('../../utils/mock')
|
|
const dateUtil = require('../../utils/date')
|
|
|
|
const toDurationMinutes = value => {
|
|
if (typeof value === 'number') return value
|
|
const parsed = parseInt(String(value || ''), 10)
|
|
return Number.isFinite(parsed) && parsed > 0 ? parsed : 60
|
|
}
|
|
|
|
Page({
|
|
data: {
|
|
form: {
|
|
customerName: '',
|
|
phone: '',
|
|
appointmentDate: dateUtil.formatDate(new Date()),
|
|
appointmentTime: '13:00',
|
|
productId: '',
|
|
durationMinutes: '',
|
|
artistId: '',
|
|
remark: ''
|
|
},
|
|
products: [],
|
|
artists: config.USE_DEV_MOCK_DATA ? mock.artists : [],
|
|
selectedProduct: {},
|
|
selectedArtist: {},
|
|
conflictTip: '',
|
|
conflictLevel: 'free',
|
|
loading: false,
|
|
optionError: ''
|
|
},
|
|
|
|
onLoad(options = {}) {
|
|
this.setData({
|
|
'form.appointmentDate': options.date || dateUtil.formatDate(new Date()),
|
|
'form.artistId': options.artistId || ''
|
|
})
|
|
this.loadOptions()
|
|
},
|
|
|
|
async loadOptions() {
|
|
let products = []
|
|
let artists = []
|
|
let optionError = ''
|
|
try {
|
|
const productsData = await productsApi.list({ page: 1, size: 100 })
|
|
products = productsData.list || productsData || []
|
|
} catch (error) {
|
|
optionError = error.message || '项目数据加载失败'
|
|
products = config.USE_DEV_MOCK_DATA
|
|
? [
|
|
{ id: 1, title: '纯色美甲', approximateDuration: '90分钟' },
|
|
{ id: 2, title: '猫眼延长', approximateDuration: '120分钟' },
|
|
{ id: 3, title: '手部护理', approximateDuration: '60分钟' }
|
|
]
|
|
: []
|
|
}
|
|
|
|
try {
|
|
const calendar = await planningApi.calendar({
|
|
startDate: this.data.form.appointmentDate,
|
|
endDate: this.data.form.appointmentDate
|
|
})
|
|
artists = calendar.artists || []
|
|
} catch (error) {
|
|
optionError = optionError || error.message || '美甲师数据加载失败'
|
|
artists = config.USE_DEV_MOCK_DATA ? mock.artists : []
|
|
}
|
|
|
|
const selectedArtist = this.data.form.artistId
|
|
? artists.find(item => String(item.id) === String(this.data.form.artistId)) || {}
|
|
: {}
|
|
this.setData({ products, artists, selectedArtist, optionError })
|
|
this.checkAvailability()
|
|
},
|
|
|
|
onInput(event) {
|
|
this.setData({
|
|
[`form.${event.currentTarget.dataset.field}`]: event.detail.value
|
|
})
|
|
},
|
|
|
|
onDateChange(event) {
|
|
this.setData({ 'form.appointmentDate': event.detail.value })
|
|
this.loadOptions()
|
|
},
|
|
|
|
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(toDurationMinutes(selectedProduct.approximateDuration))
|
|
})
|
|
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 }
|
|
}
|
|
|
|
let calendar
|
|
try {
|
|
calendar = await planningApi.calendar({
|
|
startDate: form.appointmentDate,
|
|
endDate: form.appointmentDate,
|
|
artistId: form.artistId
|
|
})
|
|
} catch (error) {
|
|
if (config.USE_DEV_MOCK_DATA) {
|
|
calendar = mock.buildCalendar(form.appointmentDate)
|
|
} else {
|
|
const tip = error.message || '暂未获取到排班,无法确认该时段是否可预约'
|
|
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 }
|
|
},
|
|
|
|
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
|
|
})
|
|
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()
|
|
}
|
|
})
|