Browse Source

feat(图片保存): 优化图片保存到相册功能并添加权限描述

修改图片保存逻辑,先下载图片到本地再保存,提高成功率
在manifest.json中添加相册权限描述
master
前端-胡立永 4 days ago
parent
commit
689b1462b2
2 changed files with 59 additions and 30 deletions
  1. +4
    -1
      manifest.json
  2. +55
    -29
      pages/subcomponent/promo-qrcode.vue

+ 4
- 1
manifest.json View File

@ -59,7 +59,10 @@
"permission" : { "permission" : {
"scope.userLocation" : { "scope.userLocation" : {
"desc" : "您的位置信息将用于选择寄件地址" "desc" : "您的位置信息将用于选择寄件地址"
}
},
"scope.writePhotosAlbum": {
"desc": "需要保存图片到您的相册"
}
} }
}, },
"mp-alipay" : { "mp-alipay" : {


+ 55
- 29
pages/subcomponent/promo-qrcode.vue View File

@ -177,35 +177,61 @@ export default {
// //
imgApi(image) { imgApi(image) {
return new Promise((resolve, reject) => { 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(''))
// }
// })
}) })
}, },


Loading…
Cancel
Save