提交
提交
This commit is contained in:
@@ -9,34 +9,72 @@ App({
|
||||
})
|
||||
}
|
||||
|
||||
// 自动静默登录:调用云函数获取 openid
|
||||
|
||||
|
||||
// 自动静默登录:wx.login 获取 code,再请求后端接口换取 openid
|
||||
this.silentLogin()
|
||||
},
|
||||
|
||||
silentLogin() {
|
||||
wx.cloud.callFunction({
|
||||
name: 'login',
|
||||
data: {},
|
||||
success: (res) => {
|
||||
const openid = res.result.openid
|
||||
const userInfo = res.result.userInfo || {}
|
||||
userInfo.openid = openid
|
||||
|
||||
this.globalData.userInfo = userInfo
|
||||
this.globalData.isLoggedIn = true
|
||||
wx.setStorageSync('userInfo', userInfo)
|
||||
|
||||
// 通知当前页面刷新
|
||||
if (this.loginReadyCallback) {
|
||||
this.loginReadyCallback(userInfo)
|
||||
wx.login({
|
||||
success: (loginRes) => {
|
||||
if (loginRes.code) {
|
||||
this.loginWithCode(loginRes.code)
|
||||
} else {
|
||||
console.error('wx.login 失败', loginRes.errMsg)
|
||||
this.globalData.isLoggedIn = false
|
||||
this.globalData.loginFailed = true
|
||||
if (this.loginReadyCallback) {
|
||||
this.loginReadyCallback(null)
|
||||
}
|
||||
}
|
||||
},
|
||||
fail: (err) => {
|
||||
console.error('静默登录失败', err)
|
||||
console.error('wx.login 调用失败', err)
|
||||
this.globalData.isLoggedIn = false
|
||||
this.globalData.loginFailed = true
|
||||
if (this.loginReadyCallback) {
|
||||
this.loginReadyCallback(null)
|
||||
}
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
// 通知页面登录失败
|
||||
loginWithCode(code) {
|
||||
wx.request({
|
||||
url: 'https://xcx.yun.588580.xyz/api/wx-mini/login',
|
||||
method: 'GET',
|
||||
data: { code },
|
||||
success: (res) => {
|
||||
const result = res.data
|
||||
if (result && result.code === 0 && result.data) {
|
||||
const openid = result.data.openid
|
||||
const userInfo = {
|
||||
openid,
|
||||
sessionKey: result.data.session_key,
|
||||
unionid: result.data.unionid || ''
|
||||
}
|
||||
|
||||
this.globalData.userInfo = userInfo
|
||||
this.globalData.isLoggedIn = true
|
||||
wx.setStorageSync('userInfo', userInfo)
|
||||
|
||||
if (this.loginReadyCallback) {
|
||||
this.loginReadyCallback(userInfo)
|
||||
}
|
||||
} else {
|
||||
console.error('后端登录失败', result)
|
||||
this.globalData.isLoggedIn = false
|
||||
this.globalData.loginFailed = true
|
||||
if (this.loginReadyCallback) {
|
||||
this.loginReadyCallback(null)
|
||||
}
|
||||
}
|
||||
},
|
||||
fail: (err) => {
|
||||
console.error('请求后端登录接口失败', err)
|
||||
this.globalData.isLoggedIn = false
|
||||
this.globalData.loginFailed = true
|
||||
if (this.loginReadyCallback) {
|
||||
this.loginReadyCallback(null)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user