作者 RuoYi

路由支持单独配置菜单或角色权限

@@ -17,6 +17,8 @@ import Layout from '@/layout' @@ -17,6 +17,8 @@ import Layout from '@/layout'
17 * redirect: noRedirect // 当设置 noRedirect 的时候该路由在面包屑导航中不可被点击 17 * redirect: noRedirect // 当设置 noRedirect 的时候该路由在面包屑导航中不可被点击
18 * name:'router-name' // 设定路由的名字,一定要填写不然使用<keep-alive>时会出现各种问题 18 * name:'router-name' // 设定路由的名字,一定要填写不然使用<keep-alive>时会出现各种问题
19 * query: '{"id": 1, "name": "ry"}' // 访问路由的默认传递参数 19 * query: '{"id": 1, "name": "ry"}' // 访问路由的默认传递参数
  20 + * roles: ['admin', 'common'] // 访问路由的角色权限
  21 + * permissions: ['a:a:a', 'b:b:b'] // 访问路由的菜单权限
20 * meta : { 22 * meta : {
21 noCache: true // 如果设置为true,则不会被 <keep-alive> 缓存(默认 false) 23 noCache: true // 如果设置为true,则不会被 <keep-alive> 缓存(默认 false)
22 title: 'title' // 设置该路由在侧边栏和面包屑中展示的名字 24 title: 'title' // 设置该路由在侧边栏和面包屑中展示的名字
@@ -85,11 +87,16 @@ export const constantRoutes = [ @@ -85,11 +87,16 @@ export const constantRoutes = [
85 meta: { title: '个人中心', icon: 'user' } 87 meta: { title: '个人中心', icon: 'user' }
86 } 88 }
87 ] 89 ]
88 - }, 90 + }
  91 +]
  92 +
  93 +// 动态路由,基于用户权限动态去加载
  94 +export const dynamicRoutes = [
89 { 95 {
90 path: '/system/user-auth', 96 path: '/system/user-auth',
91 component: Layout, 97 component: Layout,
92 hidden: true, 98 hidden: true,
  99 + permissions: ['system:user:edit'],
93 children: [ 100 children: [
94 { 101 {
95 path: 'role/:userId(\\d+)', 102 path: 'role/:userId(\\d+)',
@@ -103,6 +110,7 @@ export const constantRoutes = [ @@ -103,6 +110,7 @@ export const constantRoutes = [
103 path: '/system/role-auth', 110 path: '/system/role-auth',
104 component: Layout, 111 component: Layout,
105 hidden: true, 112 hidden: true,
  113 + permissions: ['system:role:edit'],
106 children: [ 114 children: [
107 { 115 {
108 path: 'user/:roleId(\\d+)', 116 path: 'user/:roleId(\\d+)',
@@ -116,6 +124,7 @@ export const constantRoutes = [ @@ -116,6 +124,7 @@ export const constantRoutes = [
116 path: '/system/dict-data', 124 path: '/system/dict-data',
117 component: Layout, 125 component: Layout,
118 hidden: true, 126 hidden: true,
  127 + permissions: ['system:dict:list'],
119 children: [ 128 children: [
120 { 129 {
121 path: 'index/:dictId(\\d+)', 130 path: 'index/:dictId(\\d+)',
@@ -129,6 +138,7 @@ export const constantRoutes = [ @@ -129,6 +138,7 @@ export const constantRoutes = [
129 path: '/monitor/job-log', 138 path: '/monitor/job-log',
130 component: Layout, 139 component: Layout,
131 hidden: true, 140 hidden: true,
  141 + permissions: ['monitor:job:list'],
132 children: [ 142 children: [
133 { 143 {
134 path: 'index', 144 path: 'index',
@@ -142,6 +152,7 @@ export const constantRoutes = [ @@ -142,6 +152,7 @@ export const constantRoutes = [
142 path: '/tool/gen-edit', 152 path: '/tool/gen-edit',
143 component: Layout, 153 component: Layout,
144 hidden: true, 154 hidden: true,
  155 + permissions: ['tool:gen:edit'],
145 children: [ 156 children: [
146 { 157 {
147 path: 'index', 158 path: 'index',
1 -import { constantRoutes } from '@/router' 1 +import auth from '@/plugins/auth'
  2 +import router, { constantRoutes, dynamicRoutes } from '@/router'
2 import { getRouters } from '@/api/menu' 3 import { getRouters } from '@/api/menu'
3 import Layout from '@/layout/index' 4 import Layout from '@/layout/index'
4 import ParentView from '@/components/ParentView' 5 import ParentView from '@/components/ParentView'
@@ -42,7 +43,9 @@ const permission = { @@ -42,7 +43,9 @@ const permission = {
42 const rdata = JSON.parse(JSON.stringify(res.data)) 43 const rdata = JSON.parse(JSON.stringify(res.data))
43 const sidebarRoutes = filterAsyncRouter(sdata) 44 const sidebarRoutes = filterAsyncRouter(sdata)
44 const rewriteRoutes = filterAsyncRouter(rdata, false, true) 45 const rewriteRoutes = filterAsyncRouter(rdata, false, true)
  46 + const asyncRoutes = filterDynamicRoutes(dynamicRoutes);
45 rewriteRoutes.push({ path: '*', redirect: '/404', hidden: true }) 47 rewriteRoutes.push({ path: '*', redirect: '/404', hidden: true })
  48 + router.addRoutes(asyncRoutes);
46 commit('SET_ROUTES', rewriteRoutes) 49 commit('SET_ROUTES', rewriteRoutes)
47 commit('SET_SIDEBAR_ROUTERS', constantRoutes.concat(sidebarRoutes)) 50 commit('SET_SIDEBAR_ROUTERS', constantRoutes.concat(sidebarRoutes))
48 commit('SET_DEFAULT_ROUTES', sidebarRoutes) 51 commit('SET_DEFAULT_ROUTES', sidebarRoutes)
@@ -106,6 +109,23 @@ function filterChildren(childrenMap, lastRouter = false) { @@ -106,6 +109,23 @@ function filterChildren(childrenMap, lastRouter = false) {
106 return children 109 return children
107 } 110 }
108 111
  112 +// 动态路由遍历,验证是否具备权限
  113 +export function filterDynamicRoutes(routes) {
  114 + const res = []
  115 + routes.forEach(route => {
  116 + if (route.permissions) {
  117 + if (auth.hasPermiOr(route.permissions)) {
  118 + res.push(route)
  119 + }
  120 + } else if (route.roles) {
  121 + if (auth.hasRoleOr(route.roles)) {
  122 + res.push(route)
  123 + }
  124 + }
  125 + })
  126 + return res
  127 +}
  128 +
109 export const loadView = (view) => { 129 export const loadView = (view) => {
110 if (process.env.NODE_ENV === 'development') { 130 if (process.env.NODE_ENV === 'development') {
111 return (resolve) => require([`@/views/${view}`], resolve) 131 return (resolve) => require([`@/views/${view}`], resolve)