Files
JayuCloud/JC-Salon-WeChat/pages/shop-settings/index.js
T
xiaowang 385c4b06ec
自动发布开发环境 Nacos 配置 / publish (push) Successful in 25s
环境分支晋级检查 / environment-promotion-source (pull_request_target) Successful in 5s
fix(wechat): 协议页切后台暂停超时计时并全量映射技术文案
2026-11-05 11:32:29 +08:00

177 lines
7.1 KiB
JavaScript

const { BRAND_CONFIG } = require('../../utils/config')
const profileCache = require('../../utils/profile-cache')
const shopApi = require('../../api/shop')
const { showErrorToast, toUserMessage } = require('../../utils/toast')
function resolveNameStatus(value) {
const status = String(value || '').toUpperCase()
if (status === 'PENDING' || status === 'RETRY') return '名称审核中'
if (status === 'REJECTED') return '名称审核未通过'
if (status === 'APPROVED') return '名称已通过审核'
return '尚未提交审核'
}
function resolveBusinessHours(value) {
const parts = String(value || '').split('-')
const normalizeTime = value => {
const match = /^(\d{1,2}):(\d{2})(?::\d{2})?$/.exec(String(value || '').trim())
if (!match) return ''
const hour = Number(match[1])
const minute = Number(match[2])
if (hour > 23 || minute > 59) return ''
return `${String(hour).padStart(2, '0')}:${String(minute).padStart(2, '0')}`
}
const start = normalizeTime(parts[0])
const end = normalizeTime(parts[1])
if (!start || !end || start >= end) return null
return { start, end }
}
const WEEKDAYS = ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
function normalizeWeeklyHours(weeklyHours, legacyHours) {
const legacy = resolveBusinessHours(legacyHours) || { start: '10:00', end: '22:00' }
const source = Array.isArray(weeklyHours) && weeklyHours.length === 7
? weeklyHours
: WEEKDAYS.map((_, index) => ({ dayOfWeek: index + 1, open: true, startTime: legacy.start, endTime: legacy.end }))
const byDay = new Map(source.map(item => [Number(item.dayOfWeek), item]))
return WEEKDAYS.map((dayName, index) => {
const day = byDay.get(index + 1) || {}
const open = day.open !== false
return {
dayOfWeek: index + 1,
dayName,
open,
startTime: resolveBusinessHours(`${day.startTime || legacy.start}-${day.endTime || legacy.end}`)?.start || legacy.start,
endTime: resolveBusinessHours(`${day.startTime || legacy.start}-${day.endTime || legacy.end}`)?.end || legacy.end
}
})
}
Page({
data: {
profile: {},
businessExpireDateText: '未设置',
businessExpiryStatusText: '有效期未设置',
nameModerationText: '尚未提交审核',
settingsLoading: false,
settingsSaving: false,
businessHoursConfigured: false,
nameSaving: false,
settingsError: '',
form: { shopName: BRAND_CONFIG.defaultShopName, pendingShopName: '', weeklyHours: normalizeWeeklyHours(), defaultDuration: 60 }
},
onShow() { this.loadProfileAndSettings() },
async loadProfileAndSettings(options = {}) {
this.setData({ settingsLoading: true, settingsError: '' })
try {
const [profile, settings] = await Promise.all([
profileCache.getProfile(options),
shopApi.appointmentSettings()
])
const businessExpireDateText = profile.shopBusinessExpireTime ? String(profile.shopBusinessExpireTime).slice(0, 10) : '未设置'
const remainingDays = profile.shopBusinessRemainingDays
const hasRemainingDays = remainingDays !== null && remainingDays !== undefined && remainingDays !== '' && Number.isFinite(Number(remainingDays))
const businessExpiryStatusText = profile.shopBusinessExpired
? '已过期,暂不能新增预约'
: hasRemainingDays
? `剩余 ${remainingDays} 天`
: '有效期未设置'
const weeklyHours = normalizeWeeklyHours(settings.weeklyHours, settings.businessHours)
this.setData({
profile,
businessHoursConfigured: weeklyHours.some(day => day.open),
businessExpireDateText,
businessExpiryStatusText,
nameModerationText: resolveNameStatus(settings.shopNameModerationStatus),
form: {
...this.data.form,
shopName: settings.pendingShopName || settings.shopName || profile.shopName || BRAND_CONFIG.defaultShopName,
pendingShopName: settings.pendingShopName || '',
weeklyHours,
defaultDuration: Number(settings.defaultAppointmentDurationMinutes) || 60
}
})
} catch (error) {
this.setData({
settingsError: toUserMessage(error, '设置加载失败'),
businessHoursConfigured: false,
'form.weeklyHours': []
})
} finally {
this.setData({ settingsLoading: false })
}
},
onInput(event) { this.setData({ [`form.${event.currentTarget.dataset.field}`]: event.detail.value }) },
toggleBusinessDay(event) {
const index = Number(event.currentTarget.dataset.index)
const weeklyHours = (this.data.form.weeklyHours || []).map((item, itemIndex) => itemIndex === index ? { ...item, open: Boolean(event.detail.value) } : item)
this.setData({ 'form.weeklyHours': weeklyHours })
},
changeBusinessTime(event) {
const index = Number(event.currentTarget.dataset.index)
const field = event.currentTarget.dataset.field
const weeklyHours = (this.data.form.weeklyHours || []).map((item, itemIndex) => itemIndex === index ? { ...item, [field]: event.detail.value } : item)
this.setData({ 'form.weeklyHours': weeklyHours })
},
async saveAppointmentSettings() {
const { weeklyHours, defaultDuration } = this.data.form
const duration = Number(defaultDuration)
const normalizedHours = normalizeWeeklyHours(weeklyHours)
if (normalizedHours.some(day => day.open && !resolveBusinessHours(`${day.startTime}-${day.endTime}`))) {
wx.showToast({ title: '请检查营业日的开始和结束时间', icon: 'none' })
return
}
if (!Number.isInteger(duration) || duration < 15 || duration > 720) {
wx.showToast({ title: '默认时长需在15至720分钟之间', icon: 'none' })
return
}
this.setData({ settingsSaving: true })
try {
await shopApi.saveAppointmentSettings({
weeklyHours: normalizedHours.map(({ dayOfWeek, open, startTime, endTime }) => ({ dayOfWeek, open, startTime: open ? startTime : null, endTime: open ? endTime : null })),
defaultAppointmentDurationMinutes: duration
})
this.setData({ businessHoursConfigured: normalizedHours.some(day => day.open) })
wx.showToast({ title: '预约设置已生效', icon: 'success' })
} catch (error) {
showErrorToast(error, '保存失败,请重试')
} finally {
this.setData({ settingsSaving: false })
}
},
async submitShopName() {
const shopName = String(this.data.form.shopName || '').trim()
if (!shopName) {
wx.showToast({ title: '请输入店铺名称', icon: 'none' })
return
}
this.setData({ nameSaving: true })
try {
await shopApi.updateName(shopName)
wx.showToast({ title: '店铺名称已提交审核', icon: 'success' })
await this.loadProfileAndSettings({ force: true })
} catch (error) {
showErrorToast(error, '提交审核失败')
} finally {
this.setData({ nameSaving: false })
}
},
explainDefaultDuration() {
wx.showModal({
title: '默认时长',
content: '用于新建预约表单的初始值和未设置款式时的后备时长。已选款式的时长或你在预约中修改的时长优先。',
showCancel: false,
confirmText: '知道了'
})
},
back() { wx.navigateBack() }
})