微信小程序授權登陸及每次檢查是否授權實例代碼
更新時間:2019年09月18日 10:51:08 作者:Vam的金豆之路
這篇文章主要介紹了關于微信小程序授權登陸及每次檢查是否授權,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
授權登錄
<button open-type="getUserInfo" lang="zh_CN" bindgetuserinfo="onGotUserInfo" class="fix">登錄</button>
//index.js //獲取應用實例 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' // 默認值 }, 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,實則在判斷微信小程序版本是否支持相關屬性 app.userInfoReadyCallback = (res) => { // userInfoReadyCallback:userInfo的回調函數(shù),聲明一個回調函數(shù),將回調函數(shù)傳給app.js,userInfo加載完成后會執(zhí)行這個回調函數(shù),這個回調函數(shù)會將獲取的getUserInfo的結果直接傳回來 // 在app.js中獲取用戶信息之后調用這個函數(shù),結果放在函數(shù)的參數(shù)中 this.setData({ userInfo: res.userInfo, hasUserInfo: true }) } } else { wx.getUserInfo({ //在老的版本中是可以直接調用授權接口并獲取用戶信息 success: (res) => { this.setData({ userInfo: res.userInfo, hasUserInfo: true }) } }) } } })
每次檢查是否授權
//app.js App({ globalData: { userInfo: null, isok:false, unionid:null }, onLaunch: function () { /* 已授權之后,自動獲取用戶信息 */ // 判斷是否授權 wx.getSetting({ success: (res) => { //箭頭函數(shù)為了處理this的指向問題 if (res.authSetting["scope.userInfo"]) { console.log("已授權"); // 獲取用戶信息 wx.getUserInfo({ success: (res) => { //箭頭函數(shù)為了處理this的指向問題 this.globalData.isok=true var that =this console.log(res.userInfo); //用戶信息結果 wx.getStorage({ key: 'unionid', success(res) { that.globalData.unionid=res.data } }) this.globalData.userInfo = res.userInfo; if (this.userInfoReadyCallback) { //當index.js獲取到了globalData就不需要回調函數(shù)了,所以回調函數(shù)需要做做一個判斷,如果app.js中有和這個回調函數(shù),那么就對這個函數(shù)進行調用,并將請求到的結果傳到index.js中 this.userInfoReadyCallback(res.userInfo); } } }) } else{ console.log("未授權"); wx.removeStorage({ key: 'unionid' }) } } }) } })
總結
以上所述是小編給大家介紹的微信小程序授權登陸及每次檢查是否授權實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉載,煩請注明出處,謝謝!
相關文章
javascript實現(xiàn)一個網(wǎng)頁加載進度loading
本篇文章主要介紹了javascript實現(xiàn)一個頁面加載進度loading的具體步驟以及示例代碼,具有一定的參考價值,下面跟著小編一起來看下吧2017-01-01JS實現(xiàn)根據(jù)指定值刪除數(shù)組中的元素操作示例
這篇文章主要介紹了JS實現(xiàn)根據(jù)指定值刪除數(shù)組中的元素操作,結合實例形式總結分析了JavaScript針對數(shù)組元素刪除操作的相關實現(xiàn)技巧,需要的朋友可以參考下2018-08-08