feat: permission route (#3381)
This commit is contained in:
parent
46fb1b1b4e
commit
c990ad7215
78
ui/src/router/common.ts
Normal file
78
ui/src/router/common.ts
Normal 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' }
|
||||||
|
}
|
||||||
@ -1,5 +1,7 @@
|
|||||||
import { hasPermission, set_next_route } from '@/utils/permission/index'
|
import { hasPermission, set_next_route } from '@/utils/permission/index'
|
||||||
|
import { getChildRouteList } from '@/router/common'
|
||||||
import NProgress from 'nprogress'
|
import NProgress from 'nprogress'
|
||||||
|
import { getPermissionRoute } from '@/router/common'
|
||||||
import {
|
import {
|
||||||
createRouter,
|
createRouter,
|
||||||
createWebHistory,
|
createWebHistory,
|
||||||
@ -47,44 +49,16 @@ router.beforeEach(
|
|||||||
if (to.meta.permission ? hasPermission(to.meta.permission as any, 'OR') : true) {
|
if (to.meta.permission ? hasPermission(to.meta.permission as any, 'OR') : true) {
|
||||||
next()
|
next()
|
||||||
} else {
|
} else {
|
||||||
console.log('s')
|
const n = getPermissionRoute(routes, to)
|
||||||
if (to.meta.get_permission_route) {
|
next(n)
|
||||||
const t = to.meta.get_permission_route()
|
|
||||||
console.log(t)
|
|
||||||
next(t)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
// 如果没有权限则直接取404页面
|
|
||||||
next({ path: '/no-permission' })
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
router.afterEach(() => {
|
router.afterEach(() => {
|
||||||
NProgress.done()
|
NProgress.done()
|
||||||
})
|
})
|
||||||
|
|
||||||
export const getChildRouteListByPathAndName = (path: any, name?: RouteRecordName | any) => {
|
export const getChildRouteListByPathAndName = (path: any, name?: RouteRecordName | any) => {
|
||||||
return getChildRouteList(routes, path, name)
|
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
|
export default router
|
||||||
|
|||||||
@ -1,5 +1,4 @@
|
|||||||
import { PermissionConst, EditionConst, RoleConst } from '@/utils/permission/data'
|
import { PermissionConst, EditionConst, RoleConst } from '@/utils/permission/data'
|
||||||
import { get_next_route } from '@/utils/permission'
|
|
||||||
const applicationRouter = {
|
const applicationRouter = {
|
||||||
path: '/application',
|
path: '/application',
|
||||||
name: 'application',
|
name: 'application',
|
||||||
@ -12,6 +11,7 @@ const applicationRouter = {
|
|||||||
PermissionConst.APPLICATION_READ.getWorkspacePermissionWorkspaceManageRole,
|
PermissionConst.APPLICATION_READ.getWorkspacePermissionWorkspaceManageRole,
|
||||||
PermissionConst.APPLICATION_READ.getWorkspacePermission,
|
PermissionConst.APPLICATION_READ.getWorkspacePermission,
|
||||||
],
|
],
|
||||||
|
group: 'workspace',
|
||||||
order: 1,
|
order: 1,
|
||||||
},
|
},
|
||||||
redirect: '/application',
|
redirect: '/application',
|
||||||
@ -20,7 +20,7 @@ const applicationRouter = {
|
|||||||
{
|
{
|
||||||
path: '/application',
|
path: '/application',
|
||||||
name: 'application-index',
|
name: 'application-index',
|
||||||
meta: { title: '应用主页', activeMenu: '/application' },
|
meta: { title: '应用主页', activeMenu: '/application', sameRoute: 'application' },
|
||||||
component: () => import('@/views/application/index.vue'),
|
component: () => import('@/views/application/index.vue'),
|
||||||
hidden: true,
|
hidden: true,
|
||||||
},
|
},
|
||||||
|
|||||||
@ -19,6 +19,7 @@ const DocumentRouter = {
|
|||||||
active: 'document',
|
active: 'document',
|
||||||
parentPath: '/knowledge/:id/:folderId',
|
parentPath: '/knowledge/:id/:folderId',
|
||||||
parentName: 'KnowledgeDetail',
|
parentName: 'KnowledgeDetail',
|
||||||
|
group: 'KnowledgeDetail',
|
||||||
permission: [
|
permission: [
|
||||||
RoleConst.ADMIN,
|
RoleConst.ADMIN,
|
||||||
RoleConst.WORKSPACE_MANAGE.getWorkspaceRole,
|
RoleConst.WORKSPACE_MANAGE.getWorkspaceRole,
|
||||||
@ -43,6 +44,7 @@ const DocumentRouter = {
|
|||||||
active: 'problem',
|
active: 'problem',
|
||||||
parentPath: '/knowledge/:id/:folderId',
|
parentPath: '/knowledge/:id/:folderId',
|
||||||
parentName: 'KnowledgeDetail',
|
parentName: 'KnowledgeDetail',
|
||||||
|
group: 'KnowledgeDetail',
|
||||||
permission: [
|
permission: [
|
||||||
RoleConst.ADMIN,
|
RoleConst.ADMIN,
|
||||||
RoleConst.WORKSPACE_MANAGE.getWorkspaceRole,
|
RoleConst.WORKSPACE_MANAGE.getWorkspaceRole,
|
||||||
@ -66,6 +68,7 @@ const DocumentRouter = {
|
|||||||
active: 'hit-test',
|
active: 'hit-test',
|
||||||
parentPath: '/knowledge/:id/:folderId',
|
parentPath: '/knowledge/:id/:folderId',
|
||||||
parentName: 'KnowledgeDetail',
|
parentName: 'KnowledgeDetail',
|
||||||
|
group: 'KnowledgeDetail',
|
||||||
},
|
},
|
||||||
component: () => import('@/views/hit-test/index.vue'),
|
component: () => import('@/views/hit-test/index.vue'),
|
||||||
},
|
},
|
||||||
@ -80,6 +83,7 @@ const DocumentRouter = {
|
|||||||
parentPath: '/knowledge/:id/:folderId',
|
parentPath: '/knowledge/:id/:folderId',
|
||||||
parentName: 'KnowledgeDetail',
|
parentName: 'KnowledgeDetail',
|
||||||
resourceType: ChatUserResourceEnum.KNOWLEDGE,
|
resourceType: ChatUserResourceEnum.KNOWLEDGE,
|
||||||
|
group: 'KnowledgeDetail',
|
||||||
permission: [
|
permission: [
|
||||||
RoleConst.ADMIN,
|
RoleConst.ADMIN,
|
||||||
RoleConst.WORKSPACE_MANAGE.getWorkspaceRole,
|
RoleConst.WORKSPACE_MANAGE.getWorkspaceRole,
|
||||||
@ -104,6 +108,7 @@ const DocumentRouter = {
|
|||||||
active: 'setting',
|
active: 'setting',
|
||||||
parentPath: '/knowledge/:id/:folderId',
|
parentPath: '/knowledge/:id/:folderId',
|
||||||
parentName: 'KnowledgeDetail',
|
parentName: 'KnowledgeDetail',
|
||||||
|
group: 'KnowledgeDetail',
|
||||||
permission: [
|
permission: [
|
||||||
RoleConst.ADMIN,
|
RoleConst.ADMIN,
|
||||||
RoleConst.WORKSPACE_MANAGE.getWorkspaceRole,
|
RoleConst.WORKSPACE_MANAGE.getWorkspaceRole,
|
||||||
|
|||||||
@ -11,6 +11,7 @@ const ModelRouter = {
|
|||||||
PermissionConst.KNOWLEDGE_READ.getWorkspacePermission,
|
PermissionConst.KNOWLEDGE_READ.getWorkspacePermission,
|
||||||
PermissionConst.KNOWLEDGE_READ.getWorkspacePermissionWorkspaceManageRole,
|
PermissionConst.KNOWLEDGE_READ.getWorkspacePermissionWorkspaceManageRole,
|
||||||
],
|
],
|
||||||
|
group: 'workspace',
|
||||||
order: 2,
|
order: 2,
|
||||||
},
|
},
|
||||||
redirect: '/knowledge',
|
redirect: '/knowledge',
|
||||||
@ -19,7 +20,7 @@ const ModelRouter = {
|
|||||||
{
|
{
|
||||||
path: '/knowledge',
|
path: '/knowledge',
|
||||||
name: 'knowledge-index',
|
name: 'knowledge-index',
|
||||||
meta: { title: '知识库主页', activeMenu: '/knowledge' },
|
meta: { title: '知识库主页', activeMenu: '/knowledge', sameRoute: 'knowledge' },
|
||||||
component: () => import('@/views/knowledge/index.vue'),
|
component: () => import('@/views/knowledge/index.vue'),
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
@ -11,6 +11,7 @@ const ModelRouter = {
|
|||||||
PermissionConst.MODEL_READ.getWorkspacePermission,
|
PermissionConst.MODEL_READ.getWorkspacePermission,
|
||||||
PermissionConst.MODEL_READ.getWorkspacePermissionWorkspaceManageRole,
|
PermissionConst.MODEL_READ.getWorkspacePermissionWorkspaceManageRole,
|
||||||
],
|
],
|
||||||
|
group: 'workspace',
|
||||||
order: 4,
|
order: 4,
|
||||||
},
|
},
|
||||||
redirect: '/model',
|
redirect: '/model',
|
||||||
@ -22,6 +23,7 @@ const ModelRouter = {
|
|||||||
meta: {
|
meta: {
|
||||||
title: '模型主页',
|
title: '模型主页',
|
||||||
activeMenu: '/model',
|
activeMenu: '/model',
|
||||||
|
sameRoute: 'model',
|
||||||
},
|
},
|
||||||
component: () => import('@/views/model/index.vue'),
|
component: () => import('@/views/model/index.vue'),
|
||||||
},
|
},
|
||||||
|
|||||||
@ -11,6 +11,7 @@ const ModelRouter = {
|
|||||||
PermissionConst.TOOL_READ.getWorkspacePermission,
|
PermissionConst.TOOL_READ.getWorkspacePermission,
|
||||||
PermissionConst.TOOL_READ.getWorkspacePermissionWorkspaceManageRole,
|
PermissionConst.TOOL_READ.getWorkspacePermissionWorkspaceManageRole,
|
||||||
],
|
],
|
||||||
|
group: 'workspace',
|
||||||
order: 3,
|
order: 3,
|
||||||
},
|
},
|
||||||
redirect: '/tool',
|
redirect: '/tool',
|
||||||
@ -20,6 +21,7 @@ const ModelRouter = {
|
|||||||
path: '/tool',
|
path: '/tool',
|
||||||
name: 'tool-index',
|
name: 'tool-index',
|
||||||
meta: { title: '工具主页', activeMenu: '/tool' },
|
meta: { title: '工具主页', activeMenu: '/tool' },
|
||||||
|
sameRoute: 'tool',
|
||||||
component: () => import('@/views/tool/index.vue'),
|
component: () => import('@/views/tool/index.vue'),
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
|
|||||||
@ -1,19 +1,8 @@
|
|||||||
import type { RouteRecordRaw } from 'vue-router'
|
import type { RouteRecordRaw } from 'vue-router'
|
||||||
const modules: any = import.meta.glob('./modules/*.ts', { eager: true })
|
const modules: any = import.meta.glob('./modules/*.ts', { eager: true })
|
||||||
import { hasPermission, set_next_route } from '@/utils/permission/index'
|
import { hasPermission, set_next_route } from '@/utils/permission/index'
|
||||||
|
|
||||||
const rolesRoutes: RouteRecordRaw[] = [...Object.keys(modules).map((key) => modules[key].default)]
|
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> = [
|
export const routes: Array<RouteRecordRaw> = [
|
||||||
{
|
{
|
||||||
@ -21,12 +10,7 @@ export const routes: Array<RouteRecordRaw> = [
|
|||||||
name: 'home',
|
name: 'home',
|
||||||
redirect: '/application',
|
redirect: '/application',
|
||||||
children: [
|
children: [
|
||||||
...rolesRoutes.map((r) => {
|
...rolesRoutes,
|
||||||
if (r.meta) {
|
|
||||||
r.meta.get_permission_route = get_workspace_permission_route
|
|
||||||
}
|
|
||||||
return r
|
|
||||||
}),
|
|
||||||
{
|
{
|
||||||
path: '/no-permission',
|
path: '/no-permission',
|
||||||
name: 'noPermission',
|
name: 'noPermission',
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user