// 下载预览文件
onSaveClick: function (e) {
let _this = this;
let path = e.currentTarget.dataset.path;
wx.showLoading({
title: '下载中...',
})
var filename = new Date().getTime() + ".jpg";
var savePath = wx.env.USER_DATA_PATH + "/" + filename
wx.downloadFile({
header: 'content-type: image/jpg',
url: path,
filePath: savePath,
success: function (res) {
wx.hideLoading({
success: (res) => {},
})
var filePath = res.filePath
_this.setData({
filePath: filePath
})
wx.getSetting({
success: (res) => {
console.log('getSetting', res);
var writePhotosAlbum = res.authSetting['scope.writePhotosAlbum'];
if (writePhotosAlbum == true) {
_this.saveImg()
} else if (writePhotosAlbum == false) {
wx.showModal({
title: '是否授权保存到相册',
content: '需要获取您的保存图片权限,请确认授权,否则图片将无法保存到相册',
success: function (tip) {
if (tip.confirm) {
wx.openSetting({
success: function (data) {
if (data.authSetting["scope.writePhotosAlbum"] === true) {
_this.saveImg()
} else {
wx.showToast({
title: '授权失败',
})
}
}
})
}
}
})
} else {
_this.saveImg()
}
},
fail: function (res) {},
complete: () => {}
})
}
})
},
saveImg: function () {
var filePath = this.data.filePath
wx.showLoading({
title: '保存中...',
})
wx.saveImageToPhotosAlbum({
filePath: filePath,
success: function (res) {
wx.hideLoading({
success: (res) => {},
})
wx.showToast({
title: '保存成功',
icon: 'success',
duration: 1500,
})
},
fail: function (res) {
wx.showToast({
title: "保存失败:" + res.errMsg,
icon: 'none',
duration: 1500,
})
},
complete: function (res) {},
})
},