298 lines
9.3 KiB
JavaScript
298 lines
9.3 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
|
|
}
|
|
|
|
function formatApplicationTime(value) {
|
|
if (!value) return ''
|
|
return String(value).replace('T', ' ').slice(0, 16)
|
|
}
|
|
|
|
function confirmModal(options) {
|
|
return new Promise(resolve => {
|
|
wx.showModal({ ...options, success: resolve, fail: () => resolve({ confirm: false }) })
|
|
})
|
|
}
|
|
|
|
Page({
|
|
data: {
|
|
employees: [],
|
|
loadError: '',
|
|
isManager: false,
|
|
reviewExpanded: false,
|
|
reviewApplications: [],
|
|
reviewPendingCount: 0,
|
|
reviewLoading: false,
|
|
reviewError: '',
|
|
reviewingId: '',
|
|
createModalVisible: false,
|
|
creating: false,
|
|
createForm: {
|
|
nickname: '',
|
|
phone: '',
|
|
account: '',
|
|
password: DEFAULT_EMPLOYEE_PASSWORD,
|
|
wechatId: '',
|
|
avatar: '',
|
|
avatarUrl: ''
|
|
},
|
|
avatarUploading: false
|
|
},
|
|
|
|
onLoad() {
|
|
const userInfo = getApp().globalData.userInfo || {}
|
|
this.setData({ isManager: Number(userInfo.type) === 2 })
|
|
this.loadEmployees()
|
|
if (Number(userInfo.type) === 2) this.loadReviewSummary()
|
|
},
|
|
|
|
onShow() {
|
|
const app = getApp()
|
|
if (!this.data.isManager || !app.globalData.openEmployeeReview) return
|
|
app.globalData.openEmployeeReview = false
|
|
this.setData({ reviewExpanded: true })
|
|
return this.loadReviewApplications()
|
|
},
|
|
|
|
async loadReviewSummary() {
|
|
if (!this.data.isManager) return
|
|
try {
|
|
const summary = await employeesApi.reviewSummary()
|
|
this.setData({ reviewPendingCount: Number(summary.pendingCount || 0) })
|
|
} catch (error) {
|
|
// 审核摘要失败不影响员工列表。
|
|
}
|
|
},
|
|
|
|
toggleReview() {
|
|
if (!this.data.isManager) return
|
|
const reviewExpanded = !this.data.reviewExpanded
|
|
this.setData({ reviewExpanded })
|
|
if (reviewExpanded) this.loadReviewApplications()
|
|
},
|
|
|
|
async loadReviewApplications() {
|
|
if (!this.data.isManager || this.data.reviewLoading) return
|
|
this.setData({ reviewLoading: true, reviewError: '' })
|
|
try {
|
|
const data = await employeesApi.reviewApplications()
|
|
const applications = (Array.isArray(data) ? data : []).map(item => ({
|
|
...item,
|
|
applicantNickname: item.applicantNickname || '待审核员工',
|
|
applicantAvatar: item.applicantAvatar || '',
|
|
applicantMaskedPhone: item.applicantMaskedPhone || '',
|
|
applicationTimeText: formatApplicationTime(item.createTime)
|
|
}))
|
|
this.setData({ reviewApplications: applications, reviewPendingCount: applications.length })
|
|
} catch (error) {
|
|
this.setData({ reviewError: error.message || '审核申请加载失败' })
|
|
} finally {
|
|
this.setData({ reviewLoading: false })
|
|
}
|
|
},
|
|
|
|
async approveApplication(event) {
|
|
const id = event.currentTarget.dataset.id
|
|
if (!id || this.data.reviewingId) return
|
|
const result = await confirmModal({ title: '通过员工申请', content: '确认允许该员工加入当前门店吗?' })
|
|
if (!result.confirm) return
|
|
await this.submitReview(id, () => employeesApi.approveApplication(id), '申请已通过')
|
|
},
|
|
|
|
async rejectApplication(event) {
|
|
const id = event.currentTarget.dataset.id
|
|
if (!id || this.data.reviewingId) return
|
|
const result = await confirmModal({
|
|
title: '驳回员工申请',
|
|
content: '',
|
|
editable: true,
|
|
placeholderText: '请输入驳回原因',
|
|
confirmText: '确认驳回'
|
|
})
|
|
if (!result.confirm) return
|
|
const reason = String(result.content || '').trim()
|
|
if (!reason) {
|
|
wx.showToast({ title: '请输入驳回原因', icon: 'none' })
|
|
return
|
|
}
|
|
await this.submitReview(id, () => employeesApi.rejectApplication(id, reason), '申请已驳回')
|
|
},
|
|
|
|
async submitReview(id, action, successMessage) {
|
|
this.setData({ reviewingId: String(id) })
|
|
try {
|
|
await action()
|
|
wx.showToast({ title: successMessage, icon: 'success' })
|
|
await Promise.all([this.loadReviewApplications(), this.loadEmployees()])
|
|
} catch (error) {
|
|
wx.showToast({ title: error.message || '申请状态已变化,请刷新重试', icon: 'none' })
|
|
await this.loadReviewApplications()
|
|
} finally {
|
|
this.setData({ reviewingId: '' })
|
|
}
|
|
},
|
|
|
|
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
|
|
}
|
|
if (!/^1[3-9]\d{9}$/.test(phone)) {
|
|
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 })
|
|
}
|
|
}
|
|
})
|