XiaoDaiGua-Ray 403fdd589a feat!: rebuild Ray Template as a monorepo
Share workspace packages between Ray Admin and the interactive docs.

BREAKING CHANGE: v6 is a new major line. v5 stays on legacy/v5.
2026-11-04 14:01:28 +08:00

43 lines
1.3 KiB
TypeScript

import { APP_CATCH_KEY, WHITE_ROUTES } from '@/app-config'
import { useAppRoot } from '@/hooks'
import { getStorage, setStorage } from '@/utils/cache'
import type { Router } from 'vue-router'
import { resolveLandingPath } from './resolve-landing-path'
import { validRole } from './valid-role'
/**
*
* @param router 应用的 Router 实例
*
* @description
* 注册 token、白名单与 meta.role 权限守卫,重定向返回值交给 Router 处理。
* 前端守卫不能替代后端鉴权。
*/
export const permissionRouter = (router: Router) => {
router.beforeEach((to) => {
const token = getStorage<string>(APP_CATCH_KEY.token, 'localStorage')
const isLogin = to.path === '/'
if (token === null) {
return isLogin ? true : { path: '/', replace: true }
}
const isWhiteRoute =
typeof to.name === 'string' && WHITE_ROUTES.includes(to.name)
if (isLogin || (!isWhiteRoute && !validRole(to.meta))) {
const { getRootPath } = useAppRoot()
const rootPath = resolveLandingPath(router, getRootPath.value)
// 保留既有重定向时的缓存行为;若首页本身无权限,取消导航以免循环。
setStorage(APP_CATCH_KEY.appMenuKey, rootPath, 'localStorage')
return to.fullPath === rootPath
? false
: { path: rootPath, replace: true }
}
return true
})
}