Przeglądaj źródła

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

RuoYi 4 lat temu
rodzic
commit
1e7b9fb94c

+ 12 - 1
ruoyi-ui/src/router/index.js

@@ -17,6 +17,8 @@ import Layout from '@/layout'
17 17
  * redirect: noRedirect             // 当设置 noRedirect 的时候该路由在面包屑导航中不可被点击
18 18
  * name:'router-name'               // 设定路由的名字,一定要填写不然使用<keep-alive>时会出现各种问题
19 19
  * query: '{"id": 1, "name": "ry"}' // 访问路由的默认传递参数
20
+ * roles: ['admin', 'common']       // 访问路由的角色权限
21
+ * permissions: ['a:a:a', 'b:b:b']  // 访问路由的菜单权限
20 22
  * meta : {
21 23
     noCache: true                   // 如果设置为true,则不会被 <keep-alive> 缓存(默认 false)
22 24
     title: 'title'                  // 设置该路由在侧边栏和面包屑中展示的名字
@@ -85,11 +87,16 @@ export const constantRoutes = [
85 87
         meta: { title: '个人中心', icon: 'user' }
86 88
       }
87 89
     ]
88
-  },
90
+  }
91
+]
92
+
93
+// 动态路由,基于用户权限动态去加载
94
+export const dynamicRoutes = [
89 95
   {
90 96
     path: '/system/user-auth',
91 97
     component: Layout,
92 98
     hidden: true,
99
+    permissions: ['system:user:edit'],
93 100
     children: [
94 101
       {
95 102
         path: 'role/:userId(\\d+)',
@@ -103,6 +110,7 @@ export const constantRoutes = [
103 110
     path: '/system/role-auth',
104 111
     component: Layout,
105 112
     hidden: true,
113
+    permissions: ['system:role:edit'],
106 114
     children: [
107 115
       {
108 116
         path: 'user/:roleId(\\d+)',
@@ -116,6 +124,7 @@ export const constantRoutes = [
116 124
     path: '/system/dict-data',
117 125
     component: Layout,
118 126
     hidden: true,
127
+    permissions: ['system:dict:list'],
119 128
     children: [
120 129
       {
121 130
         path: 'index/:dictId(\\d+)',
@@ -129,6 +138,7 @@ export const constantRoutes = [
129 138
     path: '/monitor/job-log',
130 139
     component: Layout,
131 140
     hidden: true,
141
+    permissions: ['monitor:job:list'],
132 142
     children: [
133 143
       {
134 144
         path: 'index',
@@ -142,6 +152,7 @@ export const constantRoutes = [
142 152
     path: '/tool/gen-edit',
143 153
     component: Layout,
144 154
     hidden: true,
155
+    permissions: ['tool:gen:edit'],
145 156
     children: [
146 157
       {
147 158
         path: 'index',

+ 21 - 1
ruoyi-ui/src/store/modules/permission.js

@@ -1,4 +1,5 @@
1
-import { constantRoutes } from '@/router'
1
+import auth from '@/plugins/auth'
2
+import router, { constantRoutes, dynamicRoutes } from '@/router'
2 3
 import { getRouters } from '@/api/menu'
3 4
 import Layout from '@/layout/index'
4 5
 import ParentView from '@/components/ParentView'
@@ -42,7 +43,9 @@ const permission = {
42 43
           const rdata = JSON.parse(JSON.stringify(res.data))
43 44
           const sidebarRoutes = filterAsyncRouter(sdata)
44 45
           const rewriteRoutes = filterAsyncRouter(rdata, false, true)
46
+          const asyncRoutes = filterDynamicRoutes(dynamicRoutes);
45 47
           rewriteRoutes.push({ path: '*', redirect: '/404', hidden: true })
48
+          router.addRoutes(asyncRoutes);
46 49
           commit('SET_ROUTES', rewriteRoutes)
47 50
           commit('SET_SIDEBAR_ROUTERS', constantRoutes.concat(sidebarRoutes))
48 51
           commit('SET_DEFAULT_ROUTES', sidebarRoutes)
@@ -106,6 +109,23 @@ function filterChildren(childrenMap, lastRouter = false) {
106 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 129
 export const loadView = (view) => {
110 130
   if (process.env.NODE_ENV === 'development') {
111 131
     return (resolve) => require([`@/views/${view}`], resolve)