修复小程序上传图片预览
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user