小程序-保存圖片,用戶授權,拒絕授權等操作


<image class='recode' data-img='{{dataInfo.qrcode}}' bindtap='onSavePicClick' src='{{dataInfo.qrcode}}'></image>
1、用戶點擊圖片彈出授權框
2、網絡路徑圖片
3、用戶點擊了拒絕
4、getSetting事件需要tap觸發
downloadImage: function (imageUrl) {
    // 下載文件  
    wx.downloadFile({
      url: imageUrl,
      success: function (res) {
        // 保存圖片到系統相冊  
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success(res) {
            wx.showToast({
              title: '保存成功',
            });
          },
          fail(res) {
            console.log("保存圖片:fail");
          }
        })
      },
      fail: function (res) {
        console.log("下載文件:fail");
      }
    })
  },
  onSavePicClick: function (e) {
    var that = this;
    console.log("onSavePicClick");
    console.log(e);
    var downloadUrl = e.currentTarget.dataset.img;
 
    if (!wx.saveImageToPhotosAlbum) {
      wx.showModal({
        title: '提示',
        content: '當前微信版本過低,無法使用該功能,請升級到最新微信版本后重試。'
      })
      return;
    }
 
    // 可以通過 wx.getSetting 先查詢一下用戶是否授權了 "scope.writePhotosAlbum" 這個 scope  
    wx.getSetting({
      success(res) {
        console.log("getSetting: success");
        if (!res.authSetting['scope.writePhotosAlbum']) {
          // 接口調用詢問  
          wx.authorize({
            scope: 'scope.writePhotosAlbum',
            success() {
              that.downloadImage(downloadUrl);
            },
            fail() {
              // 用戶拒絕了授權  
              wx.showModal({
                title: '保存圖片',
                content: '保存圖片需要您授權',
                showCancel: true,
                confirmText: '確定',
 
                success: function (res) {
                  if (res.confirm) {
                    // 打開設置頁面  
                    wx.openSetting({
                      success: function (data) {
                        if (data.authSetting['scope.writePhotosAlbum']) {
                          that.downloadImage(downloadUrl);
                        } else {
                          console.log("授權失敗");
                        }
                      },
                      fail: function (data) {
                        console.log("openSetting: fail");
                      }
                    });
                  } else if (res.cancel) {
                    console.log('用戶點擊取消')
                  }
 
                }
              })
 
 
 
            }
          })
        } else {
          that.downloadImage(downloadUrl)
        }
      },
      fail(res) {
        console.log("getSetting: fail");
        console.log(res);
      }
 
    })
 
  },
downloadImage: function (imageUrl) {
// 下載文件  
wx.downloadFile({
url: imageUrl,
success: function (res) {
console.log("下載文件:success");
console.log(res);

// 保存圖片到系統相冊  
wx.saveImageToPhotosAlbum({
filePath: res.tempFilePath,
success(res) {
console.log("保存圖片:success");
wx.showToast({
title: '保存成功',
});
},
fail(res) {
console.log("保存圖片:fail");
console.log(res);
}
})
},
fail: function (res) {
console.log("下載文件:fail");
console.log(res);
}
})
},
onSavePicClick: function (e) {
var that = this;
console.log("onSavePicClick");
console.log(e);
var downloadUrl = e.currentTarget.dataset.img;

if (!wx.saveImageToPhotosAlbum) {
wx.showModal({
title: '提示',
content: '當前微信版本過低,無法使用該功能,請升級到最新微信版本后重試。'
})
return;
}

// 可以通過 wx.getSetting 先查詢一下用戶是否授權了 "scope.writePhotosAlbum" 這個 scope  
wx.getSetting({

success(res) {
console.log("getSetting: success");
if (!res.authSetting['scope.writePhotosAlbum']) {
// 接口調用詢問  
wx.authorize({
scope: 'scope.writePhotosAlbum',
success() {
that.downloadImage(downloadUrl);
},
fail() {
// 用戶拒絕了授權  
wx.showModal({
title: '保存圖片',
content: '保存圖片需要您授權',
showCancel: true,
confirmText: '確定',

success: function (res) {
if (res.confirm) {
console.log(12134);
// 打開設置頁面  
wx.openSetting({
success: function (data) {
if (data.authSetting['scope.writePhotosAlbum']) {
console.log("授權成功");
that.downloadImage(downloadUrl);
} else {
console.log("授權失敗");
}
},
fail: function (data) {
console.log("openSetting: fail");
}
});
} else if (res.cancel) {
console.log('用戶點擊取消')
}

}
})



}
})
} else {
that.downloadImage(downloadUrl)
}
},
fail(res) {
console.log("getSetting: fail");
console.log(res);
}

})

},


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM