修复小程序上传图片预览
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
const planningApi = require('../../api/planning')
|
||||
const { markPlanningRefresh } = require('../../utils/planning-refresh')
|
||||
const { chooseImageFilePaths, uploadObjectFile } = require('../../utils/upload')
|
||||
const { chooseImageFilePaths, uploadObjectFile, resolveUploadedImagePreviewUrl } = require('../../utils/upload')
|
||||
|
||||
function canTransferAppointment() {
|
||||
const app = getApp()
|
||||
@@ -30,7 +30,7 @@ async function uploadAppointmentImageFiles(limit) {
|
||||
const filePath = filePaths[index]
|
||||
const uploadResult = await uploadObjectFile(filePath)
|
||||
const fileId = uploadResult.id || uploadResult.fileId
|
||||
const fileUrl = uploadResult.fileUrl || uploadResult.primaryUrl || filePath
|
||||
const fileUrl = resolveUploadedImagePreviewUrl(uploadResult, filePath)
|
||||
if (!fileId) {
|
||||
throw new Error('预约图片上传成功但未返回文件ID')
|
||||
}
|
||||
@@ -121,12 +121,15 @@ Page({
|
||||
wx.navigateTo({ url: `/pages/artist-transfer/index?id=${this.data.id}` })
|
||||
},
|
||||
|
||||
async persistAppointmentImages(fileIds) {
|
||||
async persistAppointmentImages(fileIds, previewImageUrls) {
|
||||
const appointment = await planningApi.updateAppointmentImages(this.data.id, {
|
||||
imageFileIds: fileIds.join(',')
|
||||
})
|
||||
markPlanningRefresh()
|
||||
this.applyAppointment(appointment)
|
||||
if (Array.isArray(previewImageUrls) && previewImageUrls.length > 0) {
|
||||
this.setData({ productImageUrls: previewImageUrls })
|
||||
}
|
||||
},
|
||||
|
||||
async chooseAppointmentImages() {
|
||||
@@ -145,7 +148,10 @@ Page({
|
||||
if (uploaded.fileIds.length === 0) {
|
||||
return
|
||||
}
|
||||
await this.persistAppointmentImages(currentFileIds.concat(uploaded.fileIds))
|
||||
await this.persistAppointmentImages(
|
||||
currentFileIds.concat(uploaded.fileIds),
|
||||
this.data.productImageUrls.concat(uploaded.imageUrls)
|
||||
)
|
||||
wx.showToast({ title: '图片已保存' })
|
||||
} catch (error) {
|
||||
wx.showToast({ title: error.message || '图片上传失败', icon: 'none' })
|
||||
@@ -164,7 +170,7 @@ Page({
|
||||
if (uploaded.fileIds.length === 0) {
|
||||
return
|
||||
}
|
||||
await this.persistAppointmentImages(uploaded.fileIds)
|
||||
await this.persistAppointmentImages(uploaded.fileIds, uploaded.imageUrls)
|
||||
wx.showToast({ title: '图片已保存' })
|
||||
} catch (error) {
|
||||
wx.showToast({ title: error.message || '图片上传失败', icon: 'none' })
|
||||
@@ -182,10 +188,12 @@ Page({
|
||||
if (!Number.isInteger(index) || index < 0 || index >= currentFileIds.length) {
|
||||
return
|
||||
}
|
||||
const currentImageUrls = Array.isArray(this.data.productImageUrls) ? this.data.productImageUrls : []
|
||||
const nextFileIds = currentFileIds.filter((item, itemIndex) => itemIndex !== index)
|
||||
const nextImageUrls = currentImageUrls.filter((item, itemIndex) => itemIndex !== index)
|
||||
this.setData({ imageUploading: true })
|
||||
try {
|
||||
await this.persistAppointmentImages(nextFileIds)
|
||||
await this.persistAppointmentImages(nextFileIds, nextImageUrls)
|
||||
wx.showToast({ title: '图片已保存' })
|
||||
} catch (error) {
|
||||
wx.showToast({ title: error.message || '图片保存失败', icon: 'none' })
|
||||
|
||||
@@ -4,7 +4,7 @@ const tagsApi = require('../../api/tags')
|
||||
const customersApi = require('../../api/customers')
|
||||
const dateUtil = require('../../utils/date')
|
||||
const { markPlanningRefresh } = require('../../utils/planning-refresh')
|
||||
const { chooseImageFilePaths, uploadObjectFile } = require('../../utils/upload')
|
||||
const { chooseImageFilePaths, uploadObjectFile, resolveUploadedImagePreviewUrl } = require('../../utils/upload')
|
||||
|
||||
const DRAFT_STORAGE_PREFIX = 'managerAppointmentDraft'
|
||||
const TIME_SLOT_MINUTES = 15
|
||||
@@ -792,7 +792,7 @@ Page({
|
||||
const filePath = filePaths[index]
|
||||
const uploadResult = await uploadObjectFile(filePath)
|
||||
const fileId = uploadResult.id || uploadResult.fileId
|
||||
const fileUrl = uploadResult.fileUrl || uploadResult.primaryUrl || filePath
|
||||
const fileUrl = resolveUploadedImagePreviewUrl(uploadResult, filePath)
|
||||
if (!fileId) {
|
||||
throw new Error('作品图片上传成功但未返回文件ID')
|
||||
}
|
||||
|
||||
@@ -107,6 +107,21 @@ function uploadObjectFile(filePath) {
|
||||
})
|
||||
}
|
||||
|
||||
function resolveUploadedImagePreviewUrl(uploadResult, fallbackFilePath) {
|
||||
const localPath = String(fallbackFilePath || '').trim()
|
||||
if (localPath) {
|
||||
return localPath
|
||||
}
|
||||
if (!uploadResult || typeof uploadResult !== 'object') {
|
||||
return ''
|
||||
}
|
||||
const primaryUrl = String(uploadResult.primaryUrl || '').trim()
|
||||
if (primaryUrl) {
|
||||
return primaryUrl
|
||||
}
|
||||
return String(uploadResult.fileUrl || '').trim()
|
||||
}
|
||||
|
||||
function uploadProfileImage(filePath) {
|
||||
return uploadFile({
|
||||
url: '/user/upload-profile-image',
|
||||
@@ -131,6 +146,7 @@ module.exports = {
|
||||
chooseImageFilePaths,
|
||||
chooseImageFilePath,
|
||||
uploadObjectFile,
|
||||
resolveUploadedImagePreviewUrl,
|
||||
uploadAvatar,
|
||||
uploadProfileImage
|
||||
}
|
||||
|
||||
@@ -200,3 +200,11 @@
|
||||
- 处理方式:测试对象设置不同的 `id` 后再作为 Map 值和 mock 参数使用,确保 equals 匹配可区分。
|
||||
- 以后避免:对 Lombok `@Data` 值对象做 Mockito 精确参数 stub 时,不要使用字段全空的多个实例;设置主键/唯一字段,或改用 `thenAnswer` 按字段分派。
|
||||
- 验证方式:`mvn -f JY-Core/pom.xml -Dtest=PlanningServiceImplTest test` 通过。
|
||||
|
||||
## 2026-07-21 小程序上传图片预览不能直接使用对象存储原始地址
|
||||
|
||||
- 问题:店务小程序预约作品弹窗上传图片后生成了缩略图占位和删除按钮,但图片内容为空白。
|
||||
- 原因:`/object-storage-file/upload` 返回的 `fileUrl`/`primaryUrl` 可能是对象存储原始地址、私有桶地址或未签名地址;小程序 `<image>` 直接加载会失败。后台日志里的七牛 `incorrect region` 是异步副本配置问题,不代表本次上传没有拿到 fileId。
|
||||
- 处理方式:上传成功后前端预览优先使用 `wx.chooseMedia` 返回的本地临时路径,业务保存仍只依赖后端返回的 fileId;详情页上传保存后也保留本地预览,避免立即被不可访问的后端 URL 覆盖。
|
||||
- 以后避免:小程序图片上传链路要区分“保存标识”和“展示地址”;保存用 fileId,当前会话预览优先用本地路径,列表/详情回显再使用后端生成的可访问 URL。
|
||||
- 验证方式:上传后弹窗缩略图应立即显示真实图片;提交预约后详情页能从 fileId 回显图片;JS 语法检查、`git diff --check` 和 GitNexus 变更检测通过。
|
||||
|
||||
Reference in New Issue
Block a user