This commit is contained in:
2026-09-13 02:02:51 +08:00
parent afd0973ce5
commit df5492a48a
4 changed files with 98 additions and 37 deletions
+59 -8
View File
@@ -20,6 +20,7 @@ Page({
},
onLoad(query: Record<string, string>) {
console.log('[login] onLoad, query=', JSON.stringify(query), 'isLoggedIn=', auth.isLoggedIn())
if (query.redirect) this.setData({ redirect: query.redirect })
if (auth.isLoggedIn()) {
this.goAfterLogin()
@@ -29,27 +30,50 @@ Page({
// 登录完成后的落地页:带 redirect 时回跳来源页(分享详情等),否则进衣橱
goAfterLogin(fallback = false) {
const redirect = this.data.redirect
console.log('[login] goAfterLogin, redirect=', redirect, 'fallback=', fallback)
if (redirect && !fallback) {
wx.redirectTo({
url: redirect,
fail: () => this.goAfterLogin(true),
fail: (e) => {
console.log('[login] redirect 失败,回退衣橱, err=', e && e.errMsg)
this.goAfterLogin(true)
},
})
return
}
wx.switchTab({ url: '/pages/wardrobe/wardrobe' })
wx.switchTab({
url: '/pages/wardrobe/wardrobe',
fail: (e) => console.log('[login] switchTab 衣橱失败, err=', e && e.errMsg),
})
},
onLogin() {
if (this.data.loading) return
if (this.data.loading) {
console.log('[login] 登录中,忽略重复点击')
return
}
console.log('[login] 点击登录, redirect=', this.data.redirect)
this.setData({ loading: true })
// 超时兜底:wx.login / wx.request 异常挂起时复位按钮,避免永久 loading 点不动
this.clearLoginTimer()
;(this as any).loginTimer = setTimeout(() => {
;(this as any).loginTimer = null
if (!this.data.loading) return
console.log('[login] 登录超时(30s),复位按钮')
this.setData({ loading: false })
wx.showToast({ title: '登录超时,请重试', icon: 'none' })
}, 30000)
wx.login({
success: (res) => {
if (!res.code) {
this.setData({ loading: false })
console.log('[login] wx.login 返回空 code')
this.finishLogin()
wx.showToast({ title: '登录失败,请重试', icon: 'none' })
return
}
console.log('[login] wx.login 成功, codeLen=', res.code.length)
request<LoginResult>({
url: '/user/wxlogin',
method: 'POST',
@@ -57,6 +81,12 @@ Page({
auth: false,
})
.then((data) => {
console.log(
'[login] wxlogin 成功, isNew=',
data.user_info && data.user_info.is_new_user,
'expireTime=',
data.expire_time,
)
auth.setTokens({
accessToken: data.access_token,
refreshToken: data.refresh_token,
@@ -74,19 +104,40 @@ Page({
this.goAfterLogin()
}
})
.catch(() => {})
.catch((e) => {
console.log('[login] wxlogin 失败, code=', e && e.code, 'msg=', e && e.message)
})
.then(() => {
// 无论成功还是失败都复位:分支走向礼包/生日引导时若不复位,登录按钮会永久点不动
this.setData({ loading: false })
this.finishLogin()
})
},
fail: () => {
this.setData({ loading: false })
fail: (e) => {
console.log('[login] wx.login 失败, errMsg=', e && e.errMsg)
this.finishLogin()
wx.showToast({ title: '登录失败,请重试', icon: 'none' })
},
})
},
// 复位登录态:清超时定时器 + 复位按钮
finishLogin() {
this.clearLoginTimer()
this.setData({ loading: false })
},
clearLoginTimer() {
const timer = (this as any).loginTimer
if (timer) {
clearTimeout(timer)
;(this as any).loginTimer = null
}
},
onUnload() {
this.clearLoginTimer()
},
showWelcomeGift(userInfo: UserInfo) {
// 额度只用于礼包展示:失败静默降级,绝不能触发登出/清 token
request<{ quotas: QuotaItem[] }>({ url: '/quota/info', showError: false, silentAuth: true })