const employeesApi = require('../../api/employees') const { chooseImageFilePath, uploadObjectFile } = require('../../utils/upload') const EMPLOYEE_COLORS = ['#7AA9C2', '#8EB99B', '#BDA7D8', '#D7B17D', '#D98D8D'] const DEFAULT_EMPLOYEE_PASSWORD = 'JayuCloud@123' const PHONE_REGEX = /^1[3-9]\d{9}$/ function buildScheduleSummary(item) { const mode = item.scheduleModeSummary || item.scheduleMode || '未设置' const time = item.scheduleTimeSummary || item.ruleTimeSummary || '' return time ? `${mode} ${time}` : mode } function buildRoleText(item) { return Number(item.type) === 2 ? '店长' : '店员' } function toEmployeeRow(item, index) { const name = item.nickname || item.name || item.artistName || `美甲师${index + 1}` return { id: item.userId || item.id, name, shortName: name.slice(-1), avatar: item.avatar || item.avatarUrl || '', color: item.color || EMPLOYEE_COLORS[index % EMPLOYEE_COLORS.length], monthOrders: item.monthOrders || item.orderCount || 0, monthAmount: item.monthAmount || item.turnover || 0, scheduleSummary: buildScheduleSummary(item), roleText: buildRoleText(item), isManager: Number(item.type) === 2 } } Page({ data: { managers: [], employees: [], loadError: '', createModalVisible: false, creating: false, createForm: { nickname: '', phone: '', account: '', password: DEFAULT_EMPLOYEE_PASSWORD, wechatId: '', avatar: '', avatarUrl: '' }, avatarUploading: false }, onShow() { this.loadEmployees() }, async loadEmployees() { try { const data = await employeesApi.list({ page: 1, size: 50 }) const list = data.list || data || [] const rows = list.map(toEmployeeRow) this.setData({ loadError: '', managers: rows.filter(item => item.isManager), employees: rows.filter(item => !item.isManager) }) } catch (error) { this.setData({ managers: [], employees: [], loadError: error.message || '美甲师列表加载失败' }) } }, openDetail(event) { wx.navigateTo({ url: `/pages/employee-detail/index?id=${event.currentTarget.dataset.id}` }) }, addEmployee() { this.setData({ createModalVisible: true }) }, closeCreateModal() { if (this.data.creating) { return } this.setData({ createModalVisible: false }) }, stopTap() { }, onCreateInput(event) { const field = event.currentTarget.dataset.field const value = event.detail.value this.setData({ [`createForm.${field}`]: value }) }, resetCreateForm() { this.setData({ createForm: { nickname: '', phone: '', account: '', password: DEFAULT_EMPLOYEE_PASSWORD, wechatId: '', avatar: '', avatarUrl: '' } }) }, async chooseEmployeeAvatar() { 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({ 'createForm.avatar': avatarUrl, 'createForm.avatarUrl': avatarUrl }) } catch (error) { wx.showToast({ title: error.message || '头像上传失败', icon: 'none' }) } finally { this.setData({ avatarUploading: false }) } }, removeEmployeeAvatar() { this.setData({ 'createForm.avatar': '', 'createForm.avatarUrl': '' }) }, async submitEmployee() { const form = this.data.createForm 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 || !account || !password) { wx.showToast({ title: '请补全美甲师信息', icon: 'none' }) return } if (phone && !PHONE_REGEX.test(phone)) { wx.showToast({ title: '请输入 11 位手机号', icon: 'none' }) return } this.setData({ creating: true }) try { await employeesApi.createUser({ nickname, phone: phone || undefined, contactPhone: phone || undefined, account, password, wechatId: wechatId || undefined, avatar: form.avatar || undefined, type: 3 }) wx.showToast({ title: '美甲师已新增', icon: 'success' }) this.resetCreateForm() this.setData({ createModalVisible: false }) await this.loadEmployees() } catch (error) { wx.showToast({ title: error.message || '新增失败', icon: 'none' }) } finally { this.setData({ creating: false }) } } })