Files
JayuCloud/FM-Wechat-Fronted/pages/employees/index.js
T

123 lines
3.2 KiB
JavaScript

const employeesApi = require('../../api/employees')
const EMPLOYEE_COLORS = ['#7AA9C2', '#8EB99B', '#BDA7D8', '#D7B17D', '#D98D8D']
const DEFAULT_EMPLOYEE_PASSWORD = 'Fingertip@123'
Page({
data: {
employees: [],
loadError: '',
createModalVisible: false,
creating: false,
createForm: {
nickname: '',
phone: '',
account: '',
password: DEFAULT_EMPLOYEE_PASSWORD,
wechatId: ''
}
},
onLoad() {
this.loadEmployees()
},
async loadEmployees() {
try {
const data = await employeesApi.list({ page: 1, size: 50 })
const list = data.list || data || []
this.setData({
loadError: '',
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: EMPLOYEE_COLORS[index % EMPLOYEE_COLORS.length],
monthOrders: item.monthOrders || item.orderCount || 0,
monthAmount: item.monthAmount || item.turnover || 0
}))
})
} catch (error) {
this.setData({
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
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: DEFAULT_EMPLOYEE_PASSWORD,
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 })
}
}
})