优化店务小程序店铺设置UI
This commit is contained in:
@@ -3,6 +3,7 @@ const tagsApi = require('../../api/tags')
|
||||
const dateUtil = require('../../utils/date')
|
||||
const { BRAND_CONFIG } = require('../../utils/config')
|
||||
const { markPlanningRefresh } = require('../../utils/planning-refresh')
|
||||
const { chooseImageFilePaths, 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' },
|
||||
@@ -73,6 +74,34 @@ function resolveFirstImageUrl(urls) {
|
||||
return Array.isArray(urls) ? String(urls[0] || '').trim() : ''
|
||||
}
|
||||
|
||||
function normalizeImageUrls(urls) {
|
||||
return Array.isArray(urls) ? urls.map(url => String(url || '').trim()).filter(Boolean) : []
|
||||
}
|
||||
|
||||
function introLength(value) {
|
||||
return String(value || '').length
|
||||
}
|
||||
|
||||
function parseFileIds(value) {
|
||||
return String(value || '')
|
||||
.split(',')
|
||||
.map(item => String(item || '').trim())
|
||||
.filter(Boolean)
|
||||
}
|
||||
|
||||
function joinFileIds(fileIds) {
|
||||
return (Array.isArray(fileIds) ? fileIds : []).map(item => String(item || '').trim()).filter(Boolean).join(',')
|
||||
}
|
||||
|
||||
function normalizeGalleryItems(fileIds, urls) {
|
||||
const normalizedFileIds = parseFileIds(fileIds)
|
||||
const normalizedUrls = normalizeImageUrls(urls)
|
||||
return normalizedFileIds.map((fileId, index) => ({
|
||||
fileId,
|
||||
url: normalizedUrls[index] || ''
|
||||
}))
|
||||
}
|
||||
|
||||
function normalizeTagOption(tag) {
|
||||
const name = String((tag && (tag.displayName || tag.tagName || tag.name)) || '').trim()
|
||||
if (!name) {
|
||||
@@ -111,6 +140,25 @@ function buildWorkDaysText(days) {
|
||||
return selected.map(day => WEEKDAYS.find(item => item.value === day)).filter(Boolean).map(item => item.label).join('、') || '未设置营业日'
|
||||
}
|
||||
|
||||
function resolveBusinessStatus(settings) {
|
||||
if (Number(settings.status) === 0) {
|
||||
return {
|
||||
text: '已停用',
|
||||
className: 'closed'
|
||||
}
|
||||
}
|
||||
if (settings.authorizationExpired) {
|
||||
return {
|
||||
text: '授权到期',
|
||||
className: 'warning'
|
||||
}
|
||||
}
|
||||
return {
|
||||
text: '营业中',
|
||||
className: 'open'
|
||||
}
|
||||
}
|
||||
|
||||
function buildShopPayload(settings, form) {
|
||||
const workDays = copyWorkDays(form.scheduleWorkDays)
|
||||
return {
|
||||
@@ -139,6 +187,33 @@ function buildShopPayload(settings, form) {
|
||||
}
|
||||
}
|
||||
|
||||
function buildProfilePayload(settings, profileForm) {
|
||||
return {
|
||||
shopName: String(profileForm.shopName || '').trim(),
|
||||
description: String(profileForm.rule || '').trim(),
|
||||
address: String(profileForm.address || '').trim(),
|
||||
mapProvider: settings.mapProvider || '',
|
||||
longitude: settings.longitude,
|
||||
latitude: settings.latitude,
|
||||
businessHours: settings.businessHours || '',
|
||||
schedulePresetType: settings.schedulePresetType || '',
|
||||
scheduleWorkDays: settings.scheduleWorkDays || '',
|
||||
scheduleRemark: settings.scheduleRemark || '',
|
||||
coverImageFileId: String(profileForm.coverImageFileId || '').trim(),
|
||||
imageFileIds: joinFileIds(profileForm.galleryFileIds),
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
function resolveForm(settings) {
|
||||
const hours = String(settings.businessHours || '').split('-')
|
||||
const preset = buildPreset(settings.schedulePresetType || 'EVERYDAY')
|
||||
@@ -156,11 +231,23 @@ Page({
|
||||
loading: false,
|
||||
loaded: false,
|
||||
saving: false,
|
||||
coverUploading: false,
|
||||
galleryUploading: false,
|
||||
profileSheetVisible: false,
|
||||
activeProfileField: 'shopName',
|
||||
profileSheetTitle: '修改店铺名称',
|
||||
profileSheetSaveText: '保存名称',
|
||||
scheduleSheetVisible: false,
|
||||
activeScheduleTab: 'RULE',
|
||||
shopName: BRAND_CONFIG.defaultShopName,
|
||||
shopDescription: DEFAULT_SHOP_INTRO,
|
||||
shopAddress: '暂未填写地址',
|
||||
businessStatusText: '营业中',
|
||||
businessStatusClass: 'open',
|
||||
coverImagePreviewUrl: '',
|
||||
imageCountText: '暂无图片',
|
||||
galleryItems: [],
|
||||
introLength: introLength(DEFAULT_SHOP_INTRO),
|
||||
tagList: [],
|
||||
tagCountText: '暂无标签',
|
||||
scheduleSummary: '营业时间未设置',
|
||||
@@ -181,6 +268,13 @@ Page({
|
||||
scheduleWorkDays: [1, 2, 3, 4, 5, 6, 7],
|
||||
scheduleRemark: ''
|
||||
},
|
||||
profileForm: {
|
||||
shopName: BRAND_CONFIG.defaultShopName,
|
||||
rule: DEFAULT_SHOP_INTRO,
|
||||
address: '',
|
||||
galleryFileIds: [],
|
||||
coverImageFileId: ''
|
||||
},
|
||||
exceptionForm: {
|
||||
workDate: dateUtil.formatDate(new Date()),
|
||||
exceptionType: 'REST',
|
||||
@@ -209,6 +303,16 @@ Page({
|
||||
tagsApi.listAll()
|
||||
])
|
||||
const form = resolveForm(settings || {})
|
||||
const status = resolveBusinessStatus(settings || {})
|
||||
const imageUrls = normalizeImageUrls(settings.imageUrls)
|
||||
const galleryItems = normalizeGalleryItems(settings.imageFileIds, settings.imageUrls)
|
||||
const profileForm = {
|
||||
shopName: settings.shopName || settings.name || this.data.profileForm.shopName,
|
||||
rule: settings.description || DEFAULT_SHOP_INTRO,
|
||||
address: settings.address || '',
|
||||
galleryFileIds: galleryItems.map(item => item.fileId),
|
||||
coverImageFileId: settings.coverImageFileId || ''
|
||||
}
|
||||
const exceptionList = (exceptions || []).map(item => ({
|
||||
...item,
|
||||
startTime: item.startTime ? String(item.startTime).slice(0, 5) : '',
|
||||
@@ -221,7 +325,14 @@ Page({
|
||||
settingsSnapshot: settings || {},
|
||||
shopName: settings.shopName || settings.name || this.data.shopName,
|
||||
shopDescription: settings.description || DEFAULT_SHOP_INTRO,
|
||||
shopAddress: settings.address || '暂未填写地址',
|
||||
businessStatusText: status.text,
|
||||
businessStatusClass: status.className,
|
||||
coverImagePreviewUrl: resolveFirstImageUrl(settings.coverImageUrls),
|
||||
imageCountText: imageUrls.length ? `已上传 ${imageUrls.length} 张图片` : '暂无图片',
|
||||
galleryItems,
|
||||
profileForm,
|
||||
introLength: introLength(profileForm.rule),
|
||||
tagList,
|
||||
tagCountText: tagList.length ? `${tagList.length} 个标签` : '暂无标签',
|
||||
scheduleSummary: `${buildWorkDaysText(form.scheduleWorkDays)} · ${form.startTime} - ${form.endTime}`,
|
||||
@@ -240,8 +351,159 @@ Page({
|
||||
}
|
||||
},
|
||||
|
||||
editProfile() {
|
||||
wx.navigateTo({ url: '/pages/shop-profile-edit/index' })
|
||||
openProfileSheet(event) {
|
||||
this.openProfileSheetByField(event.currentTarget.dataset.field || 'shopName')
|
||||
},
|
||||
|
||||
openProfileSheetByField(field) {
|
||||
const titleMap = {
|
||||
shopName: '修改店铺名称',
|
||||
rule: '修改店铺简介',
|
||||
address: '修改店铺地址',
|
||||
cover: '修改店铺主图',
|
||||
gallery: '修改环境照片'
|
||||
}
|
||||
const saveTextMap = {
|
||||
shopName: '保存名称',
|
||||
rule: '保存简介',
|
||||
address: '保存地址',
|
||||
cover: '保存主图',
|
||||
gallery: '保存照片'
|
||||
}
|
||||
this.setData({
|
||||
profileSheetVisible: true,
|
||||
activeProfileField: titleMap[field] ? field : 'shopName',
|
||||
profileSheetTitle: titleMap[field] || titleMap.shopName,
|
||||
profileSheetSaveText: saveTextMap[field] || saveTextMap.shopName
|
||||
})
|
||||
},
|
||||
|
||||
closeProfileSheet() {
|
||||
if (this.data.saving || this.data.coverUploading || this.data.galleryUploading) {
|
||||
return
|
||||
}
|
||||
this.setData({ profileSheetVisible: false })
|
||||
},
|
||||
|
||||
onProfileInput(event) {
|
||||
const field = event.currentTarget.dataset.field
|
||||
const value = event.detail.value
|
||||
const nextData = { [`profileForm.${field}`]: value }
|
||||
if (field === 'rule') {
|
||||
nextData.introLength = introLength(value)
|
||||
}
|
||||
this.setData(nextData)
|
||||
},
|
||||
|
||||
async chooseCoverImage() {
|
||||
if (this.data.coverUploading || this.data.galleryUploading || 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({
|
||||
'profileForm.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.galleryUploading || this.data.saving) {
|
||||
return
|
||||
}
|
||||
this.setData({
|
||||
'profileForm.coverImageFileId': '',
|
||||
coverImagePreviewUrl: ''
|
||||
})
|
||||
},
|
||||
|
||||
async chooseGalleryImages() {
|
||||
if (this.data.galleryUploading || this.data.saving || this.data.coverUploading) {
|
||||
return
|
||||
}
|
||||
const remainingCount = 9 - this.data.galleryItems.length
|
||||
if (remainingCount <= 0) {
|
||||
wx.showToast({ title: '最多上传 9 张环境照片', icon: 'none' })
|
||||
return
|
||||
}
|
||||
try {
|
||||
const filePaths = await chooseImageFilePaths(remainingCount)
|
||||
if (!filePaths.length) {
|
||||
return
|
||||
}
|
||||
this.setData({ galleryUploading: true })
|
||||
const uploadedItems = []
|
||||
for (let index = 0; index < filePaths.length; index += 1) {
|
||||
const filePath = filePaths[index]
|
||||
const uploadResult = await uploadObjectFile(filePath)
|
||||
const fileId = String(uploadResult && uploadResult.id ? uploadResult.id : '').trim()
|
||||
if (!fileId) {
|
||||
throw new Error('环境照片上传失败:上传接口未返回文件ID')
|
||||
}
|
||||
uploadedItems.push({
|
||||
fileId,
|
||||
url: resolveUploadedImagePreviewUrl(uploadResult, filePath)
|
||||
})
|
||||
}
|
||||
const galleryItems = this.data.galleryItems.concat(uploadedItems)
|
||||
this.setData({
|
||||
galleryItems,
|
||||
'profileForm.galleryFileIds': galleryItems.map(item => item.fileId)
|
||||
})
|
||||
} catch (error) {
|
||||
wx.showToast({ title: error.message || '环境照片上传失败', icon: 'none' })
|
||||
} finally {
|
||||
this.setData({ galleryUploading: false })
|
||||
}
|
||||
},
|
||||
|
||||
removeGalleryImage(event) {
|
||||
if (this.data.galleryUploading || this.data.saving) {
|
||||
return
|
||||
}
|
||||
const index = Number(event.currentTarget.dataset.index)
|
||||
if (!Number.isInteger(index) || index < 0 || index >= this.data.galleryItems.length) {
|
||||
wx.showToast({ title: '环境照片不存在', icon: 'none' })
|
||||
return
|
||||
}
|
||||
const galleryItems = this.data.galleryItems.filter((item, itemIndex) => itemIndex !== index)
|
||||
this.setData({
|
||||
galleryItems,
|
||||
'profileForm.galleryFileIds': galleryItems.map(item => item.fileId)
|
||||
})
|
||||
},
|
||||
|
||||
async saveProfileField() {
|
||||
const shopName = String(this.data.profileForm.shopName || '').trim()
|
||||
if (!shopName) {
|
||||
wx.showToast({ title: '请填写店铺名称', icon: 'none' })
|
||||
return
|
||||
}
|
||||
this.setData({ saving: true })
|
||||
try {
|
||||
await shopApi.saveSettings(buildProfilePayload(this.data.settingsSnapshot, this.data.profileForm))
|
||||
wx.showToast({ title: '店铺信息已保存', icon: 'success' })
|
||||
this.setData({ profileSheetVisible: false })
|
||||
await this.loadSettings()
|
||||
} catch (error) {
|
||||
wx.showToast({ title: error.message || '店铺信息保存失败', icon: 'none' })
|
||||
} finally {
|
||||
this.setData({ saving: false })
|
||||
}
|
||||
},
|
||||
|
||||
editTags() {
|
||||
|
||||
Reference in New Issue
Block a user