const employeesApi = require('../../api/employees') const { chooseImageFilePath, uploadObjectFile } = require('../../utils/upload') const DEFAULT_EMPLOYEE_PASSWORD = 'Fingertip@123' Page({ data: { pickerMode: false, appointmentContext: { date: '', time: '', durationMinutes: '' }, onboardingVisible: false, createdEmployee: null, form: { nickname: '', phone: '', account: '', password: DEFAULT_EMPLOYEE_PASSWORD, wechatId: '', 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 const updates = { [`form.${field}`]: value } if (field === 'phone' && !this.data.form.account) updates['form.account'] = value this.setData(updates) }, 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 avatarUrl = uploadResult.fileUrl || uploadResult.primaryUrl || filePath this.setData({ 'form.avatar': avatarUrl, 'form.avatarUrl': avatarUrl }) } catch (error) { wx.showToast({ title: error.message || '头像上传失败', icon: 'none' }) } finally { this.setData({ avatarUploading: false }) } }, removeAvatar() { this.setData({ 'form.avatar': '', 'form.avatarUrl': '' }) }, stopTouchMove() { }, async submit() { const form = this.data.form const nickname = String(form.nickname || '').trim() const phone = String(form.phone || '').trim() const account = String(form.account || '').trim() const password = String(form.password || '').trim() const wechatId = String(form.wechatId || '').trim() if (!nickname || !phone || !account || !password || !wechatId) { 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, account, password, wechatId, 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, onboardingVisible: true }) } catch (error) { wx.showToast({ title: error.message || '新增失败', icon: 'none' }) } finally { this.setData({ creating: false }) } }, 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(true, true), 250) }) } }) }, skipFirstSchedule() { this.finishCreated(!this.data.pickerMode, 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) } })