feat: permission route (#3381)

This commit is contained in:
shaohuzhang1 2025-06-25 13:52:38 +08:00 committed by GitHub
parent 46fb1b1b4e
commit c990ad7215
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 97 additions and 51 deletions

78
ui/src/router/common.ts Normal file
View File

@ -0,0 +1,78 @@
import {
createRouter,
createWebHistory,
type NavigationGuardNext,
type RouteLocationNormalized,
type RouteRecordRaw,
type RouteRecordName,
} from 'vue-router'
import { hasPermission, set_next_route } from '@/utils/permission/index'
export const getChildRouteList: (
routeList: Array<RouteRecordRaw>,
path: string,
name?: RouteRecordName | null | undefined,
) => Array<RouteRecordRaw> = (routeList, path, name) => {
for (let index = 0; index < routeList.length; index++) {
const route = routeList[index]
if (name === route.name && path === route.path) {
return route.children || []
}
if (route.children && route.children.length > 0) {
const result = getChildRouteList(route.children, path, name)
if (result && result?.length > 0) {
return result
}
}
}
return []
}
/**
*
* @param routeList
* @param name
* @returns
*/
export const getSameRouteList: (
routeList: Array<RouteRecordRaw>,
name?: RouteRecordName | null | undefined,
) => Array<RouteRecordRaw> = (routeList, name) => {
for (let index = 0; index < routeList.length; index++) {
const route = routeList[index]
if (name === route.name) {
return routeList
}
if (route.children && route.children.length > 0) {
const result = getSameRouteList(route.children, name)
if (result && result?.length > 0) {
return result
}
}
}
return []
}
/**
*
* @param routes
* @param to
* @returns
*/
export const getPermissionRoute = (routes: Array<RouteRecordRaw>, to: RouteLocationNormalized) => {
const routeName: string = to.meta
? to.meta.permissionRoute
? (to.meta.permissionRoute as string)
: (to.name as string)
: (to.name as string)
const routeList = getSameRouteList(routes, routeName)
const route = routeList.find((route: any) => {
return (
(to.meta.group ? to.meta.group == route.meta.group : true) &&
(route.meta.permission ? hasPermission(route.meta.permission as any, 'OR') : true)
)
})
if (route?.name) {
return { name: route?.name, params: to.params }
}
return { name: 'noPermission' }
}

View File

@ -1,5 +1,7 @@
import { hasPermission, set_next_route } from '@/utils/permission/index'
import { getChildRouteList } from '@/router/common'
import NProgress from 'nprogress'
import { getPermissionRoute } from '@/router/common'
import {
createRouter,
createWebHistory,
@ -47,44 +49,16 @@ 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' })
const n = getPermissionRoute(routes, to)
next(n)
}
},
)
router.afterEach(() => {
NProgress.done()
})
export const getChildRouteListByPathAndName = (path: any, name?: RouteRecordName | any) => {
return getChildRouteList(routes, path, name)
}
export const getChildRouteList: (
routeList: Array<RouteRecordRaw>,
path: string,
name?: RouteRecordName | null | undefined,
) => Array<RouteRecordRaw> = (routeList, path, name) => {
for (let index = 0; index < routeList.length; index++) {
const route = routeList[index]
if (name === route.name && path === route.path) {
return route.children || []
}
if (route.children && route.children.length > 0) {
const result = getChildRouteList(route.children, path, name)
if (result && result?.length > 0) {
return result
}
}
}
return []
}
export default router

View File

