优化店务小程序店铺设置UI

This commit is contained in:
2026-07-24 16:58:34 +08:00
parent 97821cf371
commit 32ba6b6d64
8 changed files with 1174 additions and 954 deletions
@@ -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) {
@@ -8,7 +8,7 @@
<view class="edit-tip-card">
<text class="edit-tip-title">门店第一眼</text>
<text class="edit-tip-text">店铺名称、介绍和封面会展示在用户端门店详情页。</text>
<text class="edit-tip-text">店铺名称、介绍、地址和封面会展示在用户端门店详情页。</text>
</view>
<view class="cover-card">
@@ -21,8 +21,8 @@
</view>
<view class="cover-badge">门店封面</view>
<view class="cover-actions">
<button class="cover-btn" loading="{{coverUploading}}" disabled="{{coverUploading || saving}}" bindtap="chooseCoverImage">{{coverUploading ? '上传中' : '更换主图'}}</button>
<button wx:if="{{coverImagePreviewUrl}}" class="cover-remove" disabled="{{coverUploading || saving}}" bindtap="removeCoverImage">移除</button>
<button class="cover-btn" loading="{{coverUploading}}" disabled="{{coverUploading || galleryUploading || saving}}" bindtap="chooseCoverImage">{{coverUploading ? '上传中' : '更换主图'}}</button>
<button wx:if="{{coverImagePreviewUrl}}" class="cover-remove" disabled="{{coverUploading || galleryUploading || saving}}" bindtap="removeCoverImage">移除</button>
</view>
</view>
@@ -35,6 +35,10 @@
<text class="field-label">店铺名称</text>
<input value="{{form.shopName}}" data-field="shopName" bindinput="onInput" placeholder="请输入店铺名称" />
</view>
<view class="field">
<text class="field-label">店铺地址</text>
<input value="{{form.address}}" data-field="address" bindinput="onInput" placeholder="请输入店铺地址" />
</view>
<view class="field field-column">
<text class="field-label">门店介绍</text>
<view class="intro-editor">
@@ -51,9 +55,30 @@
</view>
</view>
</view>
<view class="card gallery-card">
<view class="gallery-head">
<view>
<text class="form-kicker">环境照片</text>
<text class="gallery-subtitle">用于店铺展示区,最多 9 张</text>
</view>
<text class="gallery-count">{{galleryItems.length}}/9</text>
</view>
<view class="gallery-grid">
<view wx:for="{{galleryItems}}" wx:key="fileId" class="gallery-item">
<image wx:if="{{item.url}}" src="{{item.url}}" mode="aspectFill"></image>
<view wx:else class="gallery-placeholder">图片</view>
<text class="gallery-remove" data-index="{{index}}" bindtap="removeGalleryImage">×</text>
</view>
<view wx:if="{{galleryItems.length < 9}}" class="gallery-add" bindtap="chooseGalleryImages">
<text class="gallery-add-icon">+</text>
<text>{{galleryUploading ? '上传中' : '添加'}}</text>
</view>
</view>
</view>
</block>
<view class="fixed-bottom">
<button class="primary-btn bottom-half-btn" loading="{{saving}}" disabled="{{saving || coverUploading}}" bindtap="save">保存档案</button>
<button class="primary-btn bottom-half-btn" loading="{{saving}}" disabled="{{saving || coverUploading || galleryUploading}}" bindtap="save">保存档案</button>
</view>
</view>
@@ -19,7 +19,8 @@
}
.page-subtitle,
.form-title {
.form-title,
.gallery-subtitle {
color: #8a796a;
font-size: 23rpx;
line-height: 1.4;
@@ -38,17 +39,17 @@
margin-bottom: 20rpx;
padding: 22rpx 24rpx;
border-radius: 22rpx;
background: #fff0ed;
background: #edf6ea;
}
.edit-tip-title {
color: #3e312d;
color: #2d392f;
font-size: 29rpx;
font-weight: 900;
}
.edit-tip-text {
color: #9f6c64;
color: #668268;
font-size: 23rpx;
line-height: 1.55;
}
@@ -136,12 +137,20 @@
color: #fffdf8;
}
.form-card {
.form-card,
.gallery-card {
margin-top: 24rpx;
padding: 12rpx 0;
overflow: hidden;
}
.form-card {
padding: 12rpx 0;
}
.gallery-card {
padding: 24rpx;
}
.form-header {
display: flex;
flex-direction: column;
@@ -229,6 +238,90 @@
line-height: 1;
}
.gallery-head {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 16rpx;
margin-bottom: 18rpx;
}
.gallery-head view {
display: flex;
flex-direction: column;
gap: 8rpx;
min-width: 0;
}
.gallery-count {
flex: 0 0 auto;
color: #7c9a82;
font-size: 24rpx;
font-weight: 900;
}
.gallery-grid {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
gap: 14rpx;
}
.gallery-item,
.gallery-add {
position: relative;
display: flex;
align-items: center;
justify-content: center;
aspect-ratio: 1 / 1;
min-width: 0;
overflow: hidden;
border-radius: 18rpx;
background: #f5efe8;
}
.gallery-item image {
width: 100%;
height: 100%;
}
.gallery-placeholder {
color: #9d968e;
font-size: 24rpx;
}
.gallery-remove {
position: absolute;
top: 8rpx;
right: 8rpx;
display: flex;
align-items: center;
justify-content: center;
width: 38rpx;
height: 38rpx;
border-radius: 50%;
background: rgba(39, 35, 31, 0.58);
color: #fffdf8;
font-size: 30rpx;
font-weight: 300;
line-height: 1;
}
.gallery-add {
flex-direction: column;
gap: 8rpx;
border: 2rpx dashed #cfdccf;
background: #f8fbf6;
color: #6f8f83;
font-size: 23rpx;
font-weight: 800;
}
.gallery-add-icon {
font-size: 42rpx;
font-weight: 300;
line-height: 1;
}
.fixed-bottom {
display: flex;
gap: 14rpx;