96 lines
2.7 KiB
JavaScript
96 lines
2.7 KiB
JavaScript
const planningApi = require('../../api/planning')
|
|
const config = require('../../utils/config')
|
|
const mock = require('../../utils/mock')
|
|
const dateUtil = require('../../utils/date')
|
|
|
|
const normalizeArtists = artists => (artists || []).map((item, index) => ({
|
|
id: item.id || item.userId || item.artistId,
|
|
name: item.name || item.nickname || item.artistName || `美甲师${index + 1}`,
|
|
shortName: item.shortName || String(item.name || item.nickname || '师').slice(-1),
|
|
color: item.color || ['#7BA7C7', '#89A98B', '#A893BC', '#D49A89'][index % 4],
|
|
statusText: item.statusText || '可接单',
|
|
availableStatus: item.availableStatus || 'free',
|
|
availableText: item.availableText || '可接'
|
|
}))
|
|
|
|
Page({
|
|
data: {
|
|
id: '',
|
|
artists: [],
|
|
selectedArtistId: '',
|
|
date: dateUtil.formatDate(new Date()),
|
|
time: '13:00',
|
|
loading: false,
|
|
loadError: false,
|
|
errorMessage: ''
|
|
},
|
|
|
|
onLoad(options = {}) {
|
|
this.setData({ id: options.id || '' })
|
|
this.loadArtists()
|
|
},
|
|
|
|
async loadArtists() {
|
|
this.setData({ loadError: false, errorMessage: '' })
|
|
try {
|
|
const calendar = await planningApi.calendar({
|
|
startDate: this.data.date,
|
|
endDate: this.data.date
|
|
})
|
|
this.setData({ artists: normalizeArtists(calendar.artists || []) })
|
|
} catch (error) {
|
|
if (config.USE_DEV_MOCK_DATA) {
|
|
this.setData({ artists: normalizeArtists(mock.artists) })
|
|
} else {
|
|
this.setData({
|
|
artists: [],
|
|
loadError: true,
|
|
errorMessage: error.message || '美甲师可接状态加载失败,请检查服务连接后重试'
|
|
})
|
|
}
|
|
}
|
|
},
|
|
|
|
selectArtist(event) {
|
|
this.setData({ selectedArtistId: event.currentTarget.dataset.id })
|
|
},
|
|
|
|
onDateChange(event) {
|
|
this.setData({ date: event.detail.value, selectedArtistId: '' })
|
|
this.loadArtists()
|
|
},
|
|
|
|
onTimeChange(event) {
|
|
this.setData({ time: event.detail.value })
|
|
},
|
|
|
|
async submit() {
|
|
if (this.data.loadError) {
|
|
wx.showToast({ title: '请先重新加载可接员工', icon: 'none' })
|
|
return
|
|
}
|
|
if (!this.data.selectedArtistId) {
|
|
wx.showToast({ title: '请选择美甲师', icon: 'none' })
|
|
return
|
|
}
|
|
this.setData({ loading: true })
|
|
try {
|
|
await planningApi.moveAppointment(this.data.id, {
|
|
artistId: Number(this.data.selectedArtistId),
|
|
appointmentDate: this.data.date,
|
|
appointmentTime: this.data.time
|
|
})
|
|
wx.showToast({ title: '已改派' })
|
|
setTimeout(() => wx.navigateBack(), 500)
|
|
} catch (error) {
|
|
wx.showToast({ title: error.message || '改派失败', icon: 'none' })
|
|
} finally {
|
|
this.setData({ loading: false })
|
|
}
|
|
},
|
|
|
|
back() {
|
|
wx.navigateBack()
|
|
}
|
|
})
|