微信小程序 --图片--保存到相册
2022-05-17 本文已影响0人
徐福瑞
image.png
wxml
wxml
<image class="qrimg" src="{{LocalApiBaseUrl}}/{{info.promotion_image}}">
</image>
<view class="right" bindtap="saveImg">保存到相册</view>
js
wx.getSetting :获取用户的当前设置返回值中只会出现小程序已经向用户请求过的权限
//保存到相册
saveImg(e) {
var that = this
// 可以通过 wx.getSetting 先查询一下用户是否授权了 "scope.writePhotosAlbum" 这个 scope
wx.getSetting({
success: function (res) {
if (!res.authSetting['scope.writePhotosAlbum']) {
wx.authorize({
scope: 'scope.writePhotosAlbum',
success() { //这里是用户同意授权后的回调
that.saveImageToPhotosAlbumSure();
},
fail() { //这里是用户拒绝授权后的回调
wx.openSetting({
success: function (res) {},
fail: function (e) {},
})
}
})
} else {
that.saveImageToPhotosAlbumSure();
}
},
fail: function (res) {},
})
},
// 保存到相册
saveImageToPhotosAlbumSure: function (e) {
var that = this;
console.log()
wx.downloadFile({
url: that.data.LocalApiBaseUrl + '/' + that.data.info.promotion_image, //下载地址+需要下载的图片
success: function (res) {
// 只要服务器有响应数据,就会把响应内容写入文件并进入 success 回调,业务需要自行判断是否下载到了想要的内容
console.log(res);
if (res.statusCode === 200) {
wx.saveImageToPhotosAlbum({
filePath: res.tempFilePath,
success(res) {
wx.showModal({
title: '提示',
content: '保存相册成功',
showCancel: false,
success: function (res) {
if (res.confirm) {
that.setData({
disabled: false
})
}
}
})
},
fail: function (res) {
console.log(res.errMsg);
},
})
}
}
})
},