SaveCard: function(e) { let that = this; console.log('保存'); var imgSrc = e.currentTarget.dataset.img; //獲取相冊授權 wx.getSetting({ success(res) { if (!res.authSetting['scope.writePhotosAlbum']) { wx.authorize({ scope: 'scope.writePhotosAlbum', success() { console.log('授權成功'); that.img(imgSrc) } }) }else{ that.img(imgSrc) } } }) }, img: function (imgSrc){ var imgSrc = imgSrc; wx.downloadFile({ url: imgSrc, success: function (res) { console.log(res); //圖片保存到本地 wx.saveImageToPhotosAlbum({ filePath: res.tempFilePath, success: function (data) { console.log(data); wx.showToast({ title: '保存成功', duration: 2000 }) }, fail: function (err) { console.log(err); if (err.errMsg === "saveImageToPhotosAlbum:fail auth deny") { wx.openSetting({ success(settingdata) { console.log(settingdata) if (settingdata.authSetting['scope.writePhotosAlbum']) { wx.showToast({ title: '圖片已保存', icon:'none', duration:2000 }) console.log('獲取權限成功,給出再次點擊圖片保存到相冊的提示。') } else { console.log('獲取權限失敗,給出不給權限就無法正常使用的提示') } } }) } } }) } }) },
先授權,再保存