101 lines
2.4 KiB
JavaScript
101 lines
2.4 KiB
JavaScript
const employeesApi = require('../../api/employees')
|
|
|
|
const EMPLOYEE_COLORS = ['#7AA9C2', '#8EB99B', '#BDA7D8', '#D7B17D', '#D98D8D']
|
|
|
|
Page({
|
|
data: {
|
|
id: '',
|
|
employee: {},
|
|
loadError: '',
|
|
deleteConfirmVisible: false,
|
|
deletingEmployee: 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)
|
|
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],
|
|
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() {
|
|
},
|
|
|
|
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()
|
|
}
|
|
})
|