const shopApi = require('../../api/shop') const { BRAND_CONFIG } = require('../../utils/config') const { chooseImageFilePaths, 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 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: String(form.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(form.coverImageFileId || '').trim(), imageFileIds: joinFileIds(form.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 } } Page({ data: { 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: '' } }, onLoad() { this.loadSettings() }, async loadSettings() { 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) { 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.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({ '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.galleryUploading || this.data.saving) { return } this.setData({ 'form.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 (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) { 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() } })