亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

微信小程序 wx.getUserInfo引導(dǎo)用戶授權(quán)問題實(shí)例分析

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

本文實(shí)例講述了微信小程序 wx.getUserInfo引導(dǎo)用戶授權(quán)問題。分享給大家供大家參考,具體如下:

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

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

然后,在page中的onload里面調(diào)用授權(quán)

onLoad: function () {
  var that = this;
  if (app.globalData.userInfo) {
   this.setData({
    userInfo: app.globalData.userInfo
   })
  } else if (this.data.canIUse) {
   // 由于 getUserInfo 是網(wǎng)絡(luò)請求,可能會在 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: '用戶未授權(quán)',
      content: '如需正常使用該小程序功能,請按確定并在授權(quán)管理中選中“用戶信息”,然后點(diǎn)按確定。最后再重新進(jìn)入小程序即可正常使用。',
      showCancel: false,
      success: function (resbtn) {
       if (resbtn.confirm) {
        wx.openSetting({
         success: function success(resopen) {
          // 獲取用戶數(shù)據(jù)
          that.checkSettingStatu();
         }
        });
       }
      }
     })
    }
   })
  }
 }

最后,在page中定義一個(gè) 用于檢測 當(dāng)前授權(quán)的狀態(tài)

checkSettingStatu: function (cb) {
  var that = this;
  // 判斷是否是第一次授權(quán),非第一次授權(quán)且授權(quán)失敗則進(jìn)行提醒
  wx.getSetting({
   success: function success(res) {
    var authSetting = res.authSetting;
    if (isEmptyObject(authSetting)) {
        //第一次
    } else {
     // 沒有授權(quán)的提醒
     if (authSetting['scope.userInfo'] === false) {
      wx.showModal({
       title: '用戶未授權(quán)',
       content: '如需正常使用該小程序功能,請按確定并在授權(quán)管理中選中“用戶信息”,然后點(diǎn)按確定。最后再重新進(jìn)入小程序即可正常使用。',
       showCancel: false,
       success: function (res) {
        if (res.confirm) {
         wx.openSetting({
          success: function success(res) {
           console.log()
          }
         });
        }
       }
      })
     } else if (authSetting['scope.userInfo'] === true) {
           //該處用戶獲取用戶的一些授權(quán)信息
      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ā)有所幫助。

相關(guān)文章

最新評論