299 lines
11 KiB
JavaScript
299 lines
11 KiB
JavaScript
const employeesApi = require('../../api/employees')
|
|
const { chooseImageFilePath, uploadObjectFile } = require('../../utils/upload')
|
|
const { showErrorToast, toUserMessage } = require('../../utils/toast')
|
|
|
|
const EMPLOYEE_COLORS = ['#7BA7C7', '#8FB89A', '#A894C7', '#D7A86E', '#D98DA5', '#79A6A3', '#B9A16B', '#9BA9C8']
|
|
|
|
Page({
|
|
data: {
|
|
pickerMode: false,
|
|
appointmentContext: { date: '', time: '', durationMinutes: '' },
|
|
applicationId: '',
|
|
employeeColors: EMPLOYEE_COLORS,
|
|
onboardingVisible: false,
|
|
createdEmployee: null,
|
|
associationSheetVisible: false,
|
|
associationLoading: false,
|
|
associationApplications: [],
|
|
associationItems: [],
|
|
associationKeyword: '',
|
|
associationSelectedId: '',
|
|
form: {
|
|
nickname: '',
|
|
phone: '',
|
|
wechatId: '',
|
|
color: '#8FB89A',
|
|
avatar: '',
|
|
avatarUrl: ''
|
|
},
|
|
avatarUploading: false,
|
|
creating: false
|
|
},
|
|
|
|
onLoad(options) {
|
|
this.setData({
|
|
pickerMode: options && options.picker === '1',
|
|
appointmentContext: {
|
|
date: options.date || '',
|
|
time: options.time || '',
|
|
durationMinutes: options.durationMinutes || ''
|
|
},
|
|
applicationId: options.applicationId || '',
|
|
applicationNo: options.applicationNo || ''
|
|
})
|
|
},
|
|
|
|
onInput(event) {
|
|
const field = event.currentTarget.dataset.field
|
|
const value = event.detail.value
|
|
this.setData({ [`form.${field}`]: value })
|
|
},
|
|
|
|
selectColor(event) {
|
|
this.setData({ 'form.color': event.currentTarget.dataset.color })
|
|
},
|
|
|
|
async chooseAvatar() {
|
|
if (this.data.avatarUploading || this.data.creating) return
|
|
try {
|
|
const filePath = await chooseImageFilePath()
|
|
if (!filePath) return
|
|
this.setData({ avatarUploading: true })
|
|
const uploadResult = await uploadObjectFile(filePath)
|
|
// 私有对象存储地址可能无法被小程序直接访问,上传完成后先用本地临时文件即时预览。
|
|
const storedAvatar = uploadResult.fileUrl || uploadResult.primaryUrl || ''
|
|
this.setData({
|
|
'form.avatar': storedAvatar,
|
|
'form.avatarUrl': filePath
|
|
})
|
|
} catch (error) {
|
|
showErrorToast(error, '头像上传失败')
|
|
} finally {
|
|
this.setData({ avatarUploading: false })
|
|
}
|
|
},
|
|
|
|
removeAvatar() {
|
|
this.setData({ 'form.avatar': '', 'form.avatarUrl': '' })
|
|
},
|
|
|
|
onAvatarError() {
|
|
this.setData({ 'form.avatarUrl': '' })
|
|
},
|
|
|
|
stopTouchMove() {
|
|
},
|
|
|
|
async submit() {
|
|
if (this.data.creating || this.data.avatarUploading) return
|
|
if (this.data.createdEmployee) {
|
|
if (this.data.applicationId && !this.data.applicationApproved) return this.associateCreatedEmployee()
|
|
this.setData({ onboardingVisible: true })
|
|
return
|
|
}
|
|
const form = this.data.form
|
|
const nickname = String(form.nickname || '').trim()
|
|
const phone = String(form.phone || '').trim()
|
|
const wechatId = String(form.wechatId || '').trim()
|
|
if (!nickname || !phone) {
|
|
wx.showToast({ title: '请补全美甲师信息', icon: 'none' })
|
|
return
|
|
}
|
|
if (!/^1[3-9]\d{9}$/.test(phone)) {
|
|
wx.showToast({ title: '请输入正确手机号', icon: 'none' })
|
|
return
|
|
}
|
|
this.setData({ creating: true })
|
|
try {
|
|
const result = await employeesApi.createUser({
|
|
nickname,
|
|
phone,
|
|
contactPhone: phone,
|
|
wechatId,
|
|
color: form.color,
|
|
avatar: form.avatar || undefined,
|
|
type: 3
|
|
})
|
|
const id = result && typeof result === 'object' ? (result.id || result.userId) : result
|
|
const employee = { id, name: nickname, title: nickname, avatar: form.avatar || '' }
|
|
this.setData({ createdEmployee: employee, applicationApproved: false })
|
|
if (this.data.applicationId) {
|
|
await this.associateCreatedEmployee()
|
|
return
|
|
}
|
|
await this.offerPendingAssociation()
|
|
} catch (error) {
|
|
showErrorToast(error, '新增失败')
|
|
} finally {
|
|
this.setData({ creating: false })
|
|
}
|
|
},
|
|
|
|
async associateCreatedEmployee() {
|
|
const employee = this.data.createdEmployee
|
|
if (!employee || !employee.id || !this.data.applicationId || this.data.associatingApplication) return
|
|
this.setData({ associatingApplication: true })
|
|
try {
|
|
await employeesApi.approveApplication(this.data.applicationId, {
|
|
targetArtistUserId: employee.id,
|
|
useApplicantNickname: false,
|
|
useApplicantAvatar: false
|
|
})
|
|
this.setData({ applicationApproved: true, associatingApplication: false, onboardingVisible: true })
|
|
wx.showToast({ title: '已关联并通过审核', icon: 'success' })
|
|
} catch (error) {
|
|
this.setData({ associatingApplication: false })
|
|
wx.showModal({
|
|
title: '关联审核未完成',
|
|
content: toUserMessage(error, '美甲师档案已创建,可重试关联申请。'),
|
|
confirmText: '重试',
|
|
cancelText: '稍后处理',
|
|
success: result => {
|
|
if (result.confirm) this.associateCreatedEmployee()
|
|
else this.setData({ onboardingVisible: true })
|
|
}
|
|
})
|
|
}
|
|
},
|
|
|
|
async offerPendingAssociation() {
|
|
try {
|
|
const data = await employeesApi.unassociatedApplications()
|
|
const applications = Array.isArray(data) ? data : []
|
|
if (!applications.length) {
|
|
this.setData({ onboardingVisible: true })
|
|
return
|
|
}
|
|
const associationItems = applications.map(item => ({
|
|
id: item.id,
|
|
title: item.applicantNickname || '待审核美甲师申请',
|
|
subtitle: [item.applicantMaskedPhone, item.createTime && String(item.createTime).replace('T', ' ').slice(0, 16)].filter(Boolean).join(' · ') || '申请资料尚未审核',
|
|
meta: item.applicationNo || ''
|
|
}))
|
|
this.setData({
|
|
associationApplications: applications,
|
|
associationItems,
|
|
associationKeyword: '',
|
|
associationSelectedId: '',
|
|
associationSheetVisible: true
|
|
})
|
|
} catch (error) {
|
|
wx.showToast({ title: '申请列表暂时不可用,已继续新增员工', icon: 'none' })
|
|
this.setData({ onboardingVisible: true })
|
|
}
|
|
},
|
|
|
|
onAssociationSearch(event) {
|
|
const associationKeyword = String(event.detail.value || '').trim().toLowerCase()
|
|
const applications = this.data.associationApplications
|
|
const associationItems = applications
|
|
.filter(item => !associationKeyword || [item.applicantNickname, item.applicantMaskedPhone, item.applicationNo]
|
|
.some(value => String(value || '').toLowerCase().includes(associationKeyword)))
|
|
.map(item => ({
|
|
id: item.id,
|
|
title: item.applicantNickname || '待审核美甲师申请',
|
|
subtitle: [item.applicantMaskedPhone, item.createTime && String(item.createTime).replace('T', ' ').slice(0, 16)].filter(Boolean).join(' · ') || '申请资料尚未审核',
|
|
meta: item.applicationNo || ''
|
|
}))
|
|
this.setData({ associationKeyword, associationItems })
|
|
},
|
|
|
|
async selectAssociation(event) {
|
|
const application = event.detail && event.detail.item
|
|
const employee = this.data.createdEmployee
|
|
if (!application || !employee || !employee.id || this.data.associationLoading) return
|
|
this.setData({ associationLoading: true, associationSelectedId: application.id })
|
|
try {
|
|
await employeesApi.preAssociateApplication(application.id, employee.id)
|
|
this.setData({ associationSheetVisible: false, onboardingVisible: true })
|
|
wx.showToast({ title: '已预关联申请', icon: 'success' })
|
|
} catch (error) {
|
|
showErrorToast(error, '关联失败,请重新选择')
|
|
this.setData({ associationSelectedId: '' })
|
|
} finally {
|
|
this.setData({ associationLoading: false })
|
|
}
|
|
},
|
|
|
|
skipAssociation() {
|
|
if (this.data.associationLoading) return
|
|
this.setData({ associationSheetVisible: false, onboardingVisible: true })
|
|
},
|
|
|
|
setupFirstSchedule() {
|
|
if (this.data.applicationId && !this.data.applicationApproved) return
|
|
const employee = this.data.createdEmployee
|
|
if (!employee || !employee.id) return
|
|
const context = this.data.appointmentContext
|
|
const query = [
|
|
`id=${encodeURIComponent(employee.id)}`,
|
|
'onboarding=1',
|
|
`source=${this.data.pickerMode ? 'appointment' : 'employees'}`,
|
|
`date=${encodeURIComponent(context.date || '')}`,
|
|
`time=${encodeURIComponent(context.time || '')}`,
|
|
`durationMinutes=${encodeURIComponent(context.durationMinutes || '')}`
|
|
]
|
|
wx.navigateTo({
|
|
url: `/pages/schedule-setting/index?${query.join('&')}`,
|
|
success: result => {
|
|
result.eventChannel.on('scheduleSaved', () => {
|
|
setTimeout(() => this.finishCreated(this.data.pickerMode, true), 250)
|
|
})
|
|
}
|
|
})
|
|
},
|
|
|
|
skipFirstSchedule() {
|
|
if (this.data.savingDefaultSchedule || !this.data.createdEmployee || (this.data.applicationId && !this.data.applicationApproved)) return
|
|
this.setData({ savingDefaultSchedule: true })
|
|
employeesApi.saveScheduleRule({
|
|
artistId: this.data.createdEmployee.id,
|
|
presetType: 'CUSTOM',
|
|
workDays: [1, 2, 3, 4, 5, 6, 7],
|
|
startTime: '00:00',
|
|
endTime: '23:59',
|
|
status: 1,
|
|
remark: '新建美甲师默认全年全天排班,预约仍受门店营业时间限制,可在排班设置中调整'
|
|
}).then(() => {
|
|
this.finishCreated(this.data.pickerMode, true)
|
|
}).catch(error => {
|
|
wx.showModal({
|
|
title: '默认排班设置失败',
|
|
content: toUserMessage(error, '未设置排班前,该美甲师暂不可预约。请重试或返回排班设置。'),
|
|
confirmText: '重试',
|
|
cancelText: '稍后设置',
|
|
success: result => {
|
|
if (result.confirm) this.skipFirstSchedule()
|
|
else this.setData({ savingDefaultSchedule: false })
|
|
}
|
|
})
|
|
}).finally(() => {
|
|
if (this.data.onboardingVisible) this.setData({ savingDefaultSchedule: false })
|
|
})
|
|
},
|
|
|
|
finishCreated(autoSelect, scheduleSaved) {
|
|
if (this.data.applicationId && !this.data.applicationApproved) {
|
|
this.setData({ onboardingVisible: true })
|
|
return
|
|
}
|
|
const employee = this.data.createdEmployee
|
|
const eventChannel = this.getOpenerEventChannel && this.getOpenerEventChannel()
|
|
if (eventChannel && eventChannel.emit) {
|
|
eventChannel.emit('employeeCreated', { ...employee, autoSelect, scheduleSaved })
|
|
}
|
|
this.setData({ onboardingVisible: false })
|
|
wx.showToast({ title: scheduleSaved ? '排班已保存' : '美甲师已新增', icon: 'success' })
|
|
setTimeout(() => wx.navigateBack(), 300)
|
|
},
|
|
|
|
leaveAssociationForLater() {
|
|
const employee = this.data.createdEmployee
|
|
const eventChannel = this.getOpenerEventChannel && this.getOpenerEventChannel()
|
|
if (eventChannel && eventChannel.emit && employee) {
|
|
eventChannel.emit('employeeCreated', { ...employee, autoSelect: this.data.pickerMode, scheduleSaved: false })
|
|
}
|
|
wx.navigateBack()
|
|
}
|
|
})
|