feat: 登录注册流程重构 - 登录页+完善信息页+401跳转
- 新增 profile/login.vue 登录页(UnauthLoginPanel) - 新增 profile/setup.vue 完善信息页(头像/昵称/手机号) - profile/index 未登录态显示头像占位+点击登录+菜单列表 - 登录后始终跳转setup页,completeLogin后才持久化token - auth.js 登录后用pendingToken(内存),不检查profile_completed - request.js 401时reLaunch到登录页,不显示toast,防重复跳转 - 约课页移除UnauthLoginPanel,未登录直接显示列表 - session.js 新增needsSetup状态和completeLogin方法
This commit is contained in:
@@ -2,6 +2,8 @@ const KEY_TOKEN = 'COMPREHENSIVE_VENUE_TOKEN'
|
||||
|
||||
const API_BASE_URL = import.meta.env.VITE_COMPREHENSIVE_API_BASE_URL || 'http://127.0.0.1:8001'
|
||||
|
||||
let pendingToken = null
|
||||
|
||||
const normalizeError = (res, fallbackMessage = '请求服务失败') => {
|
||||
const body = res?.data || res || {}
|
||||
return {
|
||||
@@ -19,16 +21,41 @@ export const authStorage = {
|
||||
getToken() {
|
||||
return uni.getStorageSync(KEY_TOKEN)
|
||||
},
|
||||
setPendingToken(token) {
|
||||
pendingToken = token
|
||||
},
|
||||
getPendingToken() {
|
||||
return pendingToken
|
||||
},
|
||||
clearPendingToken() {
|
||||
pendingToken = null
|
||||
},
|
||||
clear() {
|
||||
uni.removeStorageSync(KEY_TOKEN)
|
||||
pendingToken = null
|
||||
},
|
||||
isLoggedIn() {
|
||||
return !!uni.getStorageSync(KEY_TOKEN)
|
||||
}
|
||||
}
|
||||
|
||||
let isRedirecting = false
|
||||
|
||||
function redirectToLogin() {
|
||||
if (isRedirecting) return
|
||||
const pages = getCurrentPages()
|
||||
const current = pages[pages.length - 1]
|
||||
const currentRoute = current?.route || ''
|
||||
if (currentRoute === 'pages/comprehensive/profile/login' || currentRoute === 'pages/comprehensive/profile/setup') {
|
||||
return
|
||||
}
|
||||
isRedirecting = true
|
||||
authStorage.clear()
|
||||
uni.reLaunch({ url: '/pages/comprehensive/profile/login', complete: () => { isRedirecting = false } })
|
||||
}
|
||||
|
||||
export function request(options = {}) {
|
||||
const token = authStorage.getToken()
|
||||
const token = authStorage.getToken() || authStorage.getPendingToken()
|
||||
const header = {
|
||||
'Content-Type': 'application/json',
|
||||
...(options.header || {})
|
||||
@@ -48,7 +75,9 @@ export function request(options = {}) {
|
||||
const body = res.data
|
||||
|
||||
if (res.statusCode === 401 || body?.code === 40001) {
|
||||
authStorage.clear()
|
||||
redirectToLogin()
|
||||
reject(normalizeError(res, ''))
|
||||
return
|
||||
}
|
||||
|
||||
if (res.statusCode >= 200 && res.statusCode < 300 && body?.code === 0) {
|
||||
@@ -76,7 +105,7 @@ export function request(options = {}) {
|
||||
}
|
||||
|
||||
export function uploadFile(options = {}) {
|
||||
const token = authStorage.getToken()
|
||||
const token = authStorage.getToken() || authStorage.getPendingToken()
|
||||
const header = {
|
||||
...(options.header || {})
|
||||
}
|
||||
@@ -101,7 +130,9 @@ export function uploadFile(options = {}) {
|
||||
}
|
||||
|
||||
if (res.statusCode === 401 || body?.code === 40001) {
|
||||
authStorage.clear()
|
||||
redirectToLogin()
|
||||
reject(normalizeError({ ...res, data: body }, ''))
|
||||
return
|
||||
}
|
||||
|
||||
if (res.statusCode >= 200 && res.statusCode < 300 && body?.code === 0) {
|
||||
|
||||
Reference in New Issue
Block a user