diff --git a/FM-Wechat-Fronted/pages/appointment-form/index.js b/FM-Wechat-Fronted/pages/appointment-form/index.js index 297b8c72..532588c0 100644 --- a/FM-Wechat-Fronted/pages/appointment-form/index.js +++ b/FM-Wechat-Fronted/pages/appointment-form/index.js @@ -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 } diff --git a/FM-Wechat-Fronted/pages/appointment-form/index.wxml b/FM-Wechat-Fronted/pages/appointment-form/index.wxml index 470a9f02..a725d8fe 100644 --- a/FM-Wechat-Fronted/pages/appointment-form/index.wxml +++ b/FM-Wechat-Fronted/pages/appointment-form/index.wxml @@ -30,12 +30,6 @@ {{form.appointmentDate}} - - 预约时间 - - {{form.appointmentTime}} - - 预约作品 @@ -54,6 +48,16 @@ {{selectedArtist.name || '请选择美甲师'}} + + 预约时间 + + + {{form.appointmentTime || timeSelectTip}} + {{form.appointmentDate}} · {{selectedArtist.name}} · {{form.durationMinutes}}分钟 + + › + + 备注