const employeesApi = require('../../api/employees') const { chooseImageFilePath, uploadObjectFile } = require('../../utils/upload') const EMPLOYEE_COLORS = ['#7BA7C7', '#8FB89A', '#A894C7', '#D7A86E', '#D98DA5', '#79A6A3', '#B9A16B', '#9BA9C8'] Page({ data: { pickerMode: false, appointmentContext: { date: '', time: '', durationMinutes: '' }, 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 || '' } }) }, 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) { wx.showToast({ title: error.message || '头像上传失败', icon: 'none' }) } finally { this.setData({ avatarUploading: false }) } }, removeAvatar() { this.setData({ 'form.avatar': '', 'form.avatarUrl': '' }) }, onAvatarError() { this.setData({ 'form.avatarUrl': '' }) }, stopTouchMove() { }, async submit() { 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 }) await this.offerPendingAssociation() } catch (error) { wx.showToast({ title: error.message || '新增失败', icon: 'none' }) } finally { this.setData({ creating: false }) } }, 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) { wx.showToast({ title: error.message || '关联失败,请重新选择', icon: 'none' }) this.setData({ associationSelectedId: '' }) } finally { this.setData({ associationLoading: false }) } }, skipAssociation() { if (this.data.associationLoading) return this.setData({ associationSheetVisible: false, onboardingVisible: true }) }, setupFirstSchedule() { 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) 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: error.message || '未设置排班前,该美甲师暂不可预约。请重试或返回排班设置。', 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) { 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) } })