重构店铺设置展示与编辑流程
This commit is contained in:
@@ -3,7 +3,6 @@ const tagsApi = require('../../api/tags')
|
||||
const dateUtil = require('../../utils/date')
|
||||
const { BRAND_CONFIG } = require('../../utils/config')
|
||||
const { markPlanningRefresh } = require('../../utils/planning-refresh')
|
||||
const { chooseImageFilePath, uploadObjectFile, resolveUploadedImagePreviewUrl } = require('../../utils/upload')
|
||||
|
||||
const PRESETS = [
|
||||
{ value: 'EVERYDAY', label: '全周营业', workDays: [1, 2, 3, 4, 5, 6, 7], startTime: '10:00', endTime: '22:00' },
|
||||
@@ -29,13 +28,6 @@ const EXCEPTION_TYPES = [
|
||||
]
|
||||
|
||||
const DEFAULT_SHOP_INTRO = '专注法式、晕染与手绘设计,适合想要温柔质感的客人。'
|
||||
const DEFAULT_TAG_STYLE = {
|
||||
textColor: '#333333',
|
||||
bgColor: '#FFFFFF',
|
||||
borderColor: '#E5E5E5',
|
||||
sortOrder: 100,
|
||||
frontVisible: 1
|
||||
}
|
||||
|
||||
function copyWorkDays(days) {
|
||||
if (Array.isArray(days)) {
|
||||
@@ -81,10 +73,6 @@ function resolveFirstImageUrl(urls) {
|
||||
return Array.isArray(urls) ? String(urls[0] || '').trim() : ''
|
||||
}
|
||||
|
||||
function introLength(value) {
|
||||
return String(value || '').length
|
||||
}
|
||||
|
||||
function normalizeTagOption(tag) {
|
||||
const name = String((tag && (tag.displayName || tag.tagName || tag.name)) || '').trim()
|
||||
if (!name) {
|
||||
@@ -95,18 +83,15 @@ function normalizeTagOption(tag) {
|
||||
name,
|
||||
tagName: (tag && tag.tagName) || name,
|
||||
displayName: (tag && tag.displayName) || name,
|
||||
textColor: (tag && tag.textColor) || DEFAULT_TAG_STYLE.textColor,
|
||||
bgColor: (tag && tag.bgColor) || DEFAULT_TAG_STYLE.bgColor,
|
||||
borderColor: (tag && tag.borderColor) || DEFAULT_TAG_STYLE.borderColor,
|
||||
sortOrder: (tag && tag.sortOrder) || DEFAULT_TAG_STYLE.sortOrder,
|
||||
frontVisible: (tag && tag.frontVisible) === 0 ? 0 : DEFAULT_TAG_STYLE.frontVisible
|
||||
textColor: (tag && tag.textColor) || '#333333',
|
||||
bgColor: (tag && tag.bgColor) || '#FFFFFF',
|
||||
borderColor: (tag && tag.borderColor) || '#E5E5E5'
|
||||
}
|
||||
}
|
||||
|
||||
function normalizeTagOptions(tags) {
|
||||
const source = Array.isArray(tags) ? tags : []
|
||||
const names = new Set()
|
||||
return source
|
||||
return (Array.isArray(tags) ? tags : [])
|
||||
.map(normalizeTagOption)
|
||||
.filter(Boolean)
|
||||
.filter(item => {
|
||||
@@ -118,60 +103,83 @@ function normalizeTagOptions(tags) {
|
||||
})
|
||||
}
|
||||
|
||||
function resolveCurrentShopId() {
|
||||
const app = getApp()
|
||||
const userInfo = (app && app.globalData && app.globalData.userInfo) || {}
|
||||
return userInfo.shopId || (app && app.globalData && app.globalData.shopId) || ''
|
||||
function buildWorkDaysText(days) {
|
||||
const selected = copyWorkDays(days)
|
||||
if (selected.length === 7) {
|
||||
return '周一至周日'
|
||||
}
|
||||
return selected.map(day => WEEKDAYS.find(item => item.value === day)).filter(Boolean).map(item => item.label).join('、') || '未设置营业日'
|
||||
}
|
||||
|
||||
function buildTagPayload(tagName, sourceTag) {
|
||||
const shopId = resolveCurrentShopId()
|
||||
const payload = {
|
||||
tagName,
|
||||
textColor: (sourceTag && sourceTag.textColor) || DEFAULT_TAG_STYLE.textColor,
|
||||
bgColor: (sourceTag && sourceTag.bgColor) || DEFAULT_TAG_STYLE.bgColor,
|
||||
borderColor: (sourceTag && sourceTag.borderColor) || DEFAULT_TAG_STYLE.borderColor,
|
||||
sortOrder: (sourceTag && sourceTag.sortOrder) || DEFAULT_TAG_STYLE.sortOrder,
|
||||
frontVisible: (sourceTag && sourceTag.frontVisible) === 0 ? 0 : DEFAULT_TAG_STYLE.frontVisible
|
||||
function buildShopPayload(settings, form) {
|
||||
const workDays = copyWorkDays(form.scheduleWorkDays)
|
||||
return {
|
||||
shopName: settings.shopName || settings.name || '',
|
||||
description: settings.description || '',
|
||||
address: settings.address || '',
|
||||
mapProvider: settings.mapProvider || '',
|
||||
longitude: settings.longitude,
|
||||
latitude: settings.latitude,
|
||||
businessHours: `${form.startTime}-${form.endTime}`,
|
||||
schedulePresetType: form.schedulePresetType,
|
||||
scheduleWorkDays: encodeWorkDays(workDays),
|
||||
scheduleRemark: String(form.scheduleRemark || '').trim(),
|
||||
coverImageFileId: String(settings.coverImageFileId || '').trim(),
|
||||
imageFileIds: String(settings.imageFileIds || '').trim(),
|
||||
themePrimaryColor: settings.themePrimaryColor || '',
|
||||
themeSecondaryColor: settings.themeSecondaryColor || '',
|
||||
themeAccentColor: settings.themeAccentColor || '',
|
||||
themePageBgColor: settings.themePageBgColor || '',
|
||||
themeCardBgColor: settings.themeCardBgColor || '',
|
||||
themeBackgroundImageFileId: settings.themeBackgroundImageFileId || '',
|
||||
status: settings.status,
|
||||
authorizationStartDate: settings.authorizationStartDate,
|
||||
authorizationDays: settings.authorizationDays,
|
||||
authorizationExpireTime: settings.authorizationExpireTime
|
||||
}
|
||||
if (shopId) {
|
||||
payload.shopId = shopId
|
||||
}
|
||||
|
||||
function resolveForm(settings) {
|
||||
const hours = String(settings.businessHours || '').split('-')
|
||||
const preset = buildPreset(settings.schedulePresetType || 'EVERYDAY')
|
||||
return {
|
||||
startTime: String(settings.startTime || hours[0] || preset.startTime).slice(0, 5),
|
||||
endTime: String(settings.endTime || hours[1] || preset.endTime).slice(0, 5),
|
||||
schedulePresetType: settings.schedulePresetType || preset.value,
|
||||
scheduleWorkDays: copyWorkDays(settings.scheduleWorkDays || preset.workDays),
|
||||
scheduleRemark: settings.scheduleRemark || ''
|
||||
}
|
||||
return payload
|
||||
}
|
||||
|
||||
Page({
|
||||
data: {
|
||||
presets: PRESETS,
|
||||
loading: false,
|
||||
loaded: false,
|
||||
saving: false,
|
||||
scheduleSheetVisible: false,
|
||||
activeScheduleTab: 'RULE',
|
||||
shopName: BRAND_CONFIG.defaultShopName,
|
||||
shopDescription: DEFAULT_SHOP_INTRO,
|
||||
coverImagePreviewUrl: '',
|
||||
tagList: [],
|
||||
tagCountText: '暂无标签',
|
||||
scheduleSummary: '营业时间未设置',
|
||||
scheduleRemarkText: '点击设置营业日和营业时间',
|
||||
exceptionCountText: '特殊日期 · 0 条',
|
||||
exceptionSummary: '暂无特殊日期',
|
||||
exceptions: [],
|
||||
settingsSnapshot: {},
|
||||
presetOptions: buildPresetOptions('EVERYDAY'),
|
||||
weekdays: WEEKDAYS,
|
||||
weekdayOptions: buildWeekdayOptions([1, 2, 3, 4, 5, 6, 7]),
|
||||
exceptionTypes: EXCEPTION_TYPES,
|
||||
exceptionTypeIndex: 0,
|
||||
exceptionTypeLabel: '休息',
|
||||
exceptions: [],
|
||||
loading: false,
|
||||
saving: false,
|
||||
tagsLoading: false,
|
||||
tagSaving: false,
|
||||
coverUploading: false,
|
||||
coverImagePreviewUrl: '',
|
||||
introLength: introLength(DEFAULT_SHOP_INTRO),
|
||||
tagList: [],
|
||||
tagForm: {
|
||||
id: '',
|
||||
name: ''
|
||||
},
|
||||
form: {
|
||||
shopName: BRAND_CONFIG.defaultShopName,
|
||||
startTime: '10:00',
|
||||
endTime: '22:00',
|
||||
defaultDuration: 60,
|
||||
rule: DEFAULT_SHOP_INTRO,
|
||||
schedulePresetType: 'EVERYDAY',
|
||||
scheduleWorkDays: [1, 2, 3, 4, 5, 6, 7],
|
||||
scheduleRemark: '',
|
||||
coverImageFileId: ''
|
||||
scheduleRemark: ''
|
||||
},
|
||||
exceptionForm: {
|
||||
workDate: dateUtil.formatDate(new Date()),
|
||||
@@ -186,15 +194,9 @@ Page({
|
||||
this.loadSettings()
|
||||
},
|
||||
|
||||
async loadTags() {
|
||||
this.setData({ tagsLoading: true })
|
||||
try {
|
||||
const tagsData = await tagsApi.listAll()
|
||||
this.setData({ tagList: normalizeTagOptions(tagsData.list || tagsData || []) })
|
||||
} catch (error) {
|
||||
wx.showToast({ title: error.message || '标签加载失败', icon: 'none' })
|
||||
} finally {
|
||||
this.setData({ tagsLoading: false })
|
||||
onShow() {
|
||||
if (this.data.loaded) {
|
||||
this.loadSettings()
|
||||
}
|
||||
},
|
||||
|
||||
@@ -206,32 +208,30 @@ Page({
|
||||
shopApi.scheduleExceptions(),
|
||||
tagsApi.listAll()
|
||||
])
|
||||
const hours = String(settings.businessHours || '').split('-')
|
||||
const preset = buildPreset(settings.schedulePresetType || 'EVERYDAY')
|
||||
const form = {
|
||||
...this.data.form,
|
||||
shopName: settings.shopName || settings.name || this.data.form.shopName,
|
||||
startTime: String(settings.startTime || hours[0] || preset.startTime).slice(0, 5),
|
||||
endTime: String(settings.endTime || hours[1] || preset.endTime).slice(0, 5),
|
||||
rule: settings.description || this.data.form.rule,
|
||||
schedulePresetType: settings.schedulePresetType || preset.value,
|
||||
scheduleWorkDays: copyWorkDays(settings.scheduleWorkDays || preset.workDays),
|
||||
scheduleRemark: settings.scheduleRemark || '',
|
||||
coverImageFileId: settings.coverImageFileId || ''
|
||||
}
|
||||
const form = resolveForm(settings || {})
|
||||
const exceptionList = (exceptions || []).map(item => ({
|
||||
...item,
|
||||
startTime: item.startTime ? String(item.startTime).slice(0, 5) : '',
|
||||
endTime: item.endTime ? String(item.endTime).slice(0, 5) : '',
|
||||
summary: buildExceptionSummary(item)
|
||||
}))
|
||||
const tagList = normalizeTagOptions(tagsData.list || tagsData || [])
|
||||
this.setData({
|
||||
form,
|
||||
introLength: introLength(form.rule),
|
||||
loaded: true,
|
||||
settingsSnapshot: settings || {},
|
||||
shopName: settings.shopName || settings.name || this.data.shopName,
|
||||
shopDescription: settings.description || DEFAULT_SHOP_INTRO,
|
||||
coverImagePreviewUrl: resolveFirstImageUrl(settings.coverImageUrls),
|
||||
tagList,
|
||||
tagCountText: tagList.length ? `${tagList.length} 个标签` : '暂无标签',
|
||||
scheduleSummary: `${buildWorkDaysText(form.scheduleWorkDays)} · ${form.startTime} - ${form.endTime}`,
|
||||
scheduleRemarkText: form.scheduleRemark || '全周营业规则',
|
||||
exceptionCountText: `特殊日期 · ${exceptionList.length} 条`,
|
||||
exceptionSummary: exceptionList[0] ? exceptionList[0].summary : '暂无特殊日期',
|
||||
exceptions: exceptionList,
|
||||
form,
|
||||
presetOptions: buildPresetOptions(form.schedulePresetType),
|
||||
weekdayOptions: buildWeekdayOptions(form.scheduleWorkDays),
|
||||
exceptions: (exceptions || []).map(item => ({
|
||||
...item,
|
||||
startTime: item.startTime ? String(item.startTime).slice(0, 5) : '',
|
||||
endTime: item.endTime ? String(item.endTime).slice(0, 5) : '',
|
||||
summary: buildExceptionSummary(item)
|
||||
})),
|
||||
tagList: normalizeTagOptions(tagsData.list || tagsData || [])
|
||||
weekdayOptions: buildWeekdayOptions(form.scheduleWorkDays)
|
||||
})
|
||||
} catch (error) {
|
||||
wx.showToast({ title: error.message || '店铺设置加载失败', icon: 'none' })
|
||||
@@ -240,146 +240,37 @@ Page({
|
||||
}
|
||||
},
|
||||
|
||||
editProfile() {
|
||||
wx.navigateTo({ url: '/pages/shop-profile-edit/index' })
|
||||
},
|
||||
|
||||
editTags() {
|
||||
wx.navigateTo({ url: '/pages/shop-tags/index' })
|
||||
},
|
||||
|
||||
openScheduleSheet(event) {
|
||||
this.setData({
|
||||
scheduleSheetVisible: true,
|
||||
activeScheduleTab: event.currentTarget.dataset.tab || 'RULE'
|
||||
})
|
||||
},
|
||||
|
||||
closeScheduleSheet() {
|
||||
if (this.data.saving) {
|
||||
return
|
||||
}
|
||||
this.setData({ scheduleSheetVisible: false })
|
||||
},
|
||||
|
||||
stopPropagation() {},
|
||||
|
||||
switchScheduleTab(event) {
|
||||
this.setData({ activeScheduleTab: event.currentTarget.dataset.tab || 'RULE' })
|
||||
},
|
||||
|
||||
onInput(event) {
|
||||
const field = event.currentTarget.dataset.field
|
||||
const value = event.detail.value
|
||||
const nextData = { [`form.${field}`]: value }
|
||||
if (field === 'rule') {
|
||||
nextData.introLength = introLength(value)
|
||||
}
|
||||
this.setData(nextData)
|
||||
},
|
||||
|
||||
onTagInput(event) {
|
||||
this.setData({ 'tagForm.name': event.detail.value })
|
||||
},
|
||||
|
||||
editTag(event) {
|
||||
const tagId = String(event.currentTarget.dataset.id || '')
|
||||
const tag = this.data.tagList.find(item => String(item.id) === tagId)
|
||||
if (!tag) {
|
||||
wx.showToast({ title: '标签不存在', icon: 'none' })
|
||||
return
|
||||
}
|
||||
this.setData({
|
||||
tagForm: {
|
||||
id: tag.id,
|
||||
name: tag.tagName || tag.name
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
clearTagForm() {
|
||||
this.setData({
|
||||
tagForm: {
|
||||
id: '',
|
||||
name: ''
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
async saveTag() {
|
||||
if (this.data.tagSaving) {
|
||||
return
|
||||
}
|
||||
const tagName = String(this.data.tagForm.name || '').trim()
|
||||
if (!tagName) {
|
||||
wx.showToast({ title: '请输入标签名称', icon: 'none' })
|
||||
return
|
||||
}
|
||||
const tagId = this.data.tagForm.id
|
||||
const editingTag = tagId
|
||||
? this.data.tagList.find(item => String(item.id) === String(tagId))
|
||||
: null
|
||||
this.setData({ tagSaving: true })
|
||||
try {
|
||||
if (tagId) {
|
||||
await tagsApi.update(tagId, buildTagPayload(tagName, editingTag))
|
||||
wx.showToast({ title: '标签已更新', icon: 'success' })
|
||||
} else {
|
||||
await tagsApi.create(buildTagPayload(tagName, null))
|
||||
wx.showToast({ title: '标签已新增', icon: 'success' })
|
||||
}
|
||||
this.clearTagForm()
|
||||
await this.loadTags()
|
||||
} catch (error) {
|
||||
wx.showToast({ title: error.message || '标签保存失败', icon: 'none' })
|
||||
} finally {
|
||||
this.setData({ tagSaving: false })
|
||||
}
|
||||
},
|
||||
|
||||
deleteTag(event) {
|
||||
const tagId = String(event.currentTarget.dataset.id || '')
|
||||
const tag = this.data.tagList.find(item => String(item.id) === tagId)
|
||||
if (!tag) {
|
||||
wx.showToast({ title: '标签不存在', icon: 'none' })
|
||||
return
|
||||
}
|
||||
wx.showModal({
|
||||
title: '删除标签',
|
||||
content: `确认删除标签“${tag.name}”?`,
|
||||
confirmText: '删除',
|
||||
confirmColor: '#d98d8d',
|
||||
success: async result => {
|
||||
if (!result.confirm) {
|
||||
return
|
||||
}
|
||||
this.setData({ tagSaving: true })
|
||||
try {
|
||||
await tagsApi.delete(tag.id)
|
||||
if (String(this.data.tagForm.id) === String(tag.id)) {
|
||||
this.clearTagForm()
|
||||
}
|
||||
await this.loadTags()
|
||||
wx.showToast({ title: '标签已删除', icon: 'success' })
|
||||
} catch (error) {
|
||||
wx.showToast({ title: error.message || '标签删除失败', icon: 'none' })
|
||||
} finally {
|
||||
this.setData({ tagSaving: false })
|
||||
}
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
async chooseCoverImage() {
|
||||
if (this.data.coverUploading || this.data.saving) {
|
||||
return
|
||||
}
|
||||
try {
|
||||
const filePath = await chooseImageFilePath()
|
||||
if (!filePath) {
|
||||
return
|
||||
}
|
||||
this.setData({ coverUploading: true })
|
||||
const uploadResult = await uploadObjectFile(filePath)
|
||||
const fileId = String(uploadResult && uploadResult.id ? uploadResult.id : '').trim()
|
||||
if (!fileId) {
|
||||
throw new Error('店铺主图上传失败:上传接口未返回文件ID')
|
||||
}
|
||||
this.setData({
|
||||
'form.coverImageFileId': fileId,
|
||||
coverImagePreviewUrl: resolveUploadedImagePreviewUrl(uploadResult, filePath)
|
||||
})
|
||||
} catch (error) {
|
||||
wx.showToast({ title: error.message || '店铺主图上传失败', icon: 'none' })
|
||||
} finally {
|
||||
this.setData({ coverUploading: false })
|
||||
}
|
||||
},
|
||||
|
||||
removeCoverImage() {
|
||||
if (this.data.coverUploading || this.data.saving) {
|
||||
return
|
||||
}
|
||||
this.setData({
|
||||
'form.coverImageFileId': '',
|
||||
coverImagePreviewUrl: ''
|
||||
})
|
||||
},
|
||||
|
||||
onExceptionInput(event) {
|
||||
this.setData({ [`exceptionForm.${event.currentTarget.dataset.field}`]: event.detail.value })
|
||||
this.setData({ [`form.${field}`]: event.detail.value })
|
||||
},
|
||||
|
||||
selectPreset(event) {
|
||||
@@ -401,8 +292,9 @@ Page({
|
||||
toggleWeekday(event) {
|
||||
const day = Number(event.currentTarget.dataset.value)
|
||||
const workDays = copyWorkDays(this.data.form.scheduleWorkDays)
|
||||
const exists = workDays.includes(day)
|
||||
const nextDays = exists ? workDays.filter(item => item !== day) : [...workDays, day].sort()
|
||||
const nextDays = workDays.includes(day)
|
||||
? workDays.filter(item => item !== day)
|
||||
: [...workDays, day].sort()
|
||||
this.setData({
|
||||
'form.schedulePresetType': 'CUSTOM',
|
||||
'form.scheduleWorkDays': nextDays,
|
||||
@@ -427,6 +319,10 @@ Page({
|
||||
})
|
||||
},
|
||||
|
||||
onExceptionInput(event) {
|
||||
this.setData({ [`exceptionForm.${event.currentTarget.dataset.field}`]: event.detail.value })
|
||||
},
|
||||
|
||||
onExceptionDateChange(event) {
|
||||
this.setData({ 'exceptionForm.workDate': event.detail.value })
|
||||
},
|
||||
@@ -449,31 +345,9 @@ Page({
|
||||
})
|
||||
},
|
||||
|
||||
explainDefaultDuration() {
|
||||
wx.showModal({
|
||||
title: '默认时长',
|
||||
content: '用于手动创建预约或未选择作品时的默认服务时长,会影响可约时间校验和预约结束时间估算。',
|
||||
showCancel: false,
|
||||
confirmText: '知道了'
|
||||
})
|
||||
},
|
||||
|
||||
explainShopIntro() {
|
||||
wx.showModal({
|
||||
title: '门店介绍',
|
||||
content: '这里会保存为门店介绍,并展示在用户端门店详情。预约时间、容量和冲突校验仍由店铺排班、特殊日期和员工排班控制。',
|
||||
showCancel: false,
|
||||
confirmText: '知道了'
|
||||
})
|
||||
},
|
||||
|
||||
async save() {
|
||||
async saveSchedule() {
|
||||
const form = this.data.form
|
||||
const workDays = copyWorkDays(form.scheduleWorkDays)
|
||||
if (!form.shopName) {
|
||||
wx.showToast({ title: '请填写店铺名称', icon: 'none' })
|
||||
return
|
||||
}
|
||||
if (!workDays.length) {
|
||||
wx.showToast({ title: '请选择营业日', icon: 'none' })
|
||||
return
|
||||
@@ -484,23 +358,13 @@ Page({
|
||||
}
|
||||
this.setData({ saving: true })
|
||||
try {
|
||||
await shopApi.saveSettings({
|
||||
shopName: form.shopName,
|
||||
name: form.shopName,
|
||||
startTime: form.startTime,
|
||||
endTime: form.endTime,
|
||||
businessHours: `${form.startTime}-${form.endTime}`,
|
||||
description: form.rule,
|
||||
schedulePresetType: form.schedulePresetType,
|
||||
scheduleWorkDays: encodeWorkDays(workDays),
|
||||
scheduleRemark: String(form.scheduleRemark || '').trim(),
|
||||
coverImageFileId: String(form.coverImageFileId || '').trim()
|
||||
})
|
||||
await shopApi.saveSettings(buildShopPayload(this.data.settingsSnapshot, form))
|
||||
markPlanningRefresh()
|
||||
wx.showToast({ title: '已保存', icon: 'success' })
|
||||
wx.showToast({ title: '排班已保存', icon: 'success' })
|
||||
this.setData({ scheduleSheetVisible: false })
|
||||
await this.loadSettings()
|
||||
} catch (error) {
|
||||
wx.showToast({ title: error.message || '保存失败', icon: 'none' })
|
||||
wx.showToast({ title: error.message || '排班保存失败', icon: 'none' })
|
||||
} finally {
|
||||
this.setData({ saving: false })
|
||||
}
|
||||
@@ -527,6 +391,7 @@ Page({
|
||||
})
|
||||
markPlanningRefresh()
|
||||
wx.showToast({ title: '特殊日期已保存', icon: 'success' })
|
||||
this.setData({ scheduleSheetVisible: false })
|
||||
await this.loadSettings()
|
||||
} catch (error) {
|
||||
wx.showToast({ title: error.message || '特殊日期保存失败', icon: 'none' })
|
||||
|
||||
Reference in New Issue
Block a user