This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user