diff --git a/frontend/src/router/index.ts b/frontend/src/router/index.ts index 8c0e2d0..b399792 100644 --- a/frontend/src/router/index.ts +++ b/frontend/src/router/index.ts @@ -47,9 +47,14 @@ const router = createRouter({ ], }) -router.beforeEach((to, from, next) => { +router.beforeEach(async (to, from, next) => { const authStore = useAuthStore() + // 已登录但用户信息未加载(如刷新页面后),先获取用户信息 + if (authStore.isLoggedIn && !authStore.user) { + await authStore.fetchUser() + } + if (to.meta.requiresAuth && !authStore.isLoggedIn) { next({ name: 'login' }) } else if (to.meta.adminOnly && authStore.user?.role !== 'admin') {