diff --git a/pages/release/index.vue b/pages/release/index.vue index d09c0b1..d44b29f 100644 --- a/pages/release/index.vue +++ b/pages/release/index.vue @@ -165,6 +165,7 @@ import { smartNavigateBack } from "@/utils/page"; import { uploadImage, FILE_BASE_URL, + chooseAndUploadImage, } from "@/utils/common.js"; const statusBarHeight = ref(getStatusBarHeight() || 0); @@ -225,76 +226,26 @@ const handlePreview = () => { }); }; -const chooseCover = () => { - uni.chooseImage({ - count: 1, - sizeType: ["original", "compressed"], - sourceType: ["album"], - success: (res) => { - const path = res.tempFilePaths[0]; - - // #ifdef MP-ALIPAY - // 支付宝小程序不支持 cropImage,直接使用原图 - onAvatarSelect(path); - // #endif - - // #ifndef MP-ALIPAY - // 调用系统裁剪,强制 1:1 - uni.cropImage({ - src: path, - aspectRatio: "1:1", - success: (cropRes) => { - onAvatarSelect(cropRes.tempFilePath); - }, - fail: (err) => { - console.warn("Crop failed or cancelled", err); - onAvatarSelect(path); - }, - }); - // #endif - }, - fail: (err) => { - console.warn("Crop failed or cancelled", err); - }, - }); -}; - -const onAvatarSelect = async (url) => { - if (!url) return; - - uni.showLoading({ title: "上传中...", mask: true }); +const chooseCover = async () => { try { - const imageUrl = await uploadImage(url); - - formData.coverUrl = imageUrl; - uni.hideLoading(); + const urls = await chooseAndUploadImage({ count: 1 }); + if (urls && urls.length > 0) { + formData.coverUrl = urls[0]; + } } catch (e) { - uni.hideLoading(); - uni.showToast({ title: e || "上传失败", icon: "none" }); - console.error("Upload avatar error", e); + console.error("选择封面失败", e); } }; -const chooseParticipantAvatar = (index) => { - uni.chooseImage({ - count: 1, - success: async (res) => { - const url = res.tempFilePaths[0]; - uni.showLoading({ title: '上传中...' }); - try { - const imageUrl = await uploadImage(url); - participants.value[index].avatar = imageUrl; - uni.hideLoading(); - } catch (e) { - uni.hideLoading(); - uni.showToast({ - title: "上传失败", - icon: "none", - }); - console.error(e); - } - }, - }); +const chooseParticipantAvatar = async (index) => { + try { + const urls = await chooseAndUploadImage({ count: 1 }); + if (urls && urls.length > 0) { + participants.value[index].avatar = urls[0]; + } + } catch (e) { + console.error("选择评分对象头像失败", e); + } }; const handleAddTag = () => { diff --git a/utils/common.js b/utils/common.js index bccc097..cb8f8e4 100644 --- a/utils/common.js +++ b/utils/common.js @@ -47,6 +47,54 @@ export const uploadImage = (filePath) => { }); }; +/** + * 统一的选择并上传图片的方法 + * @param {Object} options 配置项 + * @param {number} options.count 最多可选择的图片数量,默认为 1 + * @param {string} options.loadingTitle 上传中提示文案,默认为 '上传中...' + * @returns {Promise} 返回一个包含所有上传成功图片 URL 的数组 Promise + */ +export const chooseAndUploadImage = (options = {}) => { + const { count = 1, loadingTitle = '上传中...' } = options; + + return new Promise((resolve, reject) => { + uni.chooseImage({ + count, + success: async (res) => { + const filePaths = res.tempFilePaths; + if (!filePaths || filePaths.length === 0) { + return resolve([]); + } + + uni.showLoading({ title: loadingTitle }); + try { + // 并发上传所有选中的图片 + const uploadPromises = filePaths.map(path => uploadImage(path)); + const urls = await Promise.all(uploadPromises); + uni.hideLoading(); + resolve(urls); + } catch (error) { + uni.hideLoading(); + uni.showToast({ + title: "上传失败", + icon: "none", + }); + console.error("图片上传异常:", error); + reject(error); + } + }, + fail: (err) => { + // 用户取消选择不作为异常抛出,返回空数组 + if (err.errMsg && err.errMsg.indexOf('cancel') !== -1) { + resolve([]); + } else { + reject(err); + } + } + }); + }); +}; + export const saveRecordRequest = async (path, targetId, scene, imageUrl) => { if (!imageUrl) { imageUrl = await uploadImage(path);