refactor: 重构登录逻辑使用 async/await
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
// app.js
|
// app.js
|
||||||
const { BASE_URL, API } = require('./utils/config')
|
const { BASE_URL, API } = require('./utils/config')
|
||||||
|
const { request } = require('./utils/api')
|
||||||
|
|
||||||
App({
|
App({
|
||||||
onLaunch() {
|
onLaunch() {
|
||||||
@@ -24,18 +25,17 @@ App({
|
|||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
|
||||||
loginWithCode(code) {
|
async loginWithCode(code) {
|
||||||
wx.request({
|
try {
|
||||||
|
const data = await request({
|
||||||
url: BASE_URL + API.LOGIN,
|
url: BASE_URL + API.LOGIN,
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
data: { code },
|
data: { code }
|
||||||
success: (res) => {
|
})
|
||||||
const result = res.data
|
|
||||||
if (result && result.code === 0 && result.data) {
|
|
||||||
const userInfo = {
|
const userInfo = {
|
||||||
openid: result.data.openid,
|
openid: data.openid,
|
||||||
sessionKey: result.data.session_key,
|
sessionKey: data.session_key,
|
||||||
unionid: result.data.unionid || ''
|
unionid: data.unionid || ''
|
||||||
}
|
}
|
||||||
this.globalData.userInfo = userInfo
|
this.globalData.userInfo = userInfo
|
||||||
this.globalData.isLoggedIn = true
|
this.globalData.isLoggedIn = true
|
||||||
@@ -43,17 +43,11 @@ App({
|
|||||||
if (this.loginReadyCallback) {
|
if (this.loginReadyCallback) {
|
||||||
this.loginReadyCallback(userInfo)
|
this.loginReadyCallback(userInfo)
|
||||||
}
|
}
|
||||||
} else {
|
} catch (err) {
|
||||||
console.error('后端登录失败', result)
|
console.error('后端登录失败', err)
|
||||||
this.handleLoginFail()
|
this.handleLoginFail()
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
fail: (err) => {
|
|
||||||
console.error('请求后端登录接口失败', err)
|
|
||||||
this.handleLoginFail()
|
|
||||||
}
|
|
||||||
})
|
|
||||||
},
|
|
||||||
|
|
||||||
handleLoginFail() {
|
handleLoginFail() {
|
||||||
this.globalData.isLoggedIn = false
|
this.globalData.isLoggedIn = false
|
||||||
|
|||||||
Reference in New Issue
Block a user