前端:完善 naive-ui 配置,修复 API 返回值访问方式,修复 Sidebar 硬编码数据,修复路由守卫逻辑,修复其他组件问题。

This commit is contained in:
2026-03-31 22:23:03 +08:00
parent d10c823e23
commit 023380d14b
11 changed files with 82 additions and 50 deletions

View File

@@ -83,17 +83,37 @@ const router = createRouter({
})
// 路由守卫
router.beforeEach((to, _from, next) => {
router.beforeEach(async (to, _from, next) => {
const userStore = useUserStore()
const token = localStorage.getItem('access_token')
// 需要登录但未登录
if (to.meta.requiresAuth && !token) {
next({ name: 'Login' })
} else if (to.meta.requiresAdmin && userStore.user?.is_active !== true) {
next({ name: 'Home' })
} else {
next()
return
}
// 需要管理员权限但不是管理员
if (to.meta.requiresAdmin) {
// 如果还没有用户信息,先获取
if (!userStore.user && token) {
try {
await userStore.fetchUser()
} catch {
// 获取失败,清除 token
userStore.logout()
next({ name: 'Login' })
return
}
}
if (!userStore.user?.is_superuser) {
next({ name: 'Home' })
return
}
}
next()
})
export default router