authorizeSaveImg() { //重新授权相册
const that = this;
wx.getSetting({
success(res) {
// 第一,直接调取保存,系统会自动调取授权
if (res.authSetting['scope.writePhotosAlbum']) {
that.canvasToImage();
} else { // 授权
wx.authorize({
scope: 'scope.writePhotosAlbum',
success() {
// 授权成功
that.canvasToImage();
},
fail: function() {
// 授权失败
wx.showModal({
title: '警告',
content: '您点击了拒绝授权,将无法正常保存图片,点击确定重新获取授权。',
success: function(res) {
if (res.confirm) {
wx.openSetting({
success: (res) => {
console.log('授权成功')
that.canvasToImage();
}
})
}
}
})
}
})
}
that.$apply()
}
})
},
canvasToImage() { // canvas画布转成图片
var that = this;
wx.canvasToTempFilePath({
quality: 1,
fileType: 'jpg',
canvasId: 'mycanvas',
success: function(res) {
wx.hideLoading();
console.log('图片地址' + res.tempFilePath);
that.setData({
img_temp: res.tempFilePath
})
// wx.previewImage({
// current: res.tempFilePath, // 当前显示图片的http链接
// urls: [res.tempFilePath] // 需要预览的图片http链接列表
// })
wx.saveImageToPhotosAlbum({
filePath: res.tempFilePath,
success(res) {
console.log(res);
wx.showModal({
title: '',
content: '图片已保存到相册',
showCancel: false,
confirmText: '好的',
confirmColor: '#72B9C3',
success: function(res) {
if (res.confirm) {
console.log('用户点击确定');
}
}
})
},
fail: function(res) {
if (res.errMsg === "saveImageToPhotosAlbum:fail auth deny" || res.errMsg ==='saveImageToPhotosAlbum:fail:auth denied') {
that.authorizeSaveImg();
} else if (res.errMsg === 'saveImageToPhotosAlbum:fail cancel') {
wx.showToast({
title: '取消保存',
icon: 'none',
duration: 2000
})
}
}
})
},
fail: function(res) {
console.log(res)
}
}, this)
},