Files
JayuCloud/FD-WeChat-Fronted/admin/pages/schedules/index.js
T
2026-06-15 15:53:55 +08:00

188 lines
4.3 KiB
JavaScript

const scheduleApi = require('../../api/artist-schedule')
const navigation = require('../../utils/navigation')
const permission = require('../../utils/permission')
const format = require('../../utils/format')
const { markDirty } = require('../../utils/page-state')
function todayText(offset) {
const date = new Date()
date.setDate(date.getDate() + (offset || 0))
const month = String(date.getMonth() + 1).padStart(2, '0')
const day = String(date.getDate()).padStart(2, '0')
return `${date.getFullYear()}-${month}-${day}`
}
function safeList(value) {
return Array.isArray(value) ? value : []
}
Page({
data: {
loading: false,
list: [],
startDate: todayText(0),
endDate: todayText(6),
artistId: '',
canEdit: false,
form: {
artistId: '',
workDate: todayText(0),
startTime: '10:00',
endTime: '18:00',
slotMinutes: '60',
capacity: '1',
remark: ''
}
},
onShow() {
if (!navigation.ensureLogin('进入排班管理')) {
return
}
const permissionCodes = getApp().globalData.permissionCodes || []
this.setData({
canEdit: permission.hasPermission(permissionCodes, 'schedule:edit')
})
this.loadSchedules()
},
onPullDownRefresh() {
this.loadSchedules().finally(() => {
wx.stopPullDownRefresh()
})
},
buildQuery() {
const query = {}
;['artistId', 'startDate', 'endDate'].forEach(key => {
const value = String(this.data[key] || '').trim()
if (value) {
query[key] = value
}
})
return query
},
async loadSchedules() {
this.setData({ loading: true })
try {
const list = await scheduleApi.listSchedules(this.buildQuery())
this.setData({
list: safeList(list).map(item => this.formatSchedule(item))
})
} catch (error) {
wx.showToast({
title: error.message || '排班加载失败',
icon: 'none'
})
} finally {
this.setData({ loading: false })
}
},
formatSchedule(item) {
const startTime = String(item.startTime || '').slice(0, 5)
const endTime = String(item.endTime || '').slice(0, 5)
return {
...item,
displayDate: format.formatDate(item.workDate),
displayTime: `${startTime || '--'} - ${endTime || '--'}`,
statusText: Number(item.status) === 0 ? '停用' : '可预约'
}
},
handleArtistInput(event) {
this.setData({ artistId: event.detail.value })
},
handleStartDateChange(event) {
this.setData({ startDate: event.detail.value })
this.loadSchedules()
},
handleEndDateChange(event) {
this.setData({ endDate: event.detail.value })
this.loadSchedules()
},
handleSearch() {
this.loadSchedules()
},
handleFormInput(event) {
const field = event.currentTarget.dataset.field
if (!field) {
return
}
this.setData({
[`form.${field}`]: event.detail.value
})
},
handleFormDateChange(event) {
this.setData({
'form.workDate': event.detail.value
})
},
handleFormStartTimeChange(event) {
this.setData({
'form.startTime': event.detail.value
})
},
handleFormEndTimeChange(event) {
this.setData({
'form.endTime': event.detail.value
})
},
async handleSaveSchedule() {
if (!this.data.canEdit) {
wx.showToast({
title: '当前账号没有编辑排班权限',
icon: 'none'
})
return
}
const form = this.data.form
const artistId = String(form.artistId || '').trim()
if (!artistId) {
wx.showToast({
title: '请填写美甲师 ID',
icon: 'none'
})
return
}
try {
await scheduleApi.saveSchedule({
artistId,
workDate: form.workDate,
startTime: form.startTime,
endTime: form.endTime,
slotMinutes: Number(form.slotMinutes) || 60,
capacity: Number(form.capacity) || 1,
status: 1,
remark: String(form.remark || '').trim()
})
markDirty('admin-workbench')
wx.showToast({
title: '排班已保存',
icon: 'none'
})
this.loadSchedules()
} catch (error) {
wx.showToast({
title: error.message || '排班保存失败',
icon: 'none'
})
}
},
handleBack() {
navigation.goBack('/pages/manage/index')
}
})