第一,進(jìn)入小程序全局先進(jìn)行獲取微信code迂卢,用code作為參數(shù)請求接口得到用戶登錄信息某弦,同時(shí)判斷是否為新用戶。
第二而克,請求接口靶壮,發(fā)生登錄失效(token失效)時(shí),自動(dòng)登錄后员萍,再重新請求腾降。
第三,請求接口充活,當(dāng)返回未登錄(授權(quán))提示時(shí)蜂莉,做兼容。(比如跳到授權(quán)頁混卵,這個(gè)具體看接口的業(yè)務(wù)邏輯)
在util.js中封裝一些登錄映穗、請求方法。
1.調(diào)用微信登錄幕随,獲取微信code
function login() {
return new Promise(function (resolve, reject) {
wx.login({
success: function (res) {
if (res.code) {
resolve(res);
} else {
reject(res);
}
},
fail: function (err) {
reject(err);
}
});
});
}
2.通過微信code登錄蚁滋,獲取用戶信息(token,userInfo)
/**
* 調(diào)用微信登錄赘淮,獲取微信code辕录,作為參數(shù),來請求后端登錄接口數(shù)據(jù)(接口返回token和用戶的登錄信息)
* 1.返回code=203說明登錄失斏倚丁:用戶為空走诞。說明是新用戶,此時(shí)需要微信授權(quán)注冊(才能獲取用戶信息)蛤高,跳轉(zhuǎn)微信授權(quán)注冊頁面蚣旱,用戶點(diǎn)擊確定登錄授權(quán)完后,再resolve
* 2.code=200說明微信登錄成功戴陡,將返回的token和用戶信息存儲在緩存中塞绿,再resolve
**/
function wxLogin() {
return new Promise(function(resolve, reject){
login().then(res => {
console.log('微信code',res.code)
requestData({
url: api.LoginBycode,
data: {
code: res.code
}
}).then(wxLoginRes => {
console.log('根據(jù)微信code登錄授權(quán),返回的用戶信息',wxLoginRes)
if (wxLoginRes.data.code == 203){ //登錄失敗恤批,code為空异吻。需要微信授權(quán)注冊
wx.navigateTo({ //跳轉(zhuǎn)授權(quán)頁
url: '/pages/ucenter/index/index'
})
resolve(wxLoginRes)
return
}
//登錄成功,將用戶信息存儲到緩存中
wxLoginRes.data.user.userId = wxLoginRes.data.userId
wx.setStorageSync('userInfo', wxLoginRes.data.user)
wx.setStorageSync('token', wxLoginRes.data.token)
resolve(wxLoginRes)
}).catch(err => {
reject(err)
})
}).catch(err => {
reject(err)
})
})
}
3.請求數(shù)據(jù)喜庞,這里只封裝請求數(shù)據(jù)的參數(shù)與方法诀浪,不處理各種code
//請求成功resolve棋返,失敗則reject。其他邏輯(code的不同情況)在resolve出去的地方處理笋妥。
const requestData = obj => {
obj.method = obj.method || 'POST'
obj.token = obj.token || wx.getStorageSync('token')
obj.data = obj.data || {}
obj.header = {
'Content-Type': obj.header || 'application/json',
token: obj.token,
farmAppID: api.APP_ID
}
return new Promise((resolve, reject) => {
wx.request({
...obj,
success: res => {
if (res.statusCode == 502) {
common.sM('服務(wù)器沒有響應(yīng)懊昨,請稍后再試窄潭!', false)
return
}
resolve(res)
},
fail(err) {
common.sM(err.errMsg || '請求錯(cuò)誤春宣,請檢查網(wǎng)絡(luò)環(huán)境!', false)
common.hL()
reject(err)
}
})
})
}
4.封裝request請求方法,這里專門處理返回不同code的情況
各個(gè)頁面中的接口數(shù)據(jù)請求嫉你,都是調(diào)用這個(gè)方法月帝。
/**
* 如code=200為成功登陸;code=401為token失效幽污;code=203登陸失敗嚷辅,用戶為空(即為新用戶)
* 1.當(dāng)成功登陸時(shí),直接resolve出去
* 2.當(dāng)token失效時(shí)距误,需要重新登陸簸搞,將返回的新的token替換舊的token。再請求數(shù)據(jù)准潭,請求成功resolve出去趁俊,否則reject
* 3.當(dāng)?shù)卿浭∮脩魹榭諘r(shí),需要微信授權(quán)注冊(跳轉(zhuǎn)微信授權(quán)頁)
**/
const request = (obj) => {
return new Promise(function(resolve, reject){
requestData(obj).then(res => {
console.log('請求接口刑然,參數(shù)寺擂,返回?cái)?shù)據(jù)', obj.url, obj.data, res)
if (res.statusCode == 200 && res.data) {
if (res.data.code == 200) {
resolve(res.data)
} else if (res.data.code == 401) { //當(dāng)token失效時(shí),需要重新登陸
wxLogin().then(loginRes => {
obj.token = loginRes.data.token
requestData(obj).then(newRes => {
console.log('newRes', newRes)
if (newRes.data.code == 200) {
resolve(newRes.data)
} else {
console.log('接口返回的code不為200')
reject(newRes.error)
}
})
})
} else if (res.data.code == 203) { //登錄失敗,用戶為空(新用戶)泼掠,需要微信授權(quán)注冊
wx.navigateTo({ //跳到授權(quán)頁
url: '/pages/ucenter/index/index'
})
} else {
console.log('接口返回的code不為200')
reject(res.error)
}
} else if (res.statusCode == 502) {
common.sM('服務(wù)器沒有響應(yīng)怔软,請稍后再次', false)
return
} else {
reject(res.error)
}
}).catch(err => {
console.log(err)
})
})
}
在app.js的onLaunch方法中,判斷token和userInfo是否存在择镇,存在就不是新用戶挡逼,否則根據(jù)微信返回的code,去請求后端接口腻豌,查看返回的信息家坎,是否為新用戶。
var userInfo = wx.getStorageSync('userInfo')
var token = wx.getStorageSync('token')
if (userInfo && token) { //如果userInfo饲梭,token存在,就不是新用戶
this.globalData.newUser = false
}else{ //否則乘盖,根據(jù)微信返回的code去請求后端接口,查看返回信息是否為新用戶
util.wxLogin().then(res => {
console.log('查看返回信息是否為新用戶',res)
this.globalData.newUser = res.data.code == 203 ? true : false
}).catch(err => {
console.log(err)
})
}