Files
Snap.Server.Web/src/router/permission.ts
2026-02-05 21:52:41 +08:00

39 lines
856 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import router from './index'
import { useUserStore } from '@/stores/user'
router.beforeEach(async (to, _ , next) => {
const userStore = useUserStore()
// 未登录
if (!userStore.token) {
// 主页(/)、登录页和下载页允许未登录访问
if (to.path === '/' || to.path === '/login' || to.path === '/download') {
next()
} else {
next('/login')
}
return
}
// 已登录还去 login
if (to.path === '/login') {
next('/')
return
}
// 如果没有用户信息,尝试获取
if (!userStore.userInfo) {
try {
await userStore.fetchUserInfo()
} catch (error) {
// 获取用户信息失败可能token已过期跳转到登录页
console.error('获取用户信息失败:', error)
userStore.logout()
next('/login')
return
}
}
next()
})