feat: route permission (#3379)

This commit is contained in:
shaohuzhang1 2025-06-24 21:46:02 +08:00 committed by GitHub
parent be6dd24b4b
commit bba7a60789
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 26 additions and 13 deletions

View File

@ -47,6 +47,13 @@ router.beforeEach(
if (to.meta.permission ? hasPermission(to.meta.permission as any, 'OR') : true) {
next()
} else {
console.log('s')
if (to.meta.get_permission_route) {
const t = to.meta.get_permission_route()
console.log(t)
next(t)
return
}
// 如果没有权限则直接取404页面
next({ path: '/no-permission' })
}

View File

@ -2,25 +2,31 @@ import type { RouteRecordRaw } from 'vue-router'
const modules: any = import.meta.glob('./modules/*.ts', { eager: true })
import { hasPermission, set_next_route } from '@/utils/permission/index'
const rolesRoutes: RouteRecordRaw[] = [...Object.keys(modules).map((key) => modules[key].default)]
const get_workspace_permission_route = () => {
const route = rolesRoutes.find((route: any) => {
return (
route.meta?.menu &&
(route.meta.permission ? hasPermission(route.meta.permission as any, 'OR') : true)
)
})
if (route?.name) {
return { name: route?.name }
}
return { name: 'noPermission' }
}
export const routes: Array<RouteRecordRaw> = [
{
path: '/',
name: 'home',
redirect: (to: any) => {
const route = rolesRoutes.find((route: any) => {
return (
route.meta?.menu &&
(route.meta.permission ? hasPermission(route.meta.permission as any, 'OR') : true)
)
})
if (route?.name) {
return { name: route?.name }
}
return { name: 'noPermission' }
},
redirect: '/application',
children: [
...rolesRoutes,
...rolesRoutes.map((r) => {
if (r.meta) {
r.meta.get_permission_route = get_workspace_permission_route
}
return r
}),
{
path: '/no-permission',
name: 'noPermission',