优化店务小程序店铺设置UI
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
const shopApi = require('../../api/shop')
|
||||
const { BRAND_CONFIG } = require('../../utils/config')
|
||||
const { chooseImageFilePath, uploadObjectFile, resolveUploadedImagePreviewUrl } = require('../../utils/upload')
|
||||
const { chooseImageFilePaths, chooseImageFilePath, uploadObjectFile, resolveUploadedImagePreviewUrl } = require('../../utils/upload')
|
||||
|
||||
const DEFAULT_SHOP_INTRO = '专注法式、晕染与手绘设计,适合想要温柔质感的客人。'
|
||||
|
||||
@@ -12,11 +12,31 @@ 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 = Array.isArray(urls) ? urls.map(url => String(url || '').trim()) : []
|
||||
return normalizedFileIds.map((fileId, index) => ({
|
||||
fileId,
|
||||
url: normalizedUrls[index] || ''
|
||||
}))
|
||||
}
|
||||
|
||||
function buildShopPayload(settings, form) {
|
||||
return {
|
||||
shopName: String(form.shopName || '').trim(),
|
||||
description: String(form.rule || '').trim(),
|
||||
address: settings.address || '',
|
||||
address: String(form.address || '').trim(),
|
||||
mapProvider: settings.mapProvider || '',
|
||||
longitude: settings.longitude,
|
||||
latitude: settings.latitude,
|
||||
@@ -25,7 +45,7 @@ function buildShopPayload(settings, form) {
|
||||
scheduleWorkDays: settings.scheduleWorkDays || '',
|
||||
scheduleRemark: settings.scheduleRemark || '',
|
||||
coverImageFileId: String(form.coverImageFileId || '').trim(),
|
||||
imageFileIds: String(settings.imageFileIds || '').trim(),
|
||||
imageFileIds: joinFileIds(form.galleryFileIds),
|
||||
themePrimaryColor: settings.themePrimaryColor || '',
|
||||
themeSecondaryColor: settings.themeSecondaryColor || '',
|
||||
themeAccentColor: settings.themeAccentColor || '',
|
||||
@@ -44,12 +64,16 @@ Page({
|
||||
loading: false,
|
||||
saving: false,
|
||||
coverUploading: false,
|
||||
galleryUploading: false,
|
||||
galleryItems: [],
|
||||
coverImagePreviewUrl: '',
|
||||
introLength: introLength(DEFAULT_SHOP_INTRO),
|
||||
settingsSnapshot: {},
|
||||
form: {
|
||||
shopName: BRAND_CONFIG.defaultShopName,
|
||||
rule: DEFAULT_SHOP_INTRO,
|
||||
address: '',
|
||||
galleryFileIds: [],
|
||||
coverImageFileId: ''
|
||||
}
|
||||
},
|
||||
@@ -62,15 +86,19 @@ Page({
|
||||
this.setData({ loading: true })
|
||||
try {
|
||||
const settings = await shopApi.settings()
|
||||
const galleryItems = normalizeGalleryItems(settings.imageFileIds, settings.imageUrls)
|
||||
const form = {
|
||||
shopName: settings.shopName || settings.name || this.data.form.shopName,
|
||||
rule: settings.description || DEFAULT_SHOP_INTRO,
|
||||
address: settings.address || '',
|
||||
galleryFileIds: galleryItems.map(item => item.fileId),
|
||||
coverImageFileId: settings.coverImageFileId || ''
|
||||
}
|
||||
this.setData({
|
||||
settingsSnapshot: settings || {},
|
||||
form,
|
||||
introLength: introLength(form.rule),
|
||||
galleryItems,
|
||||
coverImagePreviewUrl: resolveFirstImageUrl(settings.coverImageUrls)
|
||||
})
|
||||
} catch (error) {
|
||||
@@ -91,7 +119,7 @@ Page({
|
||||
},
|
||||
|
||||
async chooseCoverImage() {
|
||||
if (this.data.coverUploading || this.data.saving) {
|
||||
if (this.data.coverUploading || this.data.galleryUploading || this.data.saving) {
|
||||
return
|
||||
}
|
||||
try {
|
||||
@@ -117,7 +145,7 @@ Page({
|
||||
},
|
||||
|
||||
removeCoverImage() {
|
||||
if (this.data.coverUploading || this.data.saving) {
|
||||
if (this.data.coverUploading || this.data.galleryUploading || this.data.saving) {
|
||||
return
|
||||
}
|
||||
this.setData({
|
||||
@@ -126,6 +154,61 @@ Page({
|
||||
})
|
||||
},
|
||||
|
||||
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 (const filePath of filePaths) {
|
||||
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,
|
||||
'form.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,
|
||||
'form.galleryFileIds': galleryItems.map(item => item.fileId)
|
||||
})
|
||||
},
|
||||
|
||||
async save() {
|
||||
const shopName = String(this.data.form.shopName || '').trim()
|
||||
if (!shopName) {
|
||||
|
||||
Reference in New Issue
Block a user