功能:支持待分配预约与首次排班引导

This commit is contained in:
2026-10-12 23:52:20 +08:00
parent 59608ef57f
commit 3ab615ccd4
26 changed files with 816 additions and 82 deletions
+92 -12
View File
@@ -170,6 +170,24 @@ function buildAvailableTimeGroups(calendar, form, durationMinutes) {
.filter(group => group.times.length > 0)
}
function buildUnassignedTimeGroups(durationMinutes) {
const groupMap = {
morning: { key: 'morning', title: '上午', times: [] },
afternoon: { key: 'afternoon', title: '下午', times: [] },
evening: { key: 'evening', title: '晚上', times: [] }
}
const businessStart = dateUtil.minutesOfDay('10:00')
const businessEnd = dateUtil.minutesOfDay('22:00')
for (let minute = businessStart; minute + durationMinutes <= businessEnd; minute += TIME_SLOT_MINUTES) {
const period = getTimePeriod(minute)
groupMap[period.key].times.push({
value: dateUtil.timeFromMinutes(minute),
rangeText: buildTimeRangeLabel(minute, durationMinutes)
})
}
return Object.keys(groupMap).map(key => groupMap[key]).filter(group => group.times.length > 0)
}
function flattenTimeGroups(groups) {
return (groups || []).reduce((list, group) => list.concat(group.times || []), [])
}
@@ -611,9 +629,13 @@ Page({
filterArtistItems(artists, keywordValue) {
const keyword = String(keywordValue || '').trim().toLowerCase()
return (artists || [])
const items = (artists || [])
.filter(item => !keyword || String(item.name || '').toLowerCase().includes(keyword))
.map(buildArtistSelectorItem)
if (!keyword || '暂不分配'.includes(keyword)) {
items.unshift({ id: '__unassigned__', title: '暂不分配', subtitle: '先记录预约,后续再分配员工', badge: '待分配' })
}
return items
},
openArtistSheet() {
@@ -641,6 +663,17 @@ Page({
selectArtist(event) {
const selectedArtist = event.detail.item || {}
if (selectedArtist.id === '__unassigned__') {
this.setData({
selectedArtist: {},
'form.artistId': '',
artistSheetVisible: false,
conflictTip: '',
conflictLevel: 'free'
})
this.refreshAvailableTimes()
return
}
this.setData({
selectedArtist,
'form.artistId': selectedArtist.id,
@@ -654,7 +687,7 @@ Page({
addEmployeeFromSelector() {
this.setData({ artistSheetVisible: false })
wx.navigateTo({
url: '/pages/employee-create/index?picker=1',
url: `/pages/employee-create/index?picker=1&date=${encodeURIComponent(this.data.form.appointmentDate || '')}&time=${encodeURIComponent(this.data.form.appointmentTime || '')}&durationMinutes=${encodeURIComponent(this.data.form.durationMinutes || '')}`,
success: result => {
result.eventChannel.on('employeeCreated', employee => this.handleEmployeeCreated(employee))
}
@@ -662,15 +695,34 @@ Page({
},
async handleEmployeeCreated(employee) {
await this.loadArtists(employee.id, employee)
this.setData({ 'form.appointmentTime': '' })
const originalTime = this.data.form.appointmentTime
await this.loadArtists(employee.autoSelect === false ? '' : employee.id, employee)
if (employee.autoSelect === false) {
this.setData({ selectedArtist: {}, 'form.artistId': '' })
await this.refreshAvailableTimes()
return
}
if (employee.scheduleSaved && originalTime) {
const availability = await this.checkAvailability()
if (!availability.ok) {
this.setData({
selectedArtist: {},
'form.artistId': '',
'form.appointmentTime': originalTime,
conflictTip: `${availability.tip || '新员工当前排班无法承接'},预约已保持待分配`,
conflictLevel: 'danger'
})
}
} else {
this.setData({ 'form.appointmentTime': '' })
}
await this.refreshAvailableTimes()
this.checkAvailability()
if (this.data.form.artistId) this.checkAvailability()
},
canOpenTimeSheet() {
const form = this.data.form
return Boolean(form.appointmentDate && form.artistId && normalizeDuration(form.durationMinutes))
return Boolean(form.appointmentDate && normalizeDuration(form.durationMinutes))
},
async refreshAvailableTimes() {
@@ -678,11 +730,11 @@ Page({
const durationMinutes = normalizeDuration(form.durationMinutes)
const requestSeq = (this._availableTimeRequestSeq || 0) + 1
this._availableTimeRequestSeq = requestSeq
if (!form.appointmentDate || !form.artistId || !durationMinutes) {
if (!form.appointmentDate || !durationMinutes) {
const patch = {
availableTimeGroups: [],
timeError: '',
timeSelectTip: '请先选择美甲师并填写时长'
timeSelectTip: '请先填写预约日期和时长'
}
if (form.appointmentTime) {
patch['form.appointmentTime'] = ''
@@ -690,6 +742,16 @@ Page({
this.setData(patch)
return
}
if (!form.artistId) {
const availableTimeGroups = buildUnassignedTimeGroups(durationMinutes)
this.setData({
availableTimeGroups,
timeError: '',
timeLoading: false,
timeSelectTip: '请选择预约时间,员工可稍后分配'
})
return
}
this.setData({
timeLoading: true,
timeError: '',
@@ -732,7 +794,7 @@ Page({
async openTimeSheet() {
if (!this.canOpenTimeSheet()) {
wx.showToast({ title: '请先选择美甲师并填写时长', icon: 'none' })
wx.showToast({ title: '请先填写预约日期和时长', icon: 'none' })
return
}
this.setData({ timeSheetVisible: true })
@@ -754,7 +816,7 @@ Page({
async checkAvailability() {
const form = this.data.form
if (!form.appointmentDate || !form.appointmentTime || !form.artistId) {
if (!form.appointmentDate || !form.appointmentTime) {
this.setData({ conflictTip: '', conflictLevel: 'free' })
return { ok: true }
}
@@ -770,6 +832,11 @@ Page({
this.setData({ conflictTip: tip, conflictLevel: 'danger' })
return { ok: false, tip }
}
if (!form.artistId) {
const tip = '预约将保存为待分配,当前不会占用员工时间'
this.setData({ conflictTip: tip, conflictLevel: 'free' })
return { ok: true, tip }
}
try {
const calendar = await planningApi.calendar({
startDate: form.appointmentDate,
@@ -815,7 +882,7 @@ Page({
const form = this.data.form
const hasManualProduct = String(form.manualProductTitle || '').trim() && String(form.manualProductPrice || '').trim()
const durationMinutes = normalizeDuration(form.durationMinutes)
if (!form.customerName || !form.phone || (!form.productId && !hasManualProduct) || !form.artistId) {
if (!form.customerName || !form.phone || (!form.productId && !hasManualProduct) || !form.appointmentDate || !form.appointmentTime) {
wx.showToast({ title: '请补全预约信息', icon: 'none' })
return
}
@@ -832,12 +899,25 @@ Page({
wx.showToast({ title: availability.tip || '该时段已被预约', icon: 'none' })
return
}
if (!form.artistId) {
const result = await new Promise(resolve => {
wx.showModal({
title: '保存为待分配预约',
content: '当前未指定员工,预约会正常保存,但不会占用员工时间。可以稍后在预约详情中分配。',
cancelText: '返回选择',
confirmText: '继续保存',
success: resolve,
fail: () => resolve({ confirm: false })
})
})
if (!result.confirm) return
}
this.setData({ loading: true })
try {
const payload = {
...form,
customerUserId: form.customerUserId ? String(form.customerUserId) : undefined,
artistId: String(form.artistId),
artistId: form.artistId ? String(form.artistId) : undefined,
durationMinutes,
clientRequestId: buildClientRequestId('manager-appointment')
}
@@ -30,9 +30,9 @@
<text class="unit">分钟</text>
</view>
<view class="field">
<text class="field-label required">指定员工</text>
<text class="field-label">员工(可稍后分配)</text>
<view class="field-value reference-value" bindtap="openArtistSheet">
<text>{{selectedArtist.name || '请选择员工'}}</text>
<text>{{selectedArtist.name || '暂不分配'}}</text>
<text class="reference-arrow">›</text>
</view>
</view>
@@ -41,7 +41,7 @@
<view class="field-value time-value {{form.appointmentTime ? '' : 'placeholder'}}" bindtap="openTimeSheet">
<view>
<text>{{form.appointmentTime || timeSelectTip}}</text>
<text wx:if="{{form.appointmentTime}}" class="time-range">{{form.appointmentDate}} · {{selectedArtist.name}} · {{form.durationMinutes}}分钟</text>
<text wx:if="{{form.appointmentTime}}" class="time-range">{{form.appointmentDate}} · {{selectedArtist.name || '待分配'}} · {{form.durationMinutes}}分钟</text>
</view>
<text class="time-arrow">›</text>
</view>
@@ -54,7 +54,7 @@
<view class="capacity-tip card">
<text class="tip-title">容量校验</text>
<text class="tip-text">提交预约时会校验营业时间、美甲师休息和同时间段容量,冲突会直接拦截。</text>
<text class="tip-text">已指定员工时会校验排班和冲突;暂不分配时只校验营业时间,后续分配时再完整校验。</text>
</view>
<view wx:if="{{draftSavedAtText}}" class="draft-tip card">
@@ -183,13 +183,13 @@
<view class="modal-head">
<view>
<text class="modal-title">选择预约时间</text>
<text class="modal-subtitle">{{form.appointmentDate}} · {{selectedArtist.name}} · {{form.durationMinutes}}分钟</text>
<text class="modal-subtitle">{{form.appointmentDate}} · {{selectedArtist.name || '待分配'}} · {{form.durationMinutes}}分钟</text>
</view>
<view class="modal-close" bindtap="closeTimeSheet">×</view>
</view>
<view wx:if="{{timeLoading}}" class="time-empty">正在计算可约时间...</view>
<view wx:elif="{{timeError}}" class="time-empty">{{timeError}}</view>
<view wx:elif="{{availableTimeGroups.length === 0}}" class="time-empty">当前美甲师当天暂无可约时间</view>
<view wx:elif="{{availableTimeGroups.length === 0}}" class="time-empty">当前没有可选时间</view>
<scroll-view wx:else class="time-list" scroll-y>
<view wx:for="{{availableTimeGroups}}" wx:key="key" class="time-group">
<text class="time-group-title">{{item.title}}</text>
@@ -0,0 +1,71 @@
const assert = require('assert')
const planningApi = require('../../api/planning')
let redirectedTo = ''
global.wx = {
getAccountInfoSync: () => ({ miniProgram: { envVersion: 'develop' } }),
getDeviceInfo: () => ({ platform: 'devtools' }),
removeStorageSync: () => {},
setStorageSync: () => {},
showToast: () => {},
showModal: ({ success }) => success({ confirm: true }),
redirectTo: ({ url }) => { redirectedTo = url }
}
global.getApp = () => ({ globalData: {} })
global.getCurrentPages = () => []
let definition
global.Page = value => { definition = value }
require('./index')
function createPage() {
return {
...definition,
data: JSON.parse(JSON.stringify(definition.data)),
setData(values) {
Object.entries(values).forEach(([key, value]) => {
const parts = key.split('.')
let target = this.data
for (let index = 0; index < parts.length - 1; index += 1) target = target[parts[index]]
target[parts[parts.length - 1]] = value
})
}
}
}
async function run() {
const page = createPage()
Object.assign(page.data.form, {
customerUserId: '31',
customerName: '测试客户',
phone: '13800138000',
productId: '41',
appointmentDate: '2026-10-13',
appointmentTime: '14:00',
durationMinutes: '60',
artistId: ''
})
await page.refreshAvailableTimes()
assert.strictEqual(page.data.availableTimeGroups.length > 0, true)
assert.strictEqual(page.data.timeSelectTip, '请选择预约时间,员工可稍后分配')
const availability = await page.checkAvailability()
assert.strictEqual(availability.ok, true)
assert.strictEqual(page.data.conflictTip.includes('待分配'), true)
let submittedPayload
planningApi.createAppointment = async payload => {
submittedPayload = payload
return { orderItemId: 61 }
}
await page.submit()
assert.strictEqual(submittedPayload.artistId, undefined)
assert.strictEqual(submittedPayload.appointmentTime, '14:00')
assert.strictEqual(redirectedTo, '/pages/appointment-detail/index?id=61')
}
run().catch(error => {
console.error(error)
process.exitCode = 1
})