This commit is contained in:
2026-09-13 02:28:46 +08:00
parent df5492a48a
commit 42f9530a59
27 changed files with 979 additions and 149 deletions
+32 -16
View File
@@ -1,5 +1,9 @@
import { request } from '../../utils/request'
import * as auth from '../../utils/auth'
import { logger } from '../../utils/logger'
// 日志模块名
const LOG = 'login'
import { STORAGE_KEYS, QuotaType } from '../../utils/config'
import { LoginResult, UserInfo, QuotaItem } from '../../types/models'
import { formatDate } from '../../utils/format'
@@ -20,7 +24,7 @@ Page({
},
onLoad(query: Record<string, string>) {
console.log('[login] onLoad, query=', JSON.stringify(query), 'isLoggedIn=', auth.isLoggedIn())
logger.debug(LOG, 'onLoad, query=', JSON.stringify(query), 'isLoggedIn=', auth.isLoggedIn())
if (query.redirect) this.setData({ redirect: query.redirect })
if (auth.isLoggedIn()) {
this.goAfterLogin()
@@ -30,12 +34,12 @@ Page({
// 登录完成后的落地页:带 redirect 时回跳来源页(分享详情等),否则进衣橱
goAfterLogin(fallback = false) {
const redirect = this.data.redirect
console.log('[login] goAfterLogin, redirect=', redirect, 'fallback=', fallback)
logger.debug(LOG, 'goAfterLogin, redirect=', redirect, 'fallback=', fallback)
if (redirect && !fallback) {
wx.redirectTo({
url: redirect,
fail: (e) => {
console.log('[login] redirect 失败,回退衣橱, err=', e && e.errMsg)
logger.warn(LOG, 'redirect 回跳失败,回退衣橱, err=', e && e.errMsg)
this.goAfterLogin(true)
},
})
@@ -43,16 +47,16 @@ Page({
}
wx.switchTab({
url: '/pages/wardrobe/wardrobe',
fail: (e) => console.log('[login] switchTab 衣橱失败, err=', e && e.errMsg),
fail: (e) => logger.warn(LOG, 'switchTab 衣橱失败, err=', e && e.errMsg),
})
},
onLogin() {
if (this.data.loading) {
console.log('[login] 登录中,忽略重复点击')
logger.debug(LOG, '登录中,忽略重复点击')
return
}
console.log('[login] 点击登录, redirect=', this.data.redirect)
logger.debug(LOG, '点击登录, redirect=', this.data.redirect)
this.setData({ loading: true })
// 超时兜底:wx.login / wx.request 异常挂起时复位按钮,避免永久 loading 点不动
@@ -60,7 +64,7 @@ Page({
;(this as any).loginTimer = setTimeout(() => {
;(this as any).loginTimer = null
if (!this.data.loading) return
console.log('[login] 登录超时(30s),复位按钮')
logger.warn(LOG, '登录超时(30s),复位按钮')
this.setData({ loading: false })
wx.showToast({ title: '登录超时,请重试', icon: 'none' })
}, 30000)
@@ -68,12 +72,12 @@ Page({
wx.login({
success: (res) => {
if (!res.code) {
console.log('[login] wx.login 返回空 code')
logger.error(LOG, 'wx.login 返回空 code')
this.finishLogin()
wx.showToast({ title: '登录失败,请重试', icon: 'none' })
return
}
console.log('[login] wx.login 成功, codeLen=', res.code.length)
logger.debug(LOG, 'wx.login 成功, codeLen=', res.code.length)
request<LoginResult>({
url: '/user/wxlogin',
method: 'POST',
@@ -81,8 +85,9 @@ Page({
auth: false,
})
.then((data) => {
console.log(
'[login] wxlogin 成功, isNew=',
logger.info(
LOG,
'wxlogin 成功, isNew=',
data.user_info && data.user_info.is_new_user,
'expireTime=',
data.expire_time,
@@ -105,7 +110,7 @@ Page({
}
})
.catch((e) => {
console.log('[login] wxlogin 失败, code=', e && e.code, 'msg=', e && e.message)
logger.error(LOG, 'wxlogin 失败, code=', e && e.code, 'msg=', e && e.message)
})
.then(() => {
// 无论成功还是失败都复位:分支走向礼包/生日引导时若不复位,登录按钮会永久点不动
@@ -113,7 +118,7 @@ Page({
})
},
fail: (e) => {
console.log('[login] wx.login 失败, errMsg=', e && e.errMsg)
logger.error(LOG, 'wx.login 失败, errMsg=', e && e.errMsg)
this.finishLogin()
wx.showToast({ title: '登录失败,请重试', icon: 'none' })
},
@@ -142,7 +147,11 @@ Page({
// 额度只用于礼包展示:失败静默降级,绝不能触发登出/清 token
request<{ quotas: QuotaItem[] }>({ url: '/quota/info', showError: false, silentAuth: true })
.then((res) => this.setData({ giftItems: this.buildGift(userInfo, res.quotas), giftVisible: true }))
.catch(() => this.setData({ giftItems: this.buildGift(userInfo, []), giftVisible: true }))
.catch((e) => {
// 额度拉取失败不阻塞礼包展示,降级为空额度;绝不能触发登出/清 token
logger.warn(LOG, '礼包额度拉取失败,降级展示, code=', e && e.code)
this.setData({ giftItems: this.buildGift(userInfo, []), giftVisible: true })
})
},
buildGift(userInfo: UserInfo, quotas: QuotaItem[]): GiftItem[] {
@@ -177,9 +186,16 @@ Page({
confirmText: '去完善',
cancelText: '稍后再说',
success: (res) => {
if (res.confirm) wx.switchTab({ url: '/pages/profile/profile' })
else this.goAfterLogin()
if (res.confirm) {
wx.switchTab({
url: '/pages/profile/profile',
fail: (e) => logger.warn(LOG, 'switchTab 个人资料失败, err=', e && e.errMsg),
})
} else {
this.goAfterLogin()
}
},
fail: (e) => logger.warn(LOG, '生日引导弹窗失败, err=', e && e.errMsg),
})
},