This commit is contained in:
2025-12-04 12:44:54 +08:00
parent d687bf67b1
commit 3f33e5c2f1
8 changed files with 447 additions and 99 deletions

View File

@@ -315,9 +315,18 @@ const router = createRouter({
router.beforeEach(async (to, from, next) => {
const userStore = useUserStore()
// 等待userStore初始化完成
if (!userStore.initialized) {
// 对于不需要认证的路由(如登录页),不等待初始化,直接放行
const isAuthRoute = to.path.startsWith('/auth')
const requiresAuth = to.meta.requiresAuth
// 只有在需要认证的路由上才等待初始化
if (requiresAuth && !userStore.initialized) {
await userStore.init()
} else if (!userStore.initialized) {
// 对于不需要认证的路由,异步初始化但不阻塞
userStore.init().catch(err => {
console.warn('UserStore初始化失败:', err)
})
}
// 设置页面标题
@@ -326,7 +335,7 @@ router.beforeEach(async (to, from, next) => {
}
// 检查是否需要认证
if (to.meta.requiresAuth && !userStore.isLoggedIn) {
if (requiresAuth && !userStore.isLoggedIn) {
next('/auth/login')
return
}
@@ -338,7 +347,7 @@ router.beforeEach(async (to, from, next) => {
}
// 已登录用户访问认证页面,重定向到数据大厅
if (to.path.startsWith('/auth') && userStore.isLoggedIn) {
if (isAuthRoute && userStore.isLoggedIn) {
next('/products')
return
}