优化新建预约时间选择

This commit is contained in:
2026-06-27 16:12:00 +08:00
parent c671ec264c
commit 72fcb123d4
3 changed files with 388 additions and 23 deletions
+220 -17
View File
@@ -6,6 +6,7 @@ const { markPlanningRefresh } = require('../../utils/planning-refresh')
const { chooseImageFilePath, uploadObjectFile } = require('../../utils/upload')
const DRAFT_STORAGE_PREFIX = 'managerAppointmentDraft'
const TIME_SLOT_MINUTES = 15
function buildClientRequestId(prefix) {
return `${prefix}-${Date.now()}-${Math.random().toString(36).slice(2, 10)}`
@@ -68,6 +69,111 @@ function normalizeDuration(value) {
return Number.isFinite(duration) && duration > 0 ? Math.floor(duration) : 0
}
function buildTimeRangeLabel(startMinute, durationMinutes) {
return `${dateUtil.timeFromMinutes(startMinute)}-${dateUtil.timeFromMinutes(startMinute + durationMinutes)}`
}
function getTimePeriod(minute) {
if (minute < 12 * 60) {
return { key: 'morning', title: '上午' }
}
if (minute < 18 * 60) {
return { key: 'afternoon', title: '下午' }
}
return { key: 'evening', title: '晚上' }
}
function normalizeAppointmentDate(item) {
return item.date || String(item.startTime || '').slice(0, 10)
}
function extractTimeText(value) {
const target = String(value || '')
if (target.includes('T')) {
return target.slice(11, 16)
}
if (target.includes(' ')) {
return target.slice(11, 16)
}
return target.slice(0, 5)
}
function resolveAppointmentStartMinute(item) {
if (item.startMinute !== undefined && item.startMinute !== null) {
return Number(item.startMinute)
}
return dateUtil.minutesOfDay(extractTimeText(item.startTime))
}
function resolveAppointmentEndMinute(item, startMinute) {
if (item.endMinute !== undefined && item.endMinute !== null) {
return Number(item.endMinute)
}
if (item.endTime) {
return dateUtil.minutesOfDay(extractTimeText(item.endTime))
}
return startMinute + Number(item.durationMinutes || 60)
}
function isCancelledAppointment(item) {
return item.status === 3 || item.statusText === '已取消'
}
function buildBusyRanges(appointments, form) {
return (appointments || [])
.filter(item => String(item.artistId) === String(form.artistId))
.filter(item => normalizeAppointmentDate(item) === form.appointmentDate)
.filter(item => !isCancelledAppointment(item))
.map(item => {
const startMinute = resolveAppointmentStartMinute(item)
return {
startMinute,
endMinute: resolveAppointmentEndMinute(item, startMinute)
}
})
}
function hasBusyConflict(startMinute, endMinute, ranges) {
return ranges.some(range => startMinute < range.endMinute && endMinute > range.startMinute)
}
function buildAvailableTimeGroups(calendar, form, durationMinutes) {
const schedules = (calendar.schedules || [])
.filter(item => String(item.artistId) === String(form.artistId))
.filter(item => item.date === form.appointmentDate)
.filter(item => item.status === 1 && Number(item.capacity === undefined ? 1 : item.capacity) >= 1)
const busyRanges = buildBusyRanges(calendar.appointments || [], form)
const groupMap = {
morning: { key: 'morning', title: '上午', times: [] },
afternoon: { key: 'afternoon', title: '下午', times: [] },
evening: { key: 'evening', title: '晚上', times: [] }
}
schedules.forEach(schedule => {
const scheduleStart = dateUtil.minutesOfDay(extractTimeText(schedule.startTime))
const scheduleEnd = dateUtil.minutesOfDay(extractTimeText(schedule.endTime))
for (let minute = scheduleStart; minute + durationMinutes <= scheduleEnd; minute += TIME_SLOT_MINUTES) {
const endMinute = minute + durationMinutes
if (!hasBusyConflict(minute, endMinute, busyRanges)) {
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],
times: groupMap[key].times.filter((item, index, array) => array.findIndex(candidate => candidate.value === item.value) === index)
}))
.filter(group => group.times.length > 0)
}
function flattenTimeGroups(groups) {
return (groups || []).reduce((list, group) => list.concat(group.times || []), [])
}
function parseProductTags(product) {
if (Array.isArray(product.tagList) && product.tagList.length > 0) {
return product.tagList
@@ -105,11 +211,11 @@ function isScheduleCoveringTime(schedule, form, startMinute, endMinute) {
if (!schedule || String(schedule.artistId) !== String(form.artistId) || schedule.date !== form.appointmentDate) {
return false
}
if (schedule.status !== 1 || Number(schedule.capacity || 0) < 1) {
if (schedule.status !== 1 || Number(schedule.capacity === undefined ? 1 : schedule.capacity) < 1) {
return false
}
const scheduleStartMinute = dateUtil.minutesOfDay(String(schedule.startTime || '').slice(0, 5))
const scheduleEndMinute = dateUtil.minutesOfDay(String(schedule.endTime || '').slice(0, 5))
const scheduleStartMinute = dateUtil.minutesOfDay(extractTimeText(schedule.startTime))
const scheduleEndMinute = dateUtil.minutesOfDay(extractTimeText(schedule.endTime))
return scheduleStartMinute <= startMinute && scheduleEndMinute >= endMinute
}
@@ -132,7 +238,7 @@ Page({
customerName: '',
phone: '',
appointmentDate: dateUtil.formatDate(new Date()),
appointmentTime: '13:00',
appointmentTime: '',
productId: '',
manualProductTitle: '',
manualProductPrice: '',
@@ -163,6 +269,11 @@ Page({
canCreateAppointment: true,
draftSavedAtText: '',
manualImageUploading: false,
timeSheetVisible: false,
timeLoading: false,
timeError: '',
timeSelectTip: '请先选择美甲师并填写时长',
availableTimeGroups: [],
loading: false
},
@@ -204,6 +315,7 @@ Page({
}
await this.loadArtists()
this.refreshProductCards()
await this.refreshAvailableTimes()
this.checkAvailability()
},
@@ -241,6 +353,10 @@ Page({
if (field === 'customerName' || field === 'phone') {
this.scheduleCustomerSearch(field)
}
if (field === 'durationMinutes') {
this.refreshAvailableTimes()
this.checkAvailability()
}
},
onCustomerFocus(event) {
@@ -305,13 +421,12 @@ Page({
},
async onDateChange(event) {
this.setData({ 'form.appointmentDate': event.detail.value })
this.setData({
'form.appointmentDate': event.detail.value,
'form.appointmentTime': ''
})
await this.loadArtists()
this.checkAvailability()
},
onTimeChange(event) {
this.setData({ 'form.appointmentTime': event.detail.value })
await this.refreshAvailableTimes()
this.checkAvailability()
},
@@ -360,6 +475,7 @@ Page({
'form.durationMinutes': String(parseInt(selectedProduct.approximateDuration, 10) || this.data.form.durationMinutes || '')
})
this.closeProductModal()
this.refreshAvailableTimes()
this.checkAvailability()
},
@@ -377,6 +493,7 @@ Page({
'form.manualProductPrice': String(price)
})
this.closeProductModal()
this.refreshAvailableTimes()
this.checkAvailability()
},
@@ -418,7 +535,93 @@ Page({
const selectedArtist = this.data.artists[Number(event.detail.value)] || {}
this.setData({
selectedArtist,
'form.artistId': selectedArtist.id
'form.artistId': selectedArtist.id,
'form.appointmentTime': ''
})
this.refreshAvailableTimes()
this.checkAvailability()
},
canOpenTimeSheet() {
const form = this.data.form
return Boolean(form.appointmentDate && form.artistId && normalizeDuration(form.durationMinutes))
},
async refreshAvailableTimes() {
const form = this.data.form
const durationMinutes = normalizeDuration(form.durationMinutes)
const requestSeq = (this._availableTimeRequestSeq || 0) + 1
this._availableTimeRequestSeq = requestSeq
if (!form.appointmentDate || !form.artistId || !durationMinutes) {
const patch = {
availableTimeGroups: [],
timeError: '',
timeSelectTip: '请先选择美甲师并填写时长'
}
if (form.appointmentTime) {
patch['form.appointmentTime'] = ''
}
this.setData(patch)
return
}
this.setData({
timeLoading: true,
timeError: '',
timeSelectTip: '正在计算可约时间...'
})
try {
const calendar = await planningApi.calendar({
startDate: form.appointmentDate,
endDate: form.appointmentDate,
artistId: form.artistId
})
if (requestSeq !== this._availableTimeRequestSeq) {
return
}
const availableTimeGroups = buildAvailableTimeGroups(calendar, form, durationMinutes)
const availableTimes = flattenTimeGroups(availableTimeGroups).map(item => item.value)
const patch = {
availableTimeGroups,
timeSelectTip: availableTimes.length > 0 ? '请选择预约时间' : '当前美甲师当天暂无可约时间'
}
if (form.appointmentTime && !availableTimes.includes(form.appointmentTime)) {
patch['form.appointmentTime'] = ''
}
this.setData(patch)
} catch (error) {
if (requestSeq !== this._availableTimeRequestSeq) {
return
}
this.setData({
availableTimeGroups: [],
timeError: error.message || '可约时间加载失败',
timeSelectTip: '可约时间加载失败'
})
} finally {
if (requestSeq === this._availableTimeRequestSeq) {
this.setData({ timeLoading: false })
}
}
},
async openTimeSheet() {
if (!this.canOpenTimeSheet()) {
wx.showToast({ title: '请先选择美甲师并填写时长', icon: 'none' })
return
}
this.setData({ timeSheetVisible: true })
await this.refreshAvailableTimes()
},
closeTimeSheet() {
this.setData({ timeSheetVisible: false })
},
selectAvailableTime(event) {
const value = event.currentTarget.dataset.value
this.setData({
'form.appointmentTime': value,
timeSheetVisible: false
})
this.checkAvailability()
},
@@ -454,16 +657,16 @@ Page({
return { ok: false, tip }
}
const conflicts = (calendar.appointments || []).filter(item => {
if (String(item.artistId) !== String(form.artistId) || item.date !== form.appointmentDate) return false
if (item.status === 3 || item.statusText === '已取消') return false
const itemStart = Number(item.startMinute || dateUtil.minutesOfDay((item.startTime || '').slice(11, 16)))
const itemEnd = itemStart + Number(item.durationMinutes || 60)
if (String(item.artistId) !== String(form.artistId) || normalizeAppointmentDate(item) !== form.appointmentDate) return false
if (isCancelledAppointment(item)) return false
const itemStart = resolveAppointmentStartMinute(item)
const itemEnd = resolveAppointmentEndMinute(item, itemStart)
return startMinute < itemEnd && endMinute > itemStart
})
if (conflicts.length > 0) {
const first = conflicts[0]
const itemStart = Number(first.startMinute || dateUtil.minutesOfDay((first.startTime || '').slice(11, 16)))
const itemEnd = itemStart + Number(first.durationMinutes || 60)
const itemStart = resolveAppointmentStartMinute(first)
const itemEnd = resolveAppointmentEndMinute(first, itemStart)
const tip = `${this.data.selectedArtist.name || '该美甲师'} ${dateUtil.timeFromMinutes(itemStart)}-${dateUtil.timeFromMinutes(itemEnd)} 已有预约`
this.setData({ conflictTip: tip, conflictLevel: 'danger' })
return { ok: false, tip }
@@ -30,12 +30,6 @@
<view class="field-value">{{form.appointmentDate}}</view>
</picker>
</view>
<view class="field">
<text class="field-label required">预约时间</text>
<picker mode="time" value="{{form.appointmentTime}}" start="10:00" end="22:00" bindchange="onTimeChange">
<view class="field-value">{{form.appointmentTime}}</view>
</picker>
</view>
<view class="field">
<text class="field-label required">预约作品</text>
<view class="field-value product-value" bindtap="openProductModal">
@@ -54,6 +48,16 @@
<view class="field-value">{{selectedArtist.name || '请选择美甲师'}}</view>
</picker>
</view>
<view class="field">
<text class="field-label required">预约时间</text>
<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>
</view>
<text class="time-arrow">›</text>
</view>
</view>
<view class="field textarea-field">
<text class="field-label">备注</text>
<textarea value="{{form.remark}}" data-field="remark" bindinput="onInput" placeholder="客户偏好、款式说明" />
@@ -125,6 +129,32 @@
</view>
</view>
<view wx:if="{{timeSheetVisible}}" class="modal-mask" bindtap="closeTimeSheet">
<view class="time-modal" catchtap="stopTap">
<view class="modal-head">
<view>
<text class="modal-title">选择预约时间</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>
<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>
<view class="time-grid">
<view wx:for="{{item.times}}" wx:for-item="time" wx:key="value" class="time-chip {{form.appointmentTime === time.value ? 'active' : ''}}" data-value="{{time.value}}" bindtap="selectAvailableTime">
<text>{{time.value}}</text>
<text>{{time.rangeText}}</text>
</view>
</view>
</view>
</scroll-view>
</view>
</view>
<view class="fixed-bottom">
<view wx:if="{{canCreateAppointment}}" class="action-row">
<button class="secondary-btn draft-btn" bindtap="saveDraft">保存草稿</button>
@@ -81,6 +81,41 @@
gap: 12rpx;
}
.time-value {
display: flex;
align-items: center;
justify-content: space-between;
gap: 16rpx;
}
.time-value > view {
display: flex;
min-width: 0;
flex: 1;
flex-direction: column;
gap: 6rpx;
}
.time-value.placeholder {
color: #aaa096;
}
.time-range {
overflow: hidden;
color: #9a9288;
font-size: 22rpx;
line-height: 1.35;
text-overflow: ellipsis;
white-space: nowrap;
}
.time-arrow {
flex: 0 0 auto;
color: #b78b63;
font-size: 38rpx;
line-height: 1;
}
.manual-badge {
flex: 0 0 auto;
padding: 4rpx 12rpx;
@@ -181,6 +216,15 @@
box-shadow: 0 -18rpx 40rpx rgba(41, 31, 23, 0.16);
}
.time-modal {
width: 100%;
max-height: 82vh;
padding: 28rpx 28rpx calc(28rpx + env(safe-area-inset-bottom));
border-radius: 28rpx 28rpx 0 0;
background: #fffaf4;
box-shadow: 0 -18rpx 40rpx rgba(41, 31, 23, 0.16);
}
.modal-head {
display: flex;
align-items: center;
@@ -199,6 +243,13 @@
font-weight: 900;
}
.modal-subtitle {
display: block;
margin-top: 8rpx;
color: #8d8278;
font-size: 24rpx;
}
.modal-close {
position: relative;
display: flex;
@@ -349,6 +400,87 @@
text-align: center;
}
.time-list {
max-height: 620rpx;
padding-right: 2rpx;
}
.time-group {
margin-bottom: 28rpx;
}
.time-group:last-child {
margin-bottom: 0;
}
.time-group-title {
display: block;
margin-bottom: 14rpx;
color: #4d453e;
font-size: 26rpx;
font-weight: 900;
}
.time-grid {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
gap: 14rpx;
}
.time-chip {
display: flex;
min-height: 88rpx;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 4rpx;
border: 2rpx solid #eadfd2;
border-radius: 18rpx;
background: #fff;
color: #302923;
box-shadow: 0 8rpx 18rpx rgba(92, 66, 43, 0.06);
transition: transform 0.16s ease, border-color 0.16s ease, background 0.16s ease;
}
.time-chip:active {
transform: scale(0.98);
}
.time-chip.active {
border-color: #c9874d;
background: #fff2df;
color: #9f5f2d;
}
.time-chip text:first-child {
font-size: 28rpx;
font-weight: 900;
line-height: 1.2;
}
.time-chip text:last-child {
color: #8c8177;
font-size: 20rpx;
line-height: 1.2;
}
.time-chip.active text:last-child {
color: #a26428;
}
.time-empty {
min-height: 220rpx;
display: flex;
align-items: center;
justify-content: center;
padding: 40rpx 20rpx;
border-radius: 18rpx;
background: #f7efe7;
color: #8c8177;
font-size: 26rpx;
text-align: center;
}
.manual-panel {
margin-top: 12rpx;
padding: 20rpx;