Skip to content

Commit 0f91a33

Browse files
committed
!1 Vue 获取权限动态添加路由
Merge pull request !1 from lisgroup/develop
2 parents 36c9cae + 0ae785c commit 0f91a33

File tree

25 files changed

+293
-221
lines changed

25 files changed

+293
-221
lines changed

admin/src/permission.js

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import router from './router'
2+
import { routeSuper, routerAdmin } from './router/router'
23
import store from './store'
34
import NProgress from 'nprogress' // Progress 进度条
45
import 'nprogress/nprogress.css'// Progress 进度条样式
56
import { Message } from 'element-ui'
67
import { getToken } from '@/utils/auth' // 验权
78

9+
console.log(router)
10+
router.addRoutes(routerAdmin)
11+
router.addRoutes(routeSuper)
812
const whiteList = ['/login', '/index', '/line', '/home', '/404', '/', '', '/md'] // 不重定向白名单
913
router.beforeEach((to, from, next) => {
1014
NProgress.start()
@@ -15,15 +19,17 @@ router.beforeEach((to, from, next) => {
1519
} else {
1620
if (store.getters.roles.length === 0) {
1721
store.dispatch('GetInfo').then(res => { // 拉取用户信息
18-
if (!to.meta.role || res.data.roles.indexOf('Super Administrator') >= 0 || res.data.roles.indexOf(to.meta.role) >= 0) {
22+
const roles = res.data.roles
23+
console.log(roles)
24+
store.dispatch('GenerateRoutes', { roles }).then(() => { // 根据roles权限生成可访问的路由表
25+
router.addRoutes(store.getters.addRouters) // 动态添加可访问路由表
26+
next({ ...to, replace: true }) // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
27+
})
28+
if (!to.meta.role || whiteList.indexOf(to.path) !== -1 || hasPermission(res.data.roles, to.meta.roles)) {
1929
next()
2030
} else {
21-
if (whiteList.indexOf(to.path) !== -1) {
22-
next()
23-
} else {
24-
next({ path: '/404' })
25-
NProgress.done()
26-
}
31+
next({ path: '/404' })
32+
NProgress.done()
2733
}
2834
}).catch((err) => {
2935
store.dispatch('FedLogOut').then(() => {
@@ -32,15 +38,12 @@ router.beforeEach((to, from, next) => {
3238
})
3339
})
3440
} else {
35-
if (!to.meta.role || store.getters.roles.indexOf('Super Administrator') >= 0 || store.getters.roles.indexOf(to.meta.role) >= 0) {
41+
// 动态改变权限
42+
if (!to.meta.role || whiteList.indexOf(to.path) !== -1 || hasPermission(store.getters.roles, to.meta.roles)) {
3643
next()
3744
} else {
38-
if (whiteList.indexOf(to.path) !== -1) {
39-
next()
40-
} else {
41-
next({ path: '/404' })
42-
NProgress.done()
43-
}
45+
next({ path: '/404' })
46+
NProgress.done()
4447
}
4548
}
4649
}
@@ -57,3 +60,10 @@ router.beforeEach((to, from, next) => {
5760
router.afterEach(() => {
5861
NProgress.done() // 结束Progress
5962
})
63+
64+
// permissiom judge function
65+
function hasPermission(roles, permissionRoles) {
66+
if (roles.indexOf('Super Administrator') >= 0) return true // admin permission passed directly
67+
if (!permissionRoles) return true
68+
return roles.some(role => permissionRoles.indexOf(role) >= 0)
69+
}

admin/src/router/index.js

Lines changed: 2 additions & 177 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ const Layout = () => import('../views/layout/Layout')
2121
icon: 'svg-name' the icon show in the sidebar,
2222
}
2323
**/
24-
const Super = 'Super Administrator'
25-
const Admin = 'Admin'
2624

2725
// 基础路由
2826
const routeBase = [
@@ -48,180 +46,7 @@ const routeBase = [
4846
}
4947
]
5048

51-
// 管理一般路由
52-
const routeManage = [
53-
{
54-
path: '/api_excel',
55-
component: Layout,
56-
redirect: '/api_excel/index',
57-
name: 'Excel-List',
58-
meta: { title: '批量测试管理', icon: 'ico-table', role: Admin },
59-
children: [
60-
{ path: '/api_excel/edit/:id', name: 'EditExcel', component: () => import('@/views/api_excel/edit'), hidden: true },
61-
{
62-
path: '/api_excel/add',
63-
name: 'AddExcel',
64-
component: () => import('@/views/api_excel/add'),
65-
meta: { title: '上传测试', icon: 'excel', role: Admin }
66-
},
67-
{
68-
path: '/api_excel/index',
69-
name: 'Excel',
70-
component: () => import('@/views/api_excel/index'),
71-
meta: { title: '测试管理', icon: 'ico-aliyun', role: Admin }
72-
},
73-
{ path: '/api_param/add', name: 'AddApiParam', component: () => import('@/views/api_param/add'), hidden: true },
74-
{ path: '/api_param/edit/:id', name: 'EditApiParam', component: () => import('@/views/api_param/edit'), hidden: true },
75-
{
76-
path: '/api_param/index',
77-
name: 'ApiParam',
78-
component: () => import('@/views/api_param/index'),
79-
meta: { title: '接口列表', icon: 'api', role: Admin }
80-
}
81-
]
82-
}
83-
]
84-
85-
const routerAdmin = [
86-
// { path: '/', redirect: '/index', hidden: true },
87-
88-
{
89-
path: '/category',
90-
component: Layout,
91-
redirect: '/category/index',
92-
name: 'Category-Nav',
93-
meta: { title: '栏目菜单', icon: 'category', role: Super },
94-
children: [
95-
{ path: '/category/add', name: 'AddCategory', component: () => import('@/views/category/add'), meta: { title: '添加栏目' }, hidden: true },
96-
{ path: '/category/edit/:id', name: 'EditCategory', component: () => import('@/views/category/edit'), hidden: true },
97-
{
98-
path: '/category/index',
99-
name: 'Category',
100-
component: () => import('@/views/category/index'),
101-
meta: { title: '栏目管理', icon: 'ico-category', role: Super }
102-
},
103-
{ path: '/nav/add', name: 'AddNav', component: () => import('@/views/nav/add'), hidden: true },
104-
{ path: '/nav/edit/:id', name: 'EditNav', component: () => import('@/views/nav/edit'), hidden: true },
105-
{
106-
path: '/nav',
107-
name: 'Nav',
108-
component: () => import('@/views/nav'),
109-
meta: { title: '导航管理', icon: 'nav', role: Super }
110-
},
111-
{ path: '/tag/add', name: 'AddTag', component: () => import('@/views/tag/add'), meta: { title: '添加标签' }, hidden: true },
112-
{ path: '/tag/edit/:id', name: 'EditTag', component: () => import('@/views/tag/edit'), hidden: true },
113-
{
114-
path: '/tag',
115-
name: 'Tag',
116-
component: () => import('@/views/tag/index'),
117-
meta: { title: '标签列表', icon: 'tag', role: Super }
118-
}
119-
]
120-
},
121-
122-
{
123-
path: '/article',
124-
component: Layout,
125-
// redirect: '/article',
126-
name: 'Article-List',
127-
meta: { title: '文章管理', icon: 'article', role: Super },
128-
children: [
129-
{ path: '/article/edit/:id', name: 'EditArticle', component: () => import('@/views/article/edit'), hidden: true },
130-
{
131-
path: '/article/index',
132-
name: 'Article',
133-
component: () => import('@/views/article/index'),
134-
meta: { title: '文章管理', icon: 'ico-article', role: Super }
135-
},
136-
{
137-
path: '/article/add',
138-
name: 'AddArticle',
139-
component: () => import('@/views/article/add'),
140-
meta: { title: '添加文章', icon: 'add', role: Super }
141-
}
142-
]
143-
},
144-
145-
{
146-
path: '/list',
147-
component: Layout,
148-
redirect: '/task',
149-
name: '公交',
150-
meta: { title: '公交管理', icon: 'bus', role: Super },
151-
children: [
152-
{ path: '/task/search', name: 'search', component: () => import('@/views/task/search'), hidden: true },
153-
{ path: '/task/newBus', name: 'NewBus', component: () => import('@/views/task/newBus'), hidden: true },
154-
{ path: '/task/edit/:id', name: 'taskEdit', component: () => import('@/views/task/edit'), hidden: true },
155-
{
156-
path: '/task',
157-
name: '定时任务',
158-
component: () => import('@/views/task/index'),
159-
meta: { title: '定时任务', icon: 'task', role: Super }
160-
},
161-
{ path: 'lines/add', name: 'linesAdd', component: () => import('@/views/lines/add'), hidden: true },
162-
{ path: 'lines/edit/:id', name: 'linesEdit', component: () => import('@/views/lines/edit'), hidden: true },
163-
{
164-
path: 'lines',
165-
name: '公交列表',
166-
component: () => import('@/views/lines/index'),
167-
meta: { title: '公交列表', icon: 'table', role: Super }
168-
},
169-
{
170-
path: 'config',
171-
name: '配置管理',
172-
component: () => import('@/views/lines/index'),
173-
meta: { title: '配置列表', icon: 'table', role: Super }
174-
},
175-
{
176-
path: 'tree',
177-
name: 'Tree',
178-
component: () => import('@/views/tree/index'),
179-
meta: { title: 'Tree', icon: 'tree', role: Super },
180-
hidden: true
181-
}
182-
]
183-
},
184-
185-
{
186-
path: 'user',
187-
component: Layout,
188-
redirect: '/user',
189-
name: '权限',
190-
meta: { title: '权限管理', icon: 'auth', role: Super },
191-
children: [
192-
{ path: 'index', name: 'userIndex', component: () => import('@/views/user/index'), hidden: true },
193-
{ path: 'password', name: 'userPassword', component: () => import('@/views/user/password'), hidden: true },
194-
{ path: '/user/add', name: 'AddUser', component: () => import('@/views/user/add'), hidden: true },
195-
{ path: '/user/edit/:id', name: 'EditUser', component: () => import('@/views/user/edit'), hidden: true },
196-
{
197-
path: '/user',
198-
name: '用户管理',
199-
component: () => import('@/views/user/index'),
200-
meta: { title: '用户列表', icon: 'user', role: Super }
201-
},
202-
{ path: '/permission/add', name: 'AddPermission', component: () => import('@/views/permission/add'), hidden: true },
203-
{ path: '/permission/edit/:id', name: 'EditPermission', component: () => import('@/views/permission/edit'), hidden: true },
204-
{
205-
path: '/permission',
206-
name: '权限列表',
207-
component: () => import('@/views/permission/index'),
208-
meta: { title: '权限列表', icon: 'permission', role: Super }
209-
},
210-
211-
{ path: '/role/add', name: 'AddRole', component: () => import('@/views/role/add'), hidden: true },
212-
{ path: '/role/edit/:id', name: 'EditRole', component: () => import('@/views/role/edit'), hidden: true },
213-
{
214-
path: '/role',
215-
name: '角色管理',
216-
component: () => import('@/views/role/index'),
217-
meta: { title: '角色管理', icon: 'role', role: Super }
218-
}
219-
]
220-
}
221-
222-
]
223-
224-
const routeOther = [
49+
const routeTest = [
22550
{
22651
path: '/form',
22752
component: Layout,
@@ -294,7 +119,7 @@ const routeOther = [
294119
{ path: '*', redirect: '/404', hidden: true }
295120
]
296121

297-
export const constantRouterMap = [...routeBase, ...routeManage, ...routerAdmin, ...routeOther]
122+
export const constantRouterMap = [...routeBase, ...routeTest]
298123

299124
export default new Router({
300125
// mode: 'history', //后端支持可开

0 commit comments

Comments
 (0)