fix: 修改路由功能

This commit is contained in:
2026-06-17 12:23:23 +08:00
parent dedf172539
commit 18ff5cacfe
2 changed files with 26 additions and 46 deletions

View File

@@ -39,42 +39,6 @@ export const useAccountStore = defineStore(
/** 是否已完成初始验证(防止重复请求) */
const isInitialized = ref(false)
/**
* 判断当前是否已登录
* token 存在且已验证有效才算已登录
*/
const isLoggedIn = () => isTokenValid.value
/**
* 初始化认证状态
*
* 用持久化的 Access Token 向服务端验证是否仍然有效。
* 只会在首次调用时真正发请求,后续调用直接跳过。
*
* - 无 token → 无操作
* - token 有效 → 拉取用户信息,标记已登录
* - token 过期 → requestClient 拦截器自动调用 refreshAccessToken() 刷新后重试,
* 若刷新也失败则抛出异常,此处 catch 后清除认证状态
*/
const initAuth = async () => {
console.log('initAuth', isInitialized.value)
// 已验证过,跳过
if (isInitialized.value) return
isInitialized.value = true
// 没有持久化的 Access Token
if (!accessToken.value) return
try {
await fetchUserInfo()
// 成功获取用户信息token 有效
isTokenValid.value = true
} catch {
// 请求失败(包括自动刷新也失败),清除认证状态
clearAuth()
}
}
/**
* 用户登录
* 调用登录 API成功后存储双 Token 并获取用户信息
@@ -150,12 +114,12 @@ export const useAccountStore = defineStore(
isInitialized.value = false
}
console.log('useAccountStore', accessToken.value)
return {
accountInfo,
accessToken,
refreshToken,
isLoggedIn,
initAuth,
login,
register,
logout,