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

187 lines
5.5 KiB
JavaScript

const employeesApi = require('../../api/employees')
const planningApi = require('../../api/planning')
const dateUtil = require('../../utils/date')
const { chooseImageFilePath, uploadObjectFile } = require('../../utils/upload')
const EMPLOYEE_COLORS = ['#7AA9C2', '#8EB99B', '#BDA7D8', '#D7B17D', '#D98D8D']
const DEFAULT_EMPLOYEE_PASSWORD = 'Fingertip@123'
function buildScheduleSummary(item) {
const mode = item.scheduleModeSummary || item.scheduleMode || '未设置'
const time = item.scheduleTimeSummary || item.ruleTimeSummary || ''
return time ? `${mode} ${time}` : mode
}
Page({
data: {
employees: [],
loadError: '',
createModalVisible: false,
creating: false,
createForm: {
nickname: '',
phone: '',
account: '',
password: DEFAULT_EMPLOYEE_PASSWORD,
wechatId: '',
avatar: '',
avatarUrl: ''
},
avatarUploading: false
},
onLoad() {
this.loadEmployees()
},
async loadEmployees() {
try {
const today = dateUtil.formatDate(new Date())
const [data, calendar] = await Promise.all([
employeesApi.list({ page: 1, size: 50 }),
planningApi.calendar({ startDate: today, endDate: today })
])
const list = data.list || data || []
const appointments = calendar.appointments || []
const artists = calendar.artists || []
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),
avatar: item.avatar || item.avatarUrl || '',
color: EMPLOYEE_COLORS[index % EMPLOYEE_COLORS.length],
monthOrders: item.monthOrders || item.orderCount || 0,
monthAmount: item.monthAmount || item.turnover || 0,
scheduleSummary: buildScheduleSummary(item),
todaySchedule: (() => {
const artist = artists.find(entry => String(entry.id) === String(item.userId || item.id))
return artist && artist.workStartTime && artist.workEndTime
? `${artist.workStartTime.slice(0, 5)}-${artist.workEndTime.slice(0, 5)}`
: '今日未排班'
})(),
todayAvailable: (() => {
const artist = artists.find(entry => String(entry.id) === String(item.userId || item.id))
return !!artist && Number(artist.status) !== 0
})(),
todayOrders: appointments.filter(entry => String(entry.artistId) === String(item.userId || item.id)).length
}))
})
} 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: '',
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 || !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,
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 })
}
}
})