修复本地上传图片预览

This commit is contained in:
2026-10-29 14:28:55 +08:00
parent 5793bf2390
commit 8f8f89e3b7
4 changed files with 66 additions and 20 deletions
@@ -83,15 +83,20 @@ function removeDraftForm() {
function normalizeManualProductImages(form) {
const savedImages = form && Array.isArray(form.manualProductImages) ? form.manualProductImages : null
if (savedImages) {
return savedImages.map(image => ({
fileId: image && image.fileId !== undefined ? String(image.fileId) : '',
fileUrl: image && (image.fileUrl || image.url) ? String(image.fileUrl || image.url) : ''
})).filter(image => image.fileId).slice(0, 9)
return savedImages.map(image => {
const fileUrl = image && (image.fileUrl || image.url) ? String(image.fileUrl || image.url) : ''
return {
fileId: image && image.fileId !== undefined ? String(image.fileId) : '',
fileUrl,
previewUrl: image && image.previewUrl ? String(image.previewUrl) : fileUrl
}
}).filter(image => image.fileId).slice(0, 9)
}
const fileIds = String(form && form.manualProductImageFileIds || '').split(',').map(fileId => fileId.trim()).filter(Boolean)
return fileIds.slice(0, 9).map((fileId, index) => ({
fileId,
fileUrl: index === 0 && form && form.manualProductImageUrl ? String(form.manualProductImageUrl) : ''
fileUrl: index === 0 && form && form.manualProductImageUrl ? String(form.manualProductImageUrl) : '',
previewUrl: index === 0 && form && form.manualProductImageUrl ? String(form.manualProductImageUrl) : ''
}))
}
@@ -100,7 +105,7 @@ function buildManualProductImagePatch(images) {
return {
'form.manualProductImages': normalizedImages,
'form.manualProductImageFileIds': normalizedImages.map(image => image.fileId).filter(Boolean).join(','),
'form.manualProductImageUrl': normalizedImages[0] ? normalizedImages[0].fileUrl || '' : ''
'form.manualProductImageUrl': normalizedImages[0] ? normalizedImages[0].previewUrl || normalizedImages[0].fileUrl || '' : ''
}
}
@@ -548,7 +553,7 @@ Page({
...baseForm,
manualProductImages,
manualProductImageFileIds: manualProductImages.map(image => image.fileId).join(','),
manualProductImageUrl: manualProductImages[0] ? manualProductImages[0].fileUrl : '',
manualProductImageUrl: manualProductImages[0] ? manualProductImages[0].previewUrl || manualProductImages[0].fileUrl : '',
customerName: options.customerName ? decodeURIComponent(options.customerName) : (baseForm.customerName || ''),
phone: options.phone ? decodeURIComponent(options.phone) : (baseForm.phone || ''),
appointmentDate,
@@ -1036,11 +1041,15 @@ Page({
for (const filePath of filePaths) {
const uploadResult = await uploadObjectFile(filePath)
const fileId = uploadResult.id || uploadResult.fileId
const fileUrl = uploadResult.fileUrl || uploadResult.primaryUrl || filePath
const fileUrl = uploadResult.fileUrl || uploadResult.primaryUrl || ''
if (!fileId) {
throw new Error('作品图片上传成功但未返回文件ID')
}
const images = (this.data.form.manualProductImages || []).concat({ fileId: String(fileId), fileUrl }).slice(0, 9)
const images = (this.data.form.manualProductImages || []).concat({
fileId: String(fileId),
fileUrl,
previewUrl: filePath
}).slice(0, 9)
this.setData({
...buildManualProductImagePatch(images),
'form.productId': '',
@@ -1058,6 +1067,18 @@ Page({
}
},
onManualProductImageError(event) {
const index = Number(event && event.currentTarget && event.currentTarget.dataset.index)
const images = this.data.form.manualProductImages || []
const image = images[index]
if (!Number.isInteger(index) || !image || !image.previewUrl || !image.fileUrl || image.previewUrl === image.fileUrl) {
return
}
const fallbackImages = images.slice()
fallbackImages[index] = { ...image, previewUrl: image.fileUrl }
this.setData(buildManualProductImagePatch(fallbackImages))
},
removeManualProductImage(event) {
if (this.data.manualImageUploading) {
return
@@ -15,7 +15,7 @@
</view>
</view>
<button wx:if="{{!form.productId}}" class="field-card appointment-field-card full" bindtap="openManualProduct">
<image wx:if="{{form.manualProductImageUrl}}" class="field-icon style-entry-image" src="{{form.manualProductImageUrl}}" mode="aspectFill"></image>
<image wx:if="{{form.manualProductImages.length}}" class="field-icon style-entry-image" src="{{form.manualProductImages[0].previewUrl || form.manualProductImages[0].fileUrl}}" mode="aspectFill" data-index="0" binderror="onManualProductImageError"></image>
<view wx:else class="field-icon">✦</view>
<view class="field-text">
<text wx:if="{{form.manualProductTitle}}" class="field-value">{{form.manualProductTitle}}</text>
@@ -105,7 +105,7 @@
<view class="manual-style-image-heading"><text>款式图片</text><text class="manual-style-image-count">{{form.manualProductImages.length}} / 9</text></view>
<view class="manual-style-image-grid">
<view wx:for="{{form.manualProductImages}}" wx:key="fileId" class="manual-style-image-tile">
<image src="{{item.fileUrl}}" mode="aspectFill"></image>
<image src="{{item.previewUrl || item.fileUrl}}" mode="aspectFill" data-index="{{index}}" binderror="onManualProductImageError"></image>
<text wx:if="{{index === 0}}" class="manual-style-image-cover">封面</text>
<button class="manual-style-image-remove" data-index="{{index}}" disabled="{{manualImageUploading}}" catchtap="removeManualProductImage" aria-label="移除图片">×</button>
</view>
@@ -139,7 +139,7 @@ assert.match(wxml, /placeholder="留空时默认使用客户姓名"/)
assert.match(wxml, /appointmentDateBadge/)
assert.match(wxml, /class="customer-fields"/)
assert.match(wxml, /class="manual-style-sheet"/)
assert.match(wxml, /名称<\/text><text class="manual-style-character-count">\{\{manualProductTitleLength\}\} \/ 100/)
assert.match(wxml, /名称<\/text><text class="manual-style-character-count">\s*\{\{manualProductTitleLength\}\} \/ 100/)
assert.match(wxml, /maxlength="100"[^>]*bindinput="onStyleTitleInput"/)
assert.match(wxml, /disabled="\{\{form\.productId\}\}" bindinput="onStyleTitleInput"/)
assert.match(wxml, /class="manual-style-price-duration-row"/)
@@ -153,11 +153,13 @@ assert.doesNotMatch(wxml, /class="style-remark-field"/)
assert.doesNotMatch(wxml, /class="style-number-row"/)
assert.match(wxml, /class="manual-style-footer"><button class="manual-style-confirm"[^>]*>用于本次预约<\/button><button class="manual-style-library-link"[^>]*>从款式库中选择/)
assert.match(wxml, /class="manual-style-more-toggle" bindtap="toggleManualProductMore"/)
assert.match(wxml, /更多款式信息(选填)/)
assert.match(wxml, /点击展开补充信息/)
assert.match(wxml, /bind:openmanual="openManualProductFromLibrary"/)
assert.match(wxml, /show-manual-action="\{\{true\}\}"/)
assert.match(wxml, /本次 \{\{form\.amount \|\| '待填'\}\} 元/)
assert.match(wxml, /wx:for="\{\{form\.manualProductImages\}\}"/)
assert.match(wxml, /src="\{\{item\.previewUrl \|\| item\.fileUrl\}\}"/)
assert.match(wxml, /form\.manualProductImages\[0\]\.previewUrl \|\| form\.manualProductImages\[0\]\.fileUrl/)
assert.match(wxml, /manualProductImages\.length < 9/)
assert.match(wxml, /data-index="\{\{index\}\}"[^>]*catchtap="removeManualProductImage"/)
assert.match(wxml, /manualProductImages\.length\}\} \/ 9/)
@@ -173,16 +175,18 @@ async function testManualProductImages() {
assert.strictEqual(requestedImageCount, 9)
assert.deepStrictEqual(uploadCalls.slice(-3), selectedImagePaths)
assert.deepStrictEqual(page.data.form.manualProductImages, [
{ fileId: '11', fileUrl: 'https://img.example/1.jpg' },
{ fileId: '12', fileUrl: 'https://img.example/2.jpg' },
{ fileId: '13', fileUrl: 'https://img.example/3.jpg' }
{ fileId: '11', fileUrl: 'https://img.example/1.jpg', previewUrl: '/tmp/style-1.jpg' },
{ fileId: '12', fileUrl: 'https://img.example/2.jpg', previewUrl: '/tmp/style-2.jpg' },
{ fileId: '13', fileUrl: 'https://img.example/3.jpg', previewUrl: '/tmp/style-3.jpg' }
])
assert.strictEqual(page.data.form.manualProductImageFileIds, '11,12,13')
assert.strictEqual(page.data.form.manualProductImageUrl, 'https://img.example/1.jpg')
assert.strictEqual(page.data.form.manualProductImageUrl, '/tmp/style-1.jpg')
page.onManualProductImageError({ currentTarget: { dataset: { index: 0 } } })
assert.strictEqual(page.data.form.manualProductImages[0].previewUrl, 'https://img.example/1.jpg')
page.removeManualProductImage({ currentTarget: { dataset: { index: 0 } } })
assert.strictEqual(page.data.form.manualProductImageFileIds, '12,13')
assert.strictEqual(page.data.form.manualProductImageUrl, 'https://img.example/2.jpg')
assert.strictEqual(page.data.form.manualProductImageUrl, '/tmp/style-2.jpg')
page.data.form.manualProductImages = Array.from({ length: 8 }, (_, index) => ({ fileId: String(index + 20), fileUrl: `https://img.example/${index + 20}.jpg` }))
selectedImagePaths = ['/tmp/style-9.jpg', '/tmp/ignored.jpg']
@@ -196,7 +200,7 @@ async function testManualProductImages() {
uploadResults = [{ id: 29, fileUrl: 'https://img.example/29.jpg' }, new Error('第二张上传失败')]
await page.chooseManualProductImage()
assert.deepStrictEqual(page.data.form.manualProductImages, [
{ fileId: '29', fileUrl: 'https://img.example/29.jpg' }
{ fileId: '29', fileUrl: 'https://img.example/29.jpg', previewUrl: '/tmp/style-partial-1.jpg' }
])
assert.strictEqual(toastMessages.pop(), '第二张上传失败')
@@ -213,9 +217,24 @@ async function testManualProductImages() {
legacyDraftPage.loadOptions = () => {}
legacyDraftPage.onLoad({})
assert.deepStrictEqual(legacyDraftPage.data.form.manualProductImages, [
{ fileId: '44', fileUrl: 'https://img.example/legacy.jpg' }
{ fileId: '44', fileUrl: 'https://img.example/legacy.jpg', previewUrl: 'https://img.example/legacy.jpg' }
])
assert.strictEqual(legacyDraftPage.data.form.manualProductImageFileIds, '44')
global.wx.getStorageSync = () => ({ form: {
appointmentDate: '2026-10-21',
manualProductImages: [{
fileId: '45',
fileUrl: 'https://img.example/restored.jpg',
previewUrl: '/tmp/stale-style.jpg'
}]
} })
const restoredDraftPage = createPage()
restoredDraftPage.loadOptions = () => {}
restoredDraftPage.onLoad({})
restoredDraftPage.onManualProductImageError({ currentTarget: { dataset: { index: 0 } } })
assert.strictEqual(restoredDraftPage.data.form.manualProductImages[0].previewUrl, 'https://img.example/restored.jpg')
assert.strictEqual(restoredDraftPage.data.form.manualProductImageUrl, 'https://img.example/restored.jpg')
}
page.openProductModal()
+6
View File
@@ -401,3 +401,9 @@
- 将款式描述与预约备注合并到弹层默认展示的“款式描述 / 备注”,删除主表单上重复的备注和时长输入。
- “用于本次预约”和“从款式库中选择”调整为并排异色按钮;库内选择后仍可在弹层调整本次价格与时长。
- 修改预约页交互前先对已有工作区改动建立独立回溯提交;以后未被明确要求时不额外编写设计文档。
## 2026-10-29 Salon 小程序本地上传图片预览修复
- 新增预约款式图片上传后优先使用微信本地临时文件路径预览,同时保留对象存储文件 ID 和服务端 URL;本地预览失效时回退到服务端 URL。
- 原因是私有对象存储的上传响应 URL 不一定能由小程序开发环境直接读取,原页面优先渲染该 URL,导致图片虽已上传成功但预览为空。
- 图片提交仍使用文件 ID,不改变对象存储公开权限或内容审核状态。