const { BRAND_CONFIG } = require('../../utils/config') const profileCache = require('../../utils/profile-cache') const shopApi = require('../../api/shop') 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 } } Page({ data: { profile: {}, businessExpireDateText: '未设置', businessExpiryStatusText: '有效期未设置', nameModerationText: '尚未提交审核', settingsLoading: false, settingsSaving: false, businessHoursConfigured: false, nameSaving: false, settingsError: '', form: { shopName: BRAND_CONFIG.defaultShopName, pendingShopName: '', startTime: '', endTime: '', 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 businessHours = resolveBusinessHours(settings.businessHours) this.setData({ profile, businessHoursConfigured: Boolean(businessHours), businessExpireDateText, businessExpiryStatusText, nameModerationText: resolveNameStatus(settings.shopNameModerationStatus), form: { ...this.data.form, shopName: settings.pendingShopName || settings.shopName || profile.shopName || BRAND_CONFIG.defaultShopName, pendingShopName: settings.pendingShopName || '', startTime: businessHours ? businessHours.start : '', endTime: businessHours ? businessHours.end : '', defaultDuration: Number(settings.defaultAppointmentDurationMinutes) || 60 } }) } catch (error) { this.setData({ settingsError: error.message || '设置加载失败', businessHoursConfigured: false, 'form.startTime': '', 'form.endTime': '' }) } finally { this.setData({ settingsLoading: false }) } }, onInput(event) { this.setData({ [`form.${event.currentTarget.dataset.field}`]: event.detail.value }) }, onStartChange(event) { this.setData({ 'form.startTime': event.detail.value }) }, onEndChange(event) { this.setData({ 'form.endTime': event.detail.value }) }, async saveAppointmentSettings() { const { startTime, endTime, defaultDuration } = this.data.form const duration = Number(defaultDuration) const businessHours = resolveBusinessHours(`${startTime}-${endTime}`) if (!businessHours) { 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({ businessHours: `${businessHours.start}-${businessHours.end}`, defaultAppointmentDurationMinutes: duration }) this.setData({ businessHoursConfigured: true }) wx.showToast({ title: '预约设置已生效', icon: 'success' }) } catch (error) { wx.showToast({ title: error.message || '保存失败,请重试', icon: 'none' }) } 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) { wx.showToast({ title: error.message || '提交审核失败', icon: 'none' }) } finally { this.setData({ nameSaving: false }) } }, explainDefaultDuration() { wx.showModal({ title: '默认时长', content: '用于新建预约表单的初始值和未设置款式时的后备时长。已选款式的时长或你在预约中修改的时长优先。', showCancel: false, confirmText: '知道了' }) }, back() { wx.navigateBack() } })