潘的第一次 commit

This commit is contained in:
panchengyong
2026-02-27 23:50:25 +08:00
commit 10b6d0099a
117 changed files with 32547 additions and 0 deletions

View File

@@ -0,0 +1,70 @@
import router from './router'
import { ElMessage } from 'element-plus'
import { getToken } from '@/utils/auth'
import { useUserStore } from '@/stores/user'
// 白名单路由(不需要登录即可访问)
const whiteList = ['/login', '/register']
// DEV模式后端未运行时跳过认证允许直接访问页面
const DEV_SKIP_AUTH = import.meta.env.DEV && !import.meta.env.VITE_REQUIRE_AUTH
// 路由守卫
router.beforeEach(async (to, from, next) => {
// 设置页面标题
document.title = to.meta.title ? `${to.meta.title} - ERP系统` : 'ERP系统'
// DEV模式跳过认证设置模拟角色以通过权限检查
if (DEV_SKIP_AUTH) {
const userStore = useUserStore()
if (userStore.state.userInfo.roles.length === 0) {
userStore.state.userInfo.roles = ['admin']
userStore.state.userInfo.permissions = ['*:*:*']
userStore.state.userInfo.userName = 'dev'
userStore.state.userInfo.nickName = '开发模式'
}
next()
return
}
const token = getToken()
const userStore = useUserStore()
if (token) {
// 已登录
if (to.path === '/login') {
// 已登录状态下访问登录页,重定向到首页
next({ path: '/' })
} else {
// 检查是否已获取用户信息
if (userStore.state.userInfo.roles.length === 0) {
try {
// 获取用户信息
await userStore.getUserInfo()
// 确保路由完成后再跳转
next({ ...to, replace: true })
} catch (error: any) {
// 获取用户信息失败,可能是 token 过期
await userStore.fedLogout()
ElMessage.error(error?.message || '获取用户信息失败,请重新登录')
next(`/login?redirect=${to.fullPath}`)
}
} else {
next()
}
}
} else {
// 未登录
if (whiteList.includes(to.path)) {
// 在白名单中,直接进入
next()
} else {
// 不在白名单中,重定向到登录页
next(`/login?redirect=${to.fullPath}`)
}
}
})
router.afterEach(() => {
// 可以在这里添加 NProgress 完成等逻辑
})