优化前端认证与类型管理

This commit is contained in:
yml
2026-05-25 01:59:59 +08:00
parent eafa6f96b2
commit 2cfd3d9422
28 changed files with 435 additions and 280 deletions
+13 -8
View File
@@ -1,6 +1,8 @@
import { createRouter, createWebHistory } from 'vue-router'
import { useAdminSessionStore } from '@/stores/adminSession'
import { useSessionStore } from '@/stores/session'
import { getAccessToken, getLoginPath } from '@/utils/authStorage'
import { accountRoutes } from './accountRoutes'
import { adminRoutes } from './adminRoutes'
import { isMobileHost, toMobilePath } from './mobileHost'
@@ -26,16 +28,17 @@ router.beforeEach(async (to) => {
if (to.meta.requiresAuth) {
const session = useSessionStore()
const hasToken = !!localStorage.getItem('access_token')
session.syncFromStorage()
const hasToken = !!session.token
if (!hasToken) {
const loginPath = to.path.startsWith('/m') ? '/m/login' : '/login'
const loginPath = getLoginPath('user', to.path)
return { path: loginPath, query: { redirect: to.fullPath } }
}
if (!session.phone) {
try {
await session.loadMe()
} catch {
const loginPath = to.path.startsWith('/m') ? '/m/login' : '/login'
const loginPath = getLoginPath('user', to.path)
return { path: loginPath, query: { redirect: to.fullPath } }
}
}
@@ -43,14 +46,15 @@ router.beforeEach(async (to) => {
if (to.meta.requiresRealname) {
const session = useSessionStore()
const loginPath = to.path.startsWith('/m') ? '/m/login' : '/login'
session.syncFromStorage()
const loginPath = getLoginPath('user', to.path)
const realnamePath = to.path.startsWith('/m') ? '/m/realname' : '/realname'
if (session.realnameStatus !== 'verified') {
try {
await session.loadMe()
} catch {
if (!localStorage.getItem('access_token')) {
if (!getAccessToken('user')) {
return { path: loginPath, query: { redirect: to.fullPath } }
}
}
@@ -61,12 +65,13 @@ router.beforeEach(async (to) => {
}
}
if (to.path === '/admin/login' && localStorage.getItem('admin_access_token')) {
const adminSession = useAdminSessionStore()
adminSession.syncFromStorage()
if (to.path === '/admin/login' && adminSession.token) {
return '/admin/dashboard'
}
if (to.meta.requiresAdmin) {
const token = localStorage.getItem('admin_access_token')
if (!token) {
if (!adminSession.token) {
return '/admin/login'
}
}