121 lines
3.1 KiB
JavaScript
121 lines
3.1 KiB
JavaScript
const employeesApi = require('../../api/employees')
|
|
const mock = require('../../utils/mock')
|
|
|
|
Page({
|
|
data: {
|
|
employees: [],
|
|
createModalVisible: false,
|
|
creating: false,
|
|
createForm: {
|
|
nickname: '',
|
|
phone: '',
|
|
account: '',
|
|
password: '888888',
|
|
wechatId: ''
|
|
}
|
|
},
|
|
|
|
onLoad() {
|
|
this.loadEmployees()
|
|
},
|
|
|
|
async loadEmployees() {
|
|
const fallback = mock.artists.map((item, index) => ({
|
|
...item,
|
|
monthOrders: 32 - index * 5,
|
|
monthAmount: 5800 - index * 760
|
|
}))
|
|
try {
|
|
const data = await employeesApi.list({ page: 1, size: 50 })
|
|
const list = data.list || data || []
|
|
this.setData({
|
|
employees: list.map((item, index) => ({
|
|
id: item.userId || item.id,
|
|
name: item.nickname || item.name || item.artistName || `美甲师${index + 1}`,
|
|
shortName: (item.nickname || item.name || '师').slice(-1),
|
|
color: mock.artists[index % mock.artists.length].color,
|
|
monthOrders: item.monthOrders || item.orderCount || 0,
|
|
monthAmount: item.monthAmount || item.turnover || 0
|
|
}))
|
|
})
|
|
} catch (error) {
|
|
this.setData({ employees: fallback })
|
|
}
|
|
},
|
|
|
|
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
|
|
const updates = {
|
|
[`createForm.${field}`]: value
|
|
}
|
|
if (field === 'phone' && !this.data.createForm.account) {
|
|
updates['createForm.account'] = value
|
|
}
|
|
this.setData(updates)
|
|
},
|
|
|
|
resetCreateForm() {
|
|
this.setData({
|
|
createForm: {
|
|
nickname: '',
|
|
phone: '',
|
|
account: '',
|
|
password: '888888',
|
|
wechatId: ''
|
|
}
|
|
})
|
|
},
|
|
|
|
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 || !phone || !account || !password || !wechatId) {
|
|
wx.showToast({ title: '请补全美甲师信息', icon: 'none' })
|
|
return
|
|
}
|
|
this.setData({ creating: true })
|
|
try {
|
|
await employeesApi.createUser({
|
|
nickname,
|
|
phone,
|
|
contactPhone: phone,
|
|
account,
|
|
password,
|
|
wechatId,
|
|
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 })
|
|
}
|
|
}
|
|
})
|