335 lines
12 KiB
JavaScript
335 lines
12 KiB
JavaScript
const employeesApi = require('../../api/employees')
|
||
const planningApi = require('../../api/planning')
|
||
const dateUtil = require('../../utils/date')
|
||
const { showErrorToast, toUserMessage } = require('../../utils/toast')
|
||
|
||
const EMPLOYEE_COLORS = ['#7AA9C2', '#8EB99B', '#BDA7D8', '#D7B17D', '#D98D8D']
|
||
|
||
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: '',
|
||
associationSheetVisible: false,
|
||
associationLoading: false,
|
||
associationError: '',
|
||
associationApplicationId: '',
|
||
associationArtists: [],
|
||
associationItems: [],
|
||
associationKeyword: '',
|
||
associationSelectedId: ''
|
||
},
|
||
|
||
onLoad() {
|
||
const userInfo = getApp().globalData.userInfo || {}
|
||
this.setData({ isManager: Number(userInfo.type) === 2 })
|
||
this.loadEmployees()
|
||
if (Number(userInfo.type) === 2) this.loadReviewSummary()
|
||
},
|
||
|
||
onShow() {
|
||
if (this._waitingForEmployeeCreate) {
|
||
this._waitingForEmployeeCreate = false
|
||
this.loadEmployees()
|
||
if (this.data.reviewExpanded) this.loadReviewApplications()
|
||
}
|
||
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,
|
||
applicantDisplayName: item.applicantNickname || '待审核美甲师',
|
||
applicantAvatar: item.applicantAvatar || '',
|
||
applicantMaskedPhone: item.applicantMaskedPhone || '',
|
||
applicationTimeText: formatApplicationTime(item.createTime)
|
||
}))
|
||
this.setData({ reviewApplications: applications, reviewPendingCount: applications.length })
|
||
} catch (error) {
|
||
this.setData({ reviewError: toUserMessage(error, '审核申请加载失败') })
|
||
} finally {
|
||
this.setData({ reviewLoading: false })
|
||
}
|
||
},
|
||
|
||
async approveApplication(event) {
|
||
const id = event.currentTarget.dataset.id
|
||
if (!id || this.data.reviewingId || this.data.associationLoading) return
|
||
this.setData({
|
||
associationApplicationId: String(id),
|
||
associationKeyword: '',
|
||
associationSelectedId: '',
|
||
associationSheetVisible: true
|
||
})
|
||
await this.loadApprovalArtists()
|
||
},
|
||
|
||
async loadApprovalArtists() {
|
||
const applicationId = this.data.associationApplicationId
|
||
if (!applicationId || this.data.associationLoading) return
|
||
this.setData({ associationLoading: true, associationError: '' })
|
||
try {
|
||
const application = this.data.reviewApplications.find(item => String(item.id) === String(applicationId))
|
||
const data = await employeesApi.unboundArtists(applicationId)
|
||
const artists = (Array.isArray(data) ? data : []).map(item => ({
|
||
id: item.userId || item.id,
|
||
name: item.nickname || item.name || '未命名美甲师',
|
||
avatar: item.avatar || ''
|
||
}))
|
||
const selectedId = application && application.targetArtistUserId
|
||
? String(application.targetArtistUserId)
|
||
: ''
|
||
this.setData({
|
||
associationArtists: artists,
|
||
associationSelectedId: selectedId,
|
||
associationItems: this.filterApprovalArtists(artists, this.data.associationKeyword)
|
||
})
|
||
if (!artists.length) {
|
||
this.setData({ associationSheetVisible: false })
|
||
const result = await confirmModal({
|
||
title: '需要先新建美甲师',
|
||
content: '当前门店没有未关联微信的美甲师档案。新建后将自动关联这条申请并通过审核。',
|
||
confirmText: '去新建',
|
||
cancelText: '稍后处理'
|
||
})
|
||
if (result.confirm) this.createEmployeeForApplication(applicationId, application && application.applicationNo)
|
||
}
|
||
} catch (error) {
|
||
this.setData({ associationError: toUserMessage(error, '未关联美甲师加载失败') })
|
||
} finally {
|
||
this.setData({ associationLoading: false })
|
||
}
|
||
},
|
||
|
||
filterApprovalArtists(artists, keywordValue) {
|
||
const keyword = String(keywordValue || '').trim().toLowerCase()
|
||
return (artists || [])
|
||
.filter(item => !keyword || String(item.name || '').toLowerCase().includes(keyword))
|
||
.map(item => ({
|
||
id: item.id,
|
||
title: item.name,
|
||
subtitle: '当前门店 · 尚未关联微信登录',
|
||
avatar: item.avatar,
|
||
badge: '可关联'
|
||
}))
|
||
},
|
||
|
||
onApprovalSearch(event) {
|
||
const associationKeyword = event.detail.value || ''
|
||
this.setData({
|
||
associationKeyword,
|
||
associationItems: this.filterApprovalArtists(this.data.associationArtists, associationKeyword)
|
||
})
|
||
},
|
||
|
||
closeApprovalAssociation() {
|
||
if (this.data.associationLoading || this.data.reviewingId) return
|
||
this.setData({ associationSheetVisible: false })
|
||
},
|
||
|
||
async selectApprovalArtist(event) {
|
||
const artist = event.detail && event.detail.item
|
||
const applicationId = this.data.associationApplicationId
|
||
const application = this.data.reviewApplications.find(item => String(item.id) === String(applicationId))
|
||
if (!artist || !application || this.data.reviewingId) return
|
||
this.setData({ associationSelectedId: String(artist.id) })
|
||
},
|
||
|
||
async confirmApprovalAssociation() {
|
||
const applicationId = this.data.associationApplicationId
|
||
const application = this.data.reviewApplications.find(item => String(item.id) === String(applicationId))
|
||
const artist = this.data.associationArtists.find(item => String(item.id) === String(this.data.associationSelectedId))
|
||
if (!artist || !application || this.data.reviewingId) {
|
||
wx.showToast({ title: '请选择要关联的美甲师', icon: 'none' })
|
||
return
|
||
}
|
||
|
||
const useApplicantNickname = application.applicantNickname
|
||
? await this.askProfileAdoption('昵称', application.applicantNickname)
|
||
: false
|
||
const useApplicantAvatar = application.applicantAvatar
|
||
? await this.askProfileAdoption('头像', '已通过内容审核的申请头像')
|
||
: false
|
||
const summary = [
|
||
`关联到:${artist.name || '所选美甲师'}`,
|
||
useApplicantNickname ? '采用申请昵称' : '保留门店姓名',
|
||
useApplicantAvatar ? '采用申请头像' : '保留门店头像'
|
||
].join('\n')
|
||
const confirmed = await confirmModal({ title: '确认关联并通过', content: summary, confirmText: '确认通过' })
|
||
if (!confirmed.confirm) return
|
||
|
||
this.setData({ associationSheetVisible: false })
|
||
await this.submitReview(applicationId, () => employeesApi.approveApplication(applicationId, {
|
||
targetArtistUserId: artist.id,
|
||
useApplicantNickname,
|
||
useApplicantAvatar
|
||
}), '申请已通过')
|
||
},
|
||
|
||
askProfileAdoption(field, value) {
|
||
return confirmModal({
|
||
title: `采用申请${field}?`,
|
||
content: field === '昵称' ? `申请昵称为“${value}”,采用后会替换门店员工姓名。` : '采用后会替换门店员工当前头像。',
|
||
confirmText: `采用申请${field}`,
|
||
cancelText: `保留门店${field}`
|
||
}).then(result => Boolean(result.confirm))
|
||
},
|
||
|
||
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) {
|
||
showErrorToast(error, '申请状态已变化,请刷新重试')
|
||
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: item.color || item.artistColor || (item.artist && item.artist.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: toUserMessage(error, '美甲师列表加载失败')
|
||
})
|
||
}
|
||
},
|
||
|
||
createEmployeeForApplication(applicationId, applicationNo) {
|
||
if (!applicationId) return
|
||
this._waitingForEmployeeCreate = true
|
||
wx.navigateTo({
|
||
url: `/pages/employee-create/index?applicationId=${encodeURIComponent(applicationId)}&applicationNo=${encodeURIComponent(applicationNo || '')}`,
|
||
fail: () => { this._waitingForEmployeeCreate = false }
|
||
})
|
||
},
|
||
|
||
onEmployeeAvatarError(event) {
|
||
const index = Number(event.currentTarget.dataset.index)
|
||
if (!Number.isInteger(index) || !this.data.employees[index]) return
|
||
this.setData({ [`employees[${index}].avatar`]: '' })
|
||
},
|
||
|
||
onReviewAvatarError(event) {
|
||
const index = Number(event.currentTarget.dataset.index)
|
||
if (!Number.isInteger(index) || !this.data.reviewApplications[index]) return
|
||
this.setData({ [`reviewApplications[${index}].applicantAvatar`]: '' })
|
||
},
|
||
|
||
openDetail(event) {
|
||
wx.navigateTo({ url: `/pages/employee-detail/index?id=${event.currentTarget.dataset.id}` })
|
||
},
|
||
|
||
addEmployee() {
|
||
wx.navigateTo({
|
||
url: '/pages/employee-create/index',
|
||
success: result => {
|
||
result.eventChannel.on('employeeCreated', () => this.loadEmployees())
|
||
}
|
||
})
|
||
}
|
||
})
|