From 689b1462b274c231823306b8c8a2f4b8914759c1 Mon Sep 17 00:00:00 2001 From: huliyong <2783385703@qq.com> Date: Wed, 23 Jul 2025 13:13:34 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E5=9B=BE=E7=89=87=E4=BF=9D=E5=AD=98):=20?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=9B=BE=E7=89=87=E4=BF=9D=E5=AD=98=E5=88=B0?= =?UTF-8?q?=E7=9B=B8=E5=86=8C=E5=8A=9F=E8=83=BD=E5=B9=B6=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E6=9D=83=E9=99=90=E6=8F=8F=E8=BF=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改图片保存逻辑,先下载图片到本地再保存,提高成功率 在manifest.json中添加相册权限描述 --- manifest.json | 5 ++- pages/subcomponent/promo-qrcode.vue | 84 ++++++++++++++++++++++++------------- 2 files changed, 59 insertions(+), 30 deletions(-) diff --git a/manifest.json b/manifest.json index db6f562..e02faa9 100644 --- a/manifest.json +++ b/manifest.json @@ -59,7 +59,10 @@ "permission" : { "scope.userLocation" : { "desc" : "您的位置信息将用于选择寄件地址" - } + }, + "scope.writePhotosAlbum": { + "desc": "需要保存图片到您的相册" + } } }, "mp-alipay" : { diff --git a/pages/subcomponent/promo-qrcode.vue b/pages/subcomponent/promo-qrcode.vue index bc8e15c..fcd8238 100644 --- a/pages/subcomponent/promo-qrcode.vue +++ b/pages/subcomponent/promo-qrcode.vue @@ -177,35 +177,61 @@ export default { // 获取图片信息并保存到相册 imgApi(image) { return new Promise((resolve, reject) => { - // 获取图片的信息 - uni.getImageInfo({ - src: image, - success: (imageInfo) => { - // 保存图片到手机相册 - uni.saveImageToPhotosAlbum({ - filePath: imageInfo.path, - success: () => { - uni.showModal({ - title: '保存成功', - content: '图片已成功保存到相册', - showCancel: false - }) - resolve() - }, - fail: (err) => { - console.log('保存失败', err) - reject(new Error('保存失败')) - }, - complete: (res) => { - console.log('保存结果:', res) - } - }) - }, - fail: (err) => { - console.log('获取图片信息失败:', err) - reject(new Error('获取图片信息失败')) - } - }) + + // 先下载图片到本地临时路径 + wx.downloadFile({ + url: image, + success: (res) => { + if (res.statusCode === 200) { + const tempFilePath = res.tempFilePath; + // 保存到相册 + wx.saveImageToPhotosAlbum({ + filePath: tempFilePath, + success: () => { + wx.showToast({ title: '保存成功', icon: 'success' }); + }, + fail: (err) => { + console.error('保存失败:', err); + wx.showToast({ title: '保存失败', icon: 'none' }); + } + }); + } + }, + fail: (err) => { + console.error('下载失败:', err); + wx.showToast({ title: '图片下载失败', icon: 'none' }); + } + }); + + // // 获取图片的信息 + // uni.getImageInfo({ + // src: image, + // success: (imageInfo) => { + // // 保存图片到手机相册 + // uni.saveImageToPhotosAlbum({ + // filePath: imageInfo.path, + // success: () => { + // uni.showModal({ + // title: '保存成功', + // content: '图片已成功保存到相册', + // showCancel: false + // }) + // resolve() + // }, + // fail: (err) => { + // console.log('保存失败', err) + // reject(new Error('保存失败')) + // }, + // complete: (res) => { + // console.log('保存结果:', res) + // } + // }) + // }, + // fail: (err) => { + // console.log('获取图片信息失败:', err) + // reject(new Error('获取图片信息失败')) + // } + // }) }) },