修复本地上传图片预览
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user