Files
JayuCloud/JY-Salon-WeChat/pages/employees/index.js
T

238 lines
6.7 KiB
JavaScript

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) {
if (Number(item.type) === 2 || item.identity === '店长') {
return '店长'
}
return item.identity || item.roleName || '美甲师'
}
function buildStatusCode(item) {
return item.currentStatusCode || (Number(item.status) === 0 ? 'DISABLED' : 'NORMAL')
}
function buildStatusText(item, statusCode) {
return item.currentStatusText || (statusCode === 'DISABLED' ? '停用' : statusCode === 'REST' ? '休假' : '正常')
}
function toEmployeeRow(item, index) {
const name = item.nickname || item.name || item.artistName || `美甲师${index + 1}`
const statusCode = buildStatusCode(item)
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,
todayOrders: item.todayOrders || item.todayAppointments || item.todayAppointmentCount || 0,
avgAmount: item.avgAmount || 0,
scheduleSummary: buildScheduleSummary(item),
roleText: buildRoleText(item),
statusCode,
statusText: buildStatusText(item, statusCode),
statusClass: statusCode === 'DISABLED' ? 'disabled' : statusCode === 'REST' ? 'rest' : 'normal',
isManager: Number(item.type) === 2
}
}
function buildStatusFilters(rows) {
const countByStatus = rows.reduce((map, item) => {
map[item.statusCode] = (map[item.statusCode] || 0) + 1
return map
}, {})
return [
{ value: 'ALL', label: '全部', count: rows.length },
{ value: 'NORMAL', label: '正常', count: countByStatus.NORMAL || 0 },
{ value: 'REST', label: '休假', count: countByStatus.REST || 0 },
{ value: 'DISABLED', label: '停用', count: countByStatus.DISABLED || 0 }
]
}
function filterEmployees(rows, status) {
if (status === 'ALL') {
return rows
}
return rows.filter(item => item.statusCode === status)
}
Page({
data: {
allEmployees: [],
employees: [],
statusFilters: [
{ value: 'ALL', label: '全部', count: 0 },
{ value: 'NORMAL', label: '正常', count: 0 },
{ value: 'REST', label: '休假', count: 0 },
{ value: 'DISABLED', label: '停用', count: 0 }
],
activeStatus: 'ALL',
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: '',
allEmployees: rows,
statusFilters: buildStatusFilters(rows),
employees: filterEmployees(rows, this.data.activeStatus)
})
} catch (error) {
this.setData({
allEmployees: [],
employees: [],
statusFilters: buildStatusFilters([]),
loadError: error.message || '员工列表加载失败'
})
}
},
switchStatus(event) {
const status = event.currentTarget.dataset.status
this.setData({
activeStatus: status,
employees: filterEmployees(this.data.allEmployees, status)
})
},
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 })
}
}
})