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

174 lines
4.6 KiB
JavaScript

const employeesApi = require('../../api/employees')
const systemParameterConfigApi = require('../../api/system-parameter-config')
const EMPLOYEE_COLORS = ['#7AA9C2', '#8EB99B', '#BDA7D8', '#D7B17D', '#D98D8D']
const EMPTY_FIELD_TEXT = '未填写'
function displayText(value) {
const text = String(value || '').trim()
return text || EMPTY_FIELD_TEXT
}
function buildRoleText(data) {
return Number(data.type) === 2 ? '店长' : '店员'
}
Page({
data: {
id: '',
employee: {},
loadError: '',
deleteConfirmVisible: false,
deletingEmployee: false,
resetConfirmVisible: false,
resettingPassword: false
},
onLoad(options) {
this.setData({ id: options.id || '' })
},
onShow() {
if (this.data.id) {
this.loadDetail()
}
},
async loadDetail() {
try {
const data = await employeesApi.detail(this.data.id)
if (!data) {
throw new Error('员工资料不存在')
}
const name = data.nickname || data.name || data.artistName || '美甲师'
const monthOrders = data.monthOrders || data.orderCount || 0
const monthAmount = data.monthAmount || data.turnover || 0
this.setData({
loadError: '',
employee: {
...data,
name,
shortName: name.slice(-1),
color: data.color || EMPLOYEE_COLORS[0],
roleText: buildRoleText(data),
accountText: displayText(data.account),
phoneText: displayText(data.phone),
contactPhoneText: displayText(data.contactPhone),
wechatIdText: displayText(data.wechatId),
identityText: displayText(data.identity),
monthOrders,
monthAmount,
avgAmount: monthOrders > 0 ? Math.round(monthAmount / monthOrders) : 0
}
})
} catch (error) {
this.setData({
employee: {},
loadError: error.message || '美甲师详情加载失败'
})
}
},
go(event) {
wx.navigateTo({ url: `${event.currentTarget.dataset.url}?id=${this.data.id}` })
},
requestDeleteEmployee() {
if (this.data.deletingEmployee) {
return
}
this.setData({ deleteConfirmVisible: true })
},
closeDeleteConfirm() {
if (this.data.deletingEmployee) {
return
}
this.setData({ deleteConfirmVisible: false })
},
stopTap() {
},
requestResetPassword() {
if (this.data.resettingPassword) {
return
}
this.setData({ resetConfirmVisible: true })
},
closeResetConfirm() {
if (this.data.resettingPassword) {
return
}
this.setData({ resetConfirmVisible: false })
},
async confirmResetPassword() {
if (this.data.resettingPassword || !this.data.id) {
return
}
this.setData({ resettingPassword: true })
try {
const success = await employeesApi.resetPassword(this.data.id)
if (!success) {
throw new Error('密码重置失败')
}
const result = await systemParameterConfigApi.getResetDefaultPassword()
const password = String(result && result.password || '').trim()
if (!password) {
throw new Error('默认密码为空')
}
this.setData({ resetConfirmVisible: false })
wx.showModal({
title: '密码已重置',
content: `系统默认密码:${password}`,
confirmText: '复制密码',
cancelText: '关闭',
success: (modalResult) => {
if (modalResult.confirm) {
wx.setClipboardData({
data: password,
success: () => wx.showToast({ title: '密码已复制', icon: 'success' })
})
}
}
})
} catch (error) {
wx.showToast({ title: error.message || '密码重置失败', icon: 'none' })
} finally {
this.setData({ resettingPassword: false })
}
},
async confirmDeleteEmployee() {
if (this.data.deletingEmployee) {
return
}
if (!this.data.id) {
wx.showToast({ title: '缺少员工 ID,无法删除', icon: 'none' })
return
}
this.setData({ deletingEmployee: true })
try {
const success = await employeesApi.deleteUser(this.data.id)
if (!success) {
throw new Error('员工不存在或已被删除')
}
this.setData({ deleteConfirmVisible: false })
wx.showToast({ title: '员工已删除', icon: 'success' })
setTimeout(() => {
wx.navigateBack()
}, 600)
} catch (error) {
wx.showToast({ title: error.message || '员工删除失败', icon: 'none' })
} finally {
this.setData({ deletingEmployee: false })
}
},
back() {
wx.navigateBack()
}
})