网友真实露脸自拍10p,成人国产精品秘?久久久按摩,国产精品久久久久久无码不卡,成人免费区一区二区三区

小程序模板網

微信小程序開發問答《五十四》同步請求授權 & 用戶拒絕授權,重新調起授權

發布時間:2017-12-26 11:42 所屬欄目:小程序開發教程

需求分析:1.在小程序首次打開的時候,我需要同時請求獲取多個權限,由用戶逐一授權。()問題分析:1. wx.authorize接口同時調用,請求多個權限,由于異步原因,將授權請求一并發出,顯然不符合要求。2. promise能很 ...

 
 
 

1、同步請求授權

作者:xiaochun365

需求分析: 
1.在小程序首次打開的時候,我需要同時請求獲取多個權限,由用戶逐一授權。 
([‘scope.userInfo’,‘scope.userLocation’,‘scope.address’,‘scope.record’,‘scope.writePhotosAlbum’]) 
問題分析: 
1. wx.authorize接口同時調用,請求多個權限,由于異步原因,將授權請求一并發出,顯然不符合要求。 
2. promise能很好的解決問題,試著嘗試了一下,下面代碼分為兩個文件。

//  scope.js
import es6 from '../helpers/es6-promise'

//  獲取用戶授權
function getScope(scopeName) {
  return new es6.Promise(function (resolve, reject) {
    //  查詢授權
    wx.getSetting({
      success(res) {
        if (!res.authSetting[scopeName]) {
          //  發起授權
          wx.authorize({
            scope: scopeName,
            success() {
              resolve(0)
            }, fail() {
              resolve(1)
            }
          })
        }
      }
    })
  })
}

module.exports = { getScope: getScope }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
//  index.js
import scope from "../../service/scope"
Page({
onShow() {
    let list = ["scope.userInfo", "scope.userLocation", "scope.address", "scope.record"];
    //  記錄請求結果
    let num = 0;
    //  問題1:怎么改成循環方式?
    scope.getScope(list[0]).then(function (res) {
      num += res;
      scope.getScope(list[1]).then(function (res) {
        num += res;
        scope.getScope(list[2]).then(function (res) {
          num += res;
          scope.getScope(list[3]).then(function (res) {
            num += res;
            //  調起設置界面
            if (num) {
              wx.openSetting({
                success(res) {
                  //  允許獲取用戶信息
                  if (res.authSetting["scope.userInfo"])
                    userService.login()
                }
              })
            } else {
              userService.login()
            }
          })
        })
      })
    })
})
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

分析求解:  1.代碼中問題1寫法過于笨,但是嘗試通過循環方式調用寫法,又不知道如何處理回調問題。  2.wx.authorize接口,success參數官方給出的解釋是(接口調用成功的回調函數),其實不然,實際上是接口調用成功,并且獲取到了scope指定的權限.

2、用戶拒絕授權,重新調起授權

作者:老鄧頭

 

		
  1. onLoad: function (options) {
  2.  
  3. console.log("onLoad=====");
  4. var that=this;
  5. wx.getUserInfo({
  6. success:function(res){
  7. var userInfo = res.userInfo;
  8. that.setData({
  9. nickName: userInfo.nickName,
  10. avatarUrl: userInfo.avatarUrl,
  11. })
  12. },fail:function(){
  13. wx.showModal({
  14. title: '警告',
  15. content: '您點擊了拒絕授權,將無法正常顯示個人信息,點擊確定重新獲取授權。',
  16. success:function(res){
  17. if (res.confirm){
  18. wx.openSetting({
  19. success: (res) => {
  20. if (res.authSetting["scope.userInfo"]){////如果用戶重新同意了授權登錄
  21. wx.getUserInfo({
  22. success:function(res){
  23. var userInfo = res.userInfo;
  24. that.setData({
  25. nickName:userInfo.nickName,
  26. avatarUrl:userInfo.avatarUrl,
  27. })
  28. }
  29. })
  30. }
  31. },fail:function(res){
  32.  
  33. }
  34. })
  35.  
  36. }
  37. }
  38. })
  39. }, complete: function (res){
  40.  
  41.  
  42. }
  43. })
  44. }



易優小程序(企業版)+靈活api+前后代碼開源 碼云倉庫:starfork
本文地址:http://www.xiuhaier.com/wxmini/doc/course/18248.html 復制鏈接 如需定制請聯系易優客服咨詢:800182392 點擊咨詢
QQ在線咨詢
AI智能客服 ×