重构店铺设置展示与编辑流程
This commit is contained in:
@@ -0,0 +1,150 @@
|
||||
const shopApi = require('../../api/shop')
|
||||
const { BRAND_CONFIG } = require('../../utils/config')
|
||||
const { chooseImageFilePath, uploadObjectFile, resolveUploadedImagePreviewUrl } = require('../../utils/upload')
|
||||
|
||||
const DEFAULT_SHOP_INTRO = '专注法式、晕染与手绘设计,适合想要温柔质感的客人。'
|
||||
|
||||
function resolveFirstImageUrl(urls) {
|
||||
return Array.isArray(urls) ? String(urls[0] || '').trim() : ''
|
||||
}
|
||||
|
||||
function introLength(value) {
|
||||
return String(value || '').length
|
||||
}
|
||||
|
||||
function buildShopPayload(settings, form) {
|
||||
return {
|
||||
shopName: String(form.shopName || '').trim(),
|
||||
description: String(form.rule || '').trim(),
|
||||
address: settings.address || '',
|
||||
mapProvider: settings.mapProvider || '',
|
||||
longitude: settings.longitude,
|
||||
latitude: settings.latitude,
|
||||
businessHours: settings.businessHours || '',
|
||||
schedulePresetType: settings.schedulePresetType || '',
|
||||
scheduleWorkDays: settings.scheduleWorkDays || '',
|
||||
scheduleRemark: settings.scheduleRemark || '',
|
||||
coverImageFileId: String(form.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
|
||||
}
|
||||
}
|
||||
|
||||
Page({
|
||||
data: {
|
||||
loading: false,
|
||||
saving: false,
|
||||
coverUploading: false,
|
||||
coverImagePreviewUrl: '',
|
||||
introLength: introLength(DEFAULT_SHOP_INTRO),
|
||||
settingsSnapshot: {},
|
||||
form: {
|
||||
shopName: BRAND_CONFIG.defaultShopName,
|
||||
rule: DEFAULT_SHOP_INTRO,
|
||||
coverImageFileId: ''
|
||||
}
|
||||
},
|
||||
|
||||
onLoad() {
|
||||
this.loadSettings()
|
||||
},
|
||||
|
||||
async loadSettings() {
|
||||
this.setData({ loading: true })
|
||||
try {
|
||||
const settings = await shopApi.settings()
|
||||
const form = {
|
||||
shopName: settings.shopName || settings.name || this.data.form.shopName,
|
||||
rule: settings.description || DEFAULT_SHOP_INTRO,
|
||||
coverImageFileId: settings.coverImageFileId || ''
|
||||
}
|
||||
this.setData({
|
||||
settingsSnapshot: settings || {},
|
||||
form,
|
||||
introLength: introLength(form.rule),
|
||||
coverImagePreviewUrl: resolveFirstImageUrl(settings.coverImageUrls)
|
||||
})
|
||||
} catch (error) {
|
||||
wx.showToast({ title: error.message || '店铺档案加载失败', icon: 'none' })
|
||||
} finally {
|
||||
this.setData({ loading: false })
|
||||
}
|
||||
},
|
||||
|
||||
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)
|
||||
},
|
||||
|
||||
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: ''
|
||||
})
|
||||
},
|
||||
|
||||
async save() {
|
||||
const shopName = String(this.data.form.shopName || '').trim()
|
||||
if (!shopName) {
|
||||
wx.showToast({ title: '请填写店铺名称', icon: 'none' })
|
||||
return
|
||||
}
|
||||
this.setData({ saving: true })
|
||||
try {
|
||||
await shopApi.saveSettings(buildShopPayload(this.data.settingsSnapshot, this.data.form))
|
||||
wx.showToast({ title: '店铺档案已保存', icon: 'success' })
|
||||
wx.navigateBack()
|
||||
} catch (error) {
|
||||
wx.showToast({ title: error.message || '店铺档案保存失败', icon: 'none' })
|
||||
} finally {
|
||||
this.setData({ saving: false })
|
||||
}
|
||||
},
|
||||
|
||||
back() {
|
||||
wx.navigateBack()
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user