@ -1,5 +1,4 @@
import { PermissionConst, EditionConst, RoleConst } from '@/utils/permission/data'
import { get_next_route } from '@/utils/permission'
const applicationRouter = {
path: '/application',
name: 'application',
@ -12,6 +11,7 @@ const applicationRouter = {
PermissionConst.APPLICATION_READ.getWorkspacePermissionWorkspaceManageRole,
PermissionConst.APPLICATION_READ.getWorkspacePermission,
],
group: 'workspace',
order: 1,
},
redirect: '/application',
@ -20,7 +20,7 @@ const applicationRouter = {
{
path: '/application',
name: 'application-index',
meta: { title: '应用主页', activeMenu: '/application' },
meta: { title: '应用主页', activeMenu: '/application', sameRoute: 'application' },
component: () => import('@/views/application/index.vue'),
hidden: true,
},

View File

@ -19,6 +19,7 @@ const DocumentRouter = {
active: 'document',
parentPath: '/knowledge/:id/:folderId',
parentName: 'KnowledgeDetail',
group: 'KnowledgeDetail',
permission: [
RoleConst.ADMIN,
RoleConst.WORKSPACE_MANAGE.getWorkspaceRole,
@ -43,6 +44,7 @@ const DocumentRouter = {
active: 'problem',
parentPath: '/knowledge/:id/:folderId',
parentName: 'KnowledgeDetail',
group: 'KnowledgeDetail',
permission: [
RoleConst.ADMIN,
RoleConst.WORKSPACE_MANAGE.getWorkspaceRole,
@ -66,6 +68,7 @@ const DocumentRouter = {
active: 'hit-test',
parentPath: '/knowledge/:id/:folderId',
parentName: 'KnowledgeDetail',
group: 'KnowledgeDetail',
},
component: () => import('@/views/hit-test/index.vue'),
},
@ -80,6 +83,7 @@ const DocumentRouter = {
parentPath: '/knowledge/:id/:folderId',
parentName: 'KnowledgeDetail',
resourceType: ChatUserResourceEnum.KNOWLEDGE,
group: 'KnowledgeDetail',
permission: [
RoleConst.ADMIN,
RoleConst.WORKSPACE_MANAGE.getWorkspaceRole,
@ -104,6 +108,7 @@ const DocumentRouter = {
active: 'setting',
parentPath: '/knowledge/:id/:folderId',
parentName: 'KnowledgeDetail',
group: 'KnowledgeDetail',
permission: [
RoleConst.ADMIN,
RoleConst.WORKSPACE_MANAGE.getWorkspaceRole,

View File

@ -11,6 +11,7 @@ const ModelRouter = {
PermissionConst.KNOWLEDGE_READ.getWorkspacePermission,
PermissionConst.KNOWLEDGE_READ.getWorkspacePermissionWorkspaceManageRole,
],
group: 'workspace',
order: 2,
},
redirect: '/knowledge',
@ -19,7 +20,7 @@ const ModelRouter = {
{
path: '/knowledge',
name: 'knowledge-index',
meta: { title: '知识库主页', activeMenu: '/knowledge' },
meta: { title: '知识库主页', activeMenu: '/knowledge', sameRoute: 'knowledge' },
component: () => import('@/views/knowledge/index.vue'),
},

View File

@ -11,6 +11,7 @@ const ModelRouter = {
PermissionConst.MODEL_READ.getWorkspacePermission,
PermissionConst.MODEL_READ.getWorkspacePermissionWorkspaceManageRole,
],
group: 'workspace',
order: 4,
},
redirect: '/model',
@ -22,6 +23,7 @@ const ModelRouter = {
meta: {
title: '模型主页',
activeMenu: '/model',
sameRoute: 'model',
},
component: () => import('@/views/model/index.vue'),
},

View File

@ -11,6 +11,7 @@ const ModelRouter = {
PermissionConst.TOOL_READ.getWorkspacePermission,
PermissionConst.TOOL_READ.getWorkspacePermissionWorkspaceManageRole,
],
group: 'workspace',
order: 3,
},
redirect: '/tool',
@ -20,6 +21,7 @@ const ModelRouter = {
path: '/tool',
name: 'tool-index',
meta: { title: '工具主页', activeMenu: '/tool' },
sameRoute: 'tool',
component: () => import('@/views/tool/index.vue'),
},
],

View File

@ -1,19 +1,8 @@
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> = [
{
@ -21,12 +10,7 @@ export const routes: Array<RouteRecordRaw> = [
name: 'home',
redirect: '/application',
children: [
...rolesRoutes.map((r) => {
if (r.meta) {
r.meta.get_permission_route = get_workspace_permission_route
}
return r
}),
...rolesRoutes,
{
path: '/no-permission',
name: 'noPermission',