微信小程序授權(quán)登陸及每次檢查是否授權(quán)實(shí)例代碼
授權(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)注明出處,謝謝!
相關(guān)文章
es6 字符串String的擴(kuò)展(實(shí)例講解)
下面小編就為大家?guī)硪黄猠s6 字符串String的擴(kuò)展(實(shí)例講解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-08-08
javascript實(shí)現(xiàn)一個(gè)網(wǎng)頁(yè)加載進(jìn)度loading
本篇文章主要介紹了javascript實(shí)現(xiàn)一個(gè)頁(yè)面加載進(jìn)度loading的具體步驟以及示例代碼,具有一定的參考價(jià)值,下面跟著小編一起來看下吧2017-01-01
JS實(shí)現(xiàn)根據(jù)指定值刪除數(shù)組中的元素操作示例
這篇文章主要介紹了JS實(shí)現(xiàn)根據(jù)指定值刪除數(shù)組中的元素操作,結(jié)合實(shí)例形式總結(jié)分析了JavaScript針對(duì)數(shù)組元素刪除操作的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-08-08

