初學(xué)微信小程序,總是搞不清app和page生命周期函數(shù)的執(zhí)行先后順序,尤其是在登陸授權(quán)個(gè)人信息這一板塊,最近老是出現(xiàn)問(wèn)題,所以本人仔細(xì)擼了一下代碼,下面是自己的一些見(jiàn)解。因?yàn)閯偨佑|小程序,見(jiàn)解不深,或者有錯(cuò)誤的地方還請(qǐng)諒解,并幫忙指正。
建議在看我寫(xiě)的東西時(shí),先看一下文章微信小程序不支持wx.getUserInfo授權(quán)的解決方法。我感覺(jué)他的文章還是寫(xiě)的比較完整的,對(duì)于新學(xué)者也是好理解的。好,廢話不多說(shuō),大家還是喜歡用代碼說(shuō)話的。注意我打console的位置,還有文筆不好,見(jiàn)諒
app.js
onLaunch: function (options) {
console.log('剛進(jìn)入onLaunch內(nèi)部');
// 展示本地存儲(chǔ)能力
var that = this;
var logs = wx.getStorageSync('logs') || []
logs.unshift(Date.now())
wx.setStorageSync('logs', logs)
// 獲取用戶信息
wx.getSetting({
success: res => {
console.log('app.getSetting異步返回?cái)?shù)據(jù)');
var that = this;
if (res.authSetting['scope.userInfo']) {
// 已經(jīng)授權(quán),可以直接調(diào)用 getUserInfo 獲取頭像昵稱,不會(huì)彈框
wx.getUserInfo({
success: res => {
console.log('app.getUserInfo異步返回?cái)?shù)據(jù)');
// 可以將 res 發(fā)送給后臺(tái)解碼出 unionId
that.globalData.userInfo = res.userInfo;
that.globalData.iv = res.iv;
that.globalData.encryptedData = res.encryptedData;
// 由于 getUserInfo 是網(wǎng)絡(luò)請(qǐng)求,可能會(huì)在 Page.onLoad 之后才返回
// 所以此處加入 callback 以防止這種情況
if (that.userInfoReadyCallback) {
that.userInfoReadyCallback(res)
}
}
})
}
}
});
},
index.js
onLoad: function(options) {
console.log('剛進(jìn)入page.onLoad');
console.log(this.data.canIUse);
var that = this;
wx.showShareMenu({
withShareTicket: true
});
that.setData({
sharedOptions: options
})
if (options.uid != undefined || options.uid != null) {
that.setData({
otherId: options.uid,
});
}
if (app.globalData.userInfo) {
console.log('page.onLoad 第一個(gè)條件');
that.setData({
userInfo: app.globalData.userInfo,
hasUserInfo: true
});
if (that.data.uid == null) {
that.setData({
loadingShow: true
})
that.time(); //調(diào)用倒計(jì)時(shí),5秒后請(qǐng)求數(shù)據(jù)
that.code(); //登錄接口
}
} else if (that.data.canIUse) {
console.log('page.onLoad 第二個(gè)條件');
// 由于 getUserInfo 是網(wǎng)絡(luò)請(qǐng)求,可能會(huì)在 Page.onLoad 之后才返回
// 所以此處加入 callback 以防止這種情況
app.userInfoReadyCallback = res => {
console.log('回調(diào)函數(shù)');
that.setData({
hasUserInfo: true
});
if (that.data.uid == null) {
that.setData({
loadingShow: true
})
that.time(); //調(diào)用倒計(jì)時(shí),5秒后請(qǐng)求數(shù)據(jù)
that.code(); //登錄接口
}
}
} else {
console.log('page.onLoad 第三個(gè)條件');
// 在沒(méi)有 open-type=getUserInfo 版本的兼容處理
wx.getUserInfo({
success: res => {
app.globalData.userInfo = res.userInfo
that.setData({
hasUserInfo: true
});
if (that.data.uid == null) {
that.setData({
loadingShow: true
});
that.time(); //調(diào)用倒計(jì)時(shí),5秒后請(qǐng)求數(shù)據(jù)
that.code(); //登錄接口
}
}
})
};
},
//點(diǎn)擊授權(quán)
getUserInfo: function(e) {
var that = this;
if (e.detail.userInfo != undefined) {
wx.getSetting({
success: res => {
var that = this;
if (res.authSetting['scope.userInfo']) {
console.log('index點(diǎn)擊授權(quán)');
console.log(that.data.canIUse);
// 已經(jīng)授權(quán),可以直接調(diào)用 getUserInfo 獲取頭像昵稱,不會(huì)彈框
wx.getUserInfo({
success: res => {
// 可以將 res 發(fā)送給后臺(tái)解碼出 unionId
app.globalData.userInfo = res.userInfo;
app.globalData.iv = res.iv;
app.globalData.encryptedData = res.encryptedData;
if (that.data.uid == null) {
that.setData({
loadingShow: true
});
that.time(); //調(diào)用倒計(jì)時(shí),5秒后請(qǐng)求數(shù)據(jù)
that.code(); //登錄接口
}
// 由于 getUserInfo 是網(wǎng)絡(luò)請(qǐng)求,可能會(huì)在 Page.onLoad 之后才返回
// 所以此處加入 callback 以防止這種情況
if (that.userInfoReadyCallback) {
that.userInfoReadyCallback(res)
}
}
})
}
}
})
this.setData({
hasUserInfo: true,
})
} else {
wx.showModal({
title: '警告',
content: '您點(diǎn)擊了拒絕授權(quán),將無(wú)法正常使用7k7k的功能體驗(yàn)。',
// success: function (res) {
// if (res.confirm) {
// wx.openSetting({
// success: function (res) {
// if (!res.authSetting["scope.userInfo"] || !res.authSetting["scope.userLocation"]) {
// //這里是授權(quán)成功之后 填寫(xiě)你重新獲取數(shù)據(jù)的js
// //參考:
// that.getLogiCallback('', function () {
// callback('');
// })
// }
// }
// })
// }
// }
})
}
},
onShow: function() {
console.log('page.onShow');
var that = this;
if (app.globalData.uid != null) {
that.setData({
money: app.globalData.money,
gameList: app.globalData.gameList
})
that.pageInitialization(); //獲取欄目
that.indexData(); //獲取用戶信息
that.friendList(); //邀請(qǐng)成功列表
that.shaerPage(); //分享頁(yè)面跳轉(zhuǎn)
}
},
// 獲取臨時(shí)code
code: function() {
console.log('臨時(shí)code');
var that = this;
wx.login({
success: res => {
// 發(fā)送 res.code 到后臺(tái)換取 openId, sessionKey, unionId’
app.globalData.code = res.code; //臨時(shí)code
wx.request({
url: app.globalData.URL + '/api.php?c=system&do=getConfig&p=api',
method: 'GET',
success: function(res) {
app.globalData.categoryId = res.data.data.id;
that.login()
}
})
}
});
// 獲取用戶信息
wx.getSetting({
success: res => {
var that = this;
if (res.authSetting['scope.userInfo']) {
// 已經(jīng)授權(quán),可以直接調(diào)用 getUserInfo 獲取頭像昵稱,不會(huì)彈框
wx.getUserInfo({
success: res => {
// 可以將 res 發(fā)送給后臺(tái)解碼出 unionId
app.globalData.userInfo = res.userInfo;
app.globalData.iv = res.iv;
app.globalData.encryptedData = res.encryptedData;
// 由于 getUserInfo 是網(wǎng)絡(luò)請(qǐng)求,可能會(huì)在 Page.onLoad 之后才返回
// 所以此處加入 callback 以防止這種情況encryptedData)
if (that.userInfoReadyCallback) {
that.userInfoReadyCallback(res)
}
}
})
}
}
})
},
授權(quán)情況下:
控制臺(tái)輸出結(jié)果
image.png
我自己畫(huà)的流程圖
image.png
流程圖分析:
首先觸發(fā)app.onLaunch函數(shù),依次執(zhí)行內(nèi)部代碼行,在運(yùn)行到wx.getSetting函數(shù)時(shí),由于是個(gè)異步,等待結(jié)果返回,但程序繼續(xù)走,進(jìn)行到index的onLoad函數(shù),app.globalData.userInfo為null,所以進(jìn)入第二個(gè)條件的代碼塊,并且定義了app.userInfoReadyCallback函數(shù),然后進(jìn)行到index.onShow,此時(shí)app.js中的getSetting數(shù)據(jù)返回,因?yàn)橐呀?jīng)授權(quán),繼續(xù)wx.getUserInfo函數(shù),userInfoReadyCallback為真,執(zhí)行userInfoReadyCallback函數(shù)
未授權(quán)情況下:
控制臺(tái)輸出:
image.png
自己畫(huà)的流程圖
image.png
流程圖分析:
首先觸發(fā)app.onLaunch函數(shù),依次執(zhí)行內(nèi)部代碼行,在運(yùn)行到wx.getSetting函數(shù)時(shí),由于是個(gè)異步,等待結(jié)果返回,但程序繼續(xù)走,進(jìn)行到index的onLoad函數(shù),app.globalData.userInfo為null,所以進(jìn)入第二個(gè)條件的代碼塊,并且定義了app.userInfoReadyCallback函數(shù),然后進(jìn)行到index.onShow,此時(shí)app.js中的getSetting數(shù)據(jù)返回,因?yàn)槲词跈?quán),wx.getUserInfo函數(shù)不執(zhí)行,所以回調(diào)函數(shù)也不執(zhí)行,只能人為點(diǎn)擊授權(quán),執(zhí)行index頁(yè)面的getUserInfo函數(shù)