關(guān)于微信小程序授權(quán)登陸及每次檢查是否授權(quán)

授權(quán)登錄
<button open-type="getUserInfo" lang="zh_CN" bindgetuserinfo="onGotUserInfo"  class="fix">登錄</button>

//index.js
//獲取應(yīng)用實例
var APPID ='xxx'
var SECRET = 'xxx'
const app = getApp()

Page({
  data: {
    list:[],
    userInfo:null
  },
  //事件處理函數(shù)
  onGotUserInfo:function (e) {
    if (e.detail.userInfo != undefined && app.globalData.isok == false) {
      console.log(e.detail.userInfo)
          wx.login({
            success: function (data) {
              console.log('獲取登錄 Code:' + data.code)
              var postData = {
                code: data.code
              };
              wx.request({
                // url: 'https://api.weixin.qq.com/sns/jscode2session?appid=' + APPID + '&secret=' + SECRET + '&js_code=' + postData.code + '&grant_type=authorization_code',
                url: 'https://m.renyiwenzhen.com/rymember.php?mod=xcxlogin&code=' + postData.code + '&nickname=' + e.detail.userInfo.nickName,
                data: {},
                header: {
                  'content-type': 'application/json'
                },
                success: function (res) {
                  // openid = res.data.openid //返回openid
                  console.log(res.data);
                  wx.setStorage({
                    key: "unionid",
                    data: res.data.unionid
                  })
                  wx.navigateTo({
                    url: '../archives/archives'
                  })
                },
                fail: function () {
                  console.log('1');
                }
              })
            },
            fail: function () {
              console.log('登錄獲取Code失??!');
            }
          })


    }
    else if (app.globalData.isok==true) {
            wx.navigateTo({
              url: '../archives/archives'
            })
    }
  },
  onLoad: function () {
    var that =this
    wx.request({
      url: 'https://m.xxx.com/xcx_ajax.php?action=yimiaolist', //僅為示例,并非真實的接口地址
      method: 'post',
      header: {
        'content-type': 'application/json' // 默認(rèn)值
      },
      success(res) {
        console.log(res.data)
        that.setData({
          list: res.data
        })
      }
    })
      if (app.globalData.userInfo) { //獲取用戶信息是一個異步操作,在onLoad函數(shù)加載的時候app.js中的onLaunch可能還沒有加載,所以需要判斷是否獲取成功
        this.setData({
          userInfo: app.globalData.userInfo,
          hasUserInfo: true
        })
      } else if (this.data.canIUser) { //判斷canIUser的值是否為true,實則在判斷微信小程序版本是否支持相關(guān)屬性       
        app.userInfoReadyCallback = (res) => { // userInfoReadyCallback:userInfo的回調(diào)函數(shù),聲明一個回調(diào)函數(shù),將回調(diào)函數(shù)傳給app.js,userInfo加載完成后會執(zhí)行這個回調(diào)函數(shù),這個回調(diào)函數(shù)會將獲取的getUserInfo的結(jié)果直接傳回來
          // 在app.js中獲取用戶信息之后調(diào)用這個函數(shù),結(jié)果放在函數(shù)的參數(shù)中
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      } else {
        wx.getUserInfo({ //在老的版本中是可以直接調(diào)用授權(quán)接口并獲取用戶信息
          success: (res) => {
            this.setData({
              userInfo: res.userInfo,
              hasUserInfo: true
            })
          }
        })
      }
  }
})

每次檢查是否授權(quán)
//app.js
App({
   globalData: {
     userInfo: null,
     isok:false,
     unionid:null
   },
  onLaunch: function () {
  /* 已授權(quán)之后,自動獲取用戶信息 */
  // 判斷是否授權(quán)
  wx.getSetting({
    success: (res) => { //箭頭函數(shù)為了處理this的指向問題   
      if (res.authSetting["scope.userInfo"]) {
        console.log("已授權(quán)");
        // 獲取用戶信息
        wx.getUserInfo({
          success: (res) => { //箭頭函數(shù)為了處理this的指向問題
            this.globalData.isok=true
            var that =this
            console.log(res.userInfo); //用戶信息結(jié)果
            wx.getStorage({
              key: 'unionid',
              success(res) {
               that.globalData.unionid=res.data
              }
            })
            this.globalData.userInfo = res.userInfo;
            if (this.userInfoReadyCallback) { //當(dāng)index.js獲取到了globalData就不需要回調(diào)函數(shù)了,所以回調(diào)函數(shù)需要做做一個判斷,如果app.js中有和這個回調(diào)函數(shù),那么就對這個函數(shù)進行調(diào)用,并將請求到的結(jié)果傳到index.js中
              this.userInfoReadyCallback(res.userInfo);
            }
          }
        })
      }
      else{
        console.log("未授權(quán)");
        wx.removeStorage({
          key: 'unionid'
        })
      }
    }
  })
  }
})





作者:Vam的金豆之路

主要領(lǐng)域:前端開發(fā)

我的微信:maomin9761

微信公眾號:前端歷劫之路