欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

微信小程序 wx.getUserInfo引導用戶授權問題實例分析

 更新時間:2020年03月09日 08:42:48   作者:WFaceBoss  
這篇文章主要介紹了微信小程序 wx.getUserInfo引導用戶授權問題,結合實例形式分析了微信小程序使用wx.getUserInfo引導用戶授權問題的具體操作步驟與實現(xiàn)方法,需要的朋友可以參考下

本文實例講述了微信小程序 wx.getUserInfo引導用戶授權問題。分享給大家供大家參考,具體如下:

首先,在page外定義一個函數(shù)用戶判斷是否為空對象

var isEmptyObject = function (e) {
 var temp;
 for (temp in e)
  return !1;
 return !0
}

然后,在page中的onload里面調用授權

onLoad: function () {
  var that = this;
  if (app.globalData.userInfo) {
   this.setData({
    userInfo: app.globalData.userInfo
   })
  } else if (this.data.canIUse) {
   // 由于 getUserInfo 是網(wǎng)絡請求,可能會在 Page.onLoad 之后才返回
   // 所以此處加入 callback 以防止這種情況
   app.userInfoReadyCallback = res => {
    this.setData({
     userInfo: res.userInfo
    })
   }
  } else {
   // 在沒有 open-type=getUserInfo 版本的兼容處理
   wx.getUserInfo({
    success: res => {
     app.globalData.userInfo = res.userInfo
     this.setData({
      userInfo: res.userInfo
     })
     that.checkSettingStatu();
    },
    fail: function () {
     wx.showModal({
      title: '用戶未授權',
      content: '如需正常使用該小程序功能,請按確定并在授權管理中選中“用戶信息”,然后點按確定。最后再重新進入小程序即可正常使用。',
      showCancel: false,
      success: function (resbtn) {
       if (resbtn.confirm) {
        wx.openSetting({
         success: function success(resopen) {
          // 獲取用戶數(shù)據(jù)
          that.checkSettingStatu();
         }
        });
       }
      }
     })
    }
   })
  }
 }

最后,在page中定義一個 用于檢測 當前授權的狀態(tài)

checkSettingStatu: function (cb) {
  var that = this;
  // 判斷是否是第一次授權,非第一次授權且授權失敗則進行提醒
  wx.getSetting({
   success: function success(res) {
    var authSetting = res.authSetting;
    if (isEmptyObject(authSetting)) {
        //第一次
    } else {
     // 沒有授權的提醒
     if (authSetting['scope.userInfo'] === false) {
      wx.showModal({
       title: '用戶未授權',
       content: '如需正常使用該小程序功能,請按確定并在授權管理中選中“用戶信息”,然后點按確定。最后再重新進入小程序即可正常使用。',
       showCancel: false,
       success: function (res) {
        if (res.confirm) {
         wx.openSetting({
          success: function success(res) {
           console.log()
          }
         });
        }
       }
      })
     } else if (authSetting['scope.userInfo'] === true) {
           //該處用戶獲取用戶的一些授權信息
      if (that.data.userInfo) {
       var nickname = that.data.userInfo.nickName;
       var gender = that.data.userInfo.gender
       //性別 0:未知、1:男、2:女
       if (gender == 1) {
        gender = "True"
       } else if (gender == 2) {
        gender = "False"
       } else {
        gender = "True"
       }
     
      }
     }
    }
   }
  })
 }

簡單的記錄,不喜勿噴。

希望本文所述對大家微信小程序開發(fā)有所幫助。

相關文章

最新評論