首頁(yè)沒(méi)有進(jìn)行強(qiáng)制授權(quán)的登錄,注意其中的回調(diào)情況舞痰,token的獲取可能在頁(yè)面的onLoad之后,注意頁(yè)面上的寫(xiě)法
//app.js
let?interfaces=require('./utils/urlConfig.js');
App({
??onLaunch:?function?()?{
???this.login();???
??},
??globalData:?{
????userInfo:?null,
????screenHeight:667,
????openId:'',
????WxVIPId:'',
????UserType:0,
????imgUrl:'XXXXXXX',
????requestUrl:"XXXXX"
??},
??getUserInfo(){
????//?獲取用戶信息
????let?self=this;
????wx.getSetting({
??????success:?res?=>?{
????????if?(res.authSetting['scope.userInfo'])?{
??????????//?已經(jīng)授權(quán),可以直接調(diào)用?getUserInfo?獲取頭像昵稱(chēng)型宙,
??????????wx.getUserInfo({
????????????success:?res?=>?{
??????????????//?可以將?res?發(fā)送給后臺(tái)解碼出?unionId
??????????????if(res.errMsg=="getUserInfo:ok"){
????????????????//需要更新數(shù)據(jù)庫(kù)個(gè)人信息
????????????????this.globalData.userInfo?=?res.userInfo????????????????
????????????????self.request({
??????????????????url:interfaces.updateUserInfo,
??????????????????method:"POST",
??????????????????data:{
????????????????????"WxVIPId":?self.globalData.WxVIPId,
????????????????????"nickname":?res.userInfo.nickName,
????????????????????"sex":?res.userInfo.gender,
????????????????????"province":?res.userInfo.province,
????????????????????"city":?res.userInfo.city,
????????????????????"country":?res.userInfo.country,
????????????????????"headimgurl":?res.userInfo.avatarUrl
??????????????????},
??????????????????callback:(res)=>{
??????????????????}
????????????????})
????????????????//?由于?getUserInfo?是網(wǎng)絡(luò)請(qǐng)求,可能會(huì)在?Page.onLoad?之后才返回
????????????????//?所以此處加入?callback?以防止這種情況
????????????????if?(this.userInfoReadyCallback)?{
??????????????????this.userInfoReadyCallback(res.userInfo)
????????????????}
??????????????}
????????????}
??????????})
????????}
??????}
????})
??},
??login(){
????let?self=this;
????wx.login({
??????success:?res?=>?{
????????//?發(fā)送?res.code?到后臺(tái)換取?openId,?sessionKey,?unionId
????????wx.request({
??????????url:?this.globalData.requestUrl+interfaces.login,?//僅為示例伦吠,并非真實(shí)的接口地址
??????????data:?{Code:res.code},
??????????method:"GET",
??????????header:?{
????????????'content-type':?'application/json',
????????????"Authorization":?`Bearer`,??
?????????????"RuntoAccessType":"Open"?????
??????????},
??????????success?(resp)?{????????????
????????????if(resp.data.Code==200){
??????????????let?info=resp.data.Data;
??????????????wx.setStorageSync('openId',?info.openId);
??????????????wx.setStorageSync('token',?info.token);
??????????????wx.setStorageSync('sessionId',?info.sessionId);
??????????????self.globalData.openId=info.openId;
??????????????self.getUserInfo();
??????????????if?(self.loginReadyCallback)?{????????????????
????????????????self.loginReadyCallback(resp)
??????????????}
????????????}else{
? ? ? ? ? ? ? ? wx.showToast({
????????????????????title: res.data.Message,
??????????????????????icon:?'none',
??????????????????????duration:?2000
????????????????})
????????????}
??????????},
??????????fail(){
????????????wx.showToast({
??????????????title:?"登陸失斪倍摇!",
??????????????icon:?'none',
??????????????duration:?2000
????????????})
??????????}
????????})
??????}
????})
??},
??//封裝的請(qǐng)求
??request({
????url,
????method?=?'GET',
????data,
????callback,
????noPop?=?false,
????showError=false
???})?{
?????let?self=this;
????let?token?=?''
????if?(wx.getStorageSync('token'))?{
??????token?=?wx.getStorageSync('token')
????}
????wx.showLoading({
??????title:?'加載中'
????})
????wx.request({
??????header:?{
????????'content-type':?'application/json',
???????"Authorization":?`Bearer?${token}`,??
????????"RuntoAccessType":"Open"?????
??????},
??????url:?this.globalData.requestUrl+url,
??????method:?method,
??????data:?data,
??????success(res)?{?????
????????//隱藏導(dǎo)航條加載動(dòng)畫(huà)
????????wx.hideNavigationBarLoading();
????????//停止下拉刷新
????????wx.stopPullDownRefresh();???
????????wx.hideLoading()
????????if(res.statusCode==200){//正常情況
??????????if?(res.data.Code?==?200)?{
????????????callback(res)
??????????}?else?{
????????????if(res.data.Code==401){//登錄超時(shí)
??????????????wx.showToast({
??????????????????title:?res.data.Message???res.data.Message?:?'未知錯(cuò)誤',
??????????????????icon:?'none',
??????????????????duration:?2000
??????????????})
??????????????self.login();??????????
????????????}
????????????else{
??????????????if?(noPop)?{}?else?{????????????????
????????????????wx.showToast({
??????????????????title:?res.data.Message???res.data.Message?:?'未知錯(cuò)誤',
??????????????????icon:?'none',
??????????????????duration:?2000,
??????????????????success(){
????????????????????if(showError){
??????????????????????callback(res)
????????????????????}
??????????????????}
????????????????})
??????????????}
????????????}
??????????}
????????}
????????if(res.statusCode==401){?//token失效毛仪,重新獲取
??????????let?WxVIPId=wx.getStorageSync('WxVIPId');
??????????wx.request({
????????????header:?{
??????????????'content-type':?'application/json'???
????????????},
????????????url:?interfaces.getToken+`?GetTokenType=4&WxVIPId=${WxVIPId}`,
????????????method:?"GET",
????????????success(re){
??????????????if(re.data.Code?==?200){//按照自己后臺(tái)返回的數(shù)據(jù)情況進(jìn)行判斷
????????????????wx.setStorageSync('token',re.data.data.access_token);
????????????????wx.showToast({
????????????????????title:?"服務(wù)器剛剛開(kāi)了個(gè)小差搁嗓,請(qǐng)重新操作?~",
????????????????????icon:?'none',
????????????????????duration:?2000
??????????????????})
??????????????}
????????????},
????????????fail(res)?{
??????????????self.login();???????
????????????}
??????????})
????????}
??????},
??????fail(res)?{
????????wx.hideLoading();
????????//隱藏導(dǎo)航條加載動(dòng)畫(huà)
????????wx.hideNavigationBarLoading();
????????//停止下拉刷新
????????wx.stopPullDownRefresh();
????????console.log(url)
????????wx.showToast({
??????????title:?"請(qǐng)求失敗,請(qǐng)檢查網(wǎng)絡(luò)連接箱靴!",
??????????icon:?'none',
??????????duration:?2000
????????})????????
??????},
????})
??},
})
頁(yè)面上onLoad寫(xiě)法
onLoad:?function?(options)?{??
????if(app.globalData.openId){??????
??????this.setData({page:1});
??????this.getMeetList();???
????}else{
??????app.loginReadyCallback=res=>{
????????this.setData({page:1});
????????this.getMeetList();???
??????}
????}????
??},