溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

微信小程序授權(quán)登陸及每次檢查是否授權(quán)實(shí)例代碼

發(fā)布時(shí)間:2020-09-13 21:55:36 來源:腳本之家 閱讀:209 作者:Vam的金豆之路 欄目:web開發(fā)

授權(quán)登錄

<button open-type="getUserInfo" lang="zh_CN" bindgetuserinfo="onGotUserInfo" class="fix">登錄</button>

//index.js
//獲取應(yīng)用實(shí)例
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', //僅為示例,并非真實(shí)的接口地址
   method: 'post',
   header: {
    'content-type': 'application/json' // 默認(rèn)值
   },
   success(res) {
    console.log(res.data)
    that.setData({
     list: res.data
    })
   }
  })
   if (app.globalData.userInfo) { //獲取用戶信息是一個(gè)異步操作,在onLoad函數(shù)加載的時(shí)候app.js中的onLaunch可能還沒有加載,所以需要判斷是否獲取成功
    this.setData({
     userInfo: app.globalData.userInfo,
     hasUserInfo: true
    })
   } else if (this.data.canIUser) { //判斷canIUser的值是否為true,實(shí)則在判斷微信小程序版本是否支持相關(guān)屬性 
    app.userInfoReadyCallback = (res) => { // userInfoReadyCallback:userInfo的回調(diào)函數(shù),聲明一個(gè)回調(diào)函數(shù),將回調(diào)函數(shù)傳給app.js,userInfo加載完成后會(huì)執(zhí)行這個(gè)回調(diào)函數(shù),這個(gè)回調(diào)函數(shù)會(huì)將獲取的getUserInfo的結(jié)果直接傳回來
     // 在app.js中獲取用戶信息之后調(diào)用這個(gè)函數(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)之后,自動(dòng)獲取用戶信息 */
 // 判斷是否授權(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ù)需要做做一個(gè)判斷,如果app.js中有和這個(gè)回調(diào)函數(shù),那么就對(duì)這個(gè)函數(shù)進(jìn)行調(diào)用,并將請(qǐng)求到的結(jié)果傳到index.js中
       this.userInfoReadyCallback(res.userInfo);
      }
     }
    })
   }
   else{
    console.log("未授權(quán)");
    wx.removeStorage({
     key: 'unionid'
    })
   }
  }
 })
 }
})

總結(jié)

以上所述是小編給大家介紹的微信小程序授權(quán)登陸及每次檢查是否授權(quán)實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)億速云網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI