Skip to content

Commit 6b66acd

Browse files
committed
fix(view): 前端权限控制
1 parent 55d3d69 commit 6b66acd

File tree

2 files changed

+42
-22
lines changed

2 files changed

+42
-22
lines changed

admin/src/permission.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,33 @@ router.beforeEach((to, from, next) => {
1515
} else {
1616
if (store.getters.roles.length === 0) {
1717
store.dispatch('GetInfo').then(res => { // 拉取用户信息
18-
next()
18+
if (!to.meta.role || res.data.roles.indexOf('Super Administrator') >= 0 || res.data.roles.indexOf(to.meta.role) >= 0) {
19+
next()
20+
} else {
21+
if (whiteList.indexOf(to.path) !== -1) {
22+
next()
23+
} else {
24+
next({ path: '/404' })
25+
NProgress.done()
26+
}
27+
}
1928
}).catch((err) => {
2029
store.dispatch('FedLogOut').then(() => {
2130
Message.error(err || 'Verification failed, please login again')
2231
next({ path: '/admin' })
2332
})
2433
})
2534
} else {
26-
next()
35+
if (!to.meta.role || store.getters.roles.indexOf('Super Administrator') >= 0 || store.getters.roles.indexOf(to.meta.role) >= 0) {
36+
next()
37+
} else {
38+
if (whiteList.indexOf(to.path) !== -1) {
39+
next()
40+
} else {
41+
next({ path: '/404' })
42+
NProgress.done()
43+
}
44+
}
2745
}
2846
}
2947
} else {

admin/src/router/index.js

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ 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'
2426

2527
// 基础路由
2628
const routeBase = [
@@ -53,28 +55,28 @@ const routeManage = [
5355
component: Layout,
5456
redirect: '/api_excel/index',
5557
name: 'Excel-List',
56-
meta: { title: '批量测试管理', icon: 'ico-table' },
58+
meta: { title: '批量测试管理', icon: 'ico-table', role: Admin },
5759
children: [
5860
{ path: '/api_excel/edit/:id', name: 'EditExcel', component: () => import('@/views/api_excel/edit'), hidden: true },
5961
{
6062
path: '/api_excel/add',
6163
name: 'AddExcel',
6264
component: () => import('@/views/api_excel/add'),
63-
meta: { title: '上传测试', icon: 'excel' }
65+
meta: { title: '上传测试', icon: 'excel', role: Admin }
6466
},
6567
{
6668
path: '/api_excel/index',
6769
name: 'Excel',
6870
component: () => import('@/views/api_excel/index'),
69-
meta: { title: '测试管理', icon: 'ico-aliyun' }
71+
meta: { title: '测试管理', icon: 'ico-aliyun', role: Admin }
7072
},
7173
{ path: '/api_param/add', name: 'AddApiParam', component: () => import('@/views/api_param/add'), hidden: true },
7274
{ path: '/api_param/edit/:id', name: 'EditApiParam', component: () => import('@/views/api_param/edit'), hidden: true },
7375
{
7476
path: '/api_param/index',
7577
name: 'ApiParam',
7678
component: () => import('@/views/api_param/index'),
77-
meta: { title: '接口列表', icon: 'api' }
79+
meta: { title: '接口列表', icon: 'api', role: Admin }
7880
}
7981
]
8082
}
@@ -88,31 +90,31 @@ const routerAdmin = [
8890
component: Layout,
8991
redirect: '/category/index',
9092
name: 'Category-Nav',
91-
meta: { title: '栏目菜单', icon: 'category' },
93+
meta: { title: '栏目菜单', icon: 'category', role: Super },
9294
children: [
9395
{ path: '/category/add', name: 'AddCategory', component: () => import('@/views/category/add'), meta: { title: '添加栏目' }, hidden: true },
9496
{ path: '/category/edit/:id', name: 'EditCategory', component: () => import('@/views/category/edit'), hidden: true },
9597
{
9698
path: '/category/index',
9799
name: 'Category',
98100
component: () => import('@/views/category/index'),
99-
meta: { title: '栏目管理', icon: 'ico-category' }
101+
meta: { title: '栏目管理', icon: 'ico-category', role: Super }
100102
},
101103
{ path: '/nav/add', name: 'AddNav', component: () => import('@/views/nav/add'), hidden: true },
102104
{ path: '/nav/edit/:id', name: 'EditNav', component: () => import('@/views/nav/edit'), hidden: true },
103105
{
104106
path: '/nav',
105107
name: 'Nav',
106108
component: () => import('@/views/nav'),
107-
meta: { title: '导航管理', icon: 'nav' }
109+
meta: { title: '导航管理', icon: 'nav', role: Super }
108110
},
109111
{ path: '/tag/add', name: 'AddTag', component: () => import('@/views/tag/add'), meta: { title: '添加标签' }, hidden: true },
110112
{ path: '/tag/edit/:id', name: 'EditTag', component: () => import('@/views/tag/edit'), hidden: true },
111113
{
112114
path: '/tag',
113115
name: 'Tag',
114116
component: () => import('@/views/tag/index'),
115-
meta: { title: '标签列表', icon: 'tag' }
117+
meta: { title: '标签列表', icon: 'tag', role: Super }
116118
}
117119
]
118120
},
@@ -122,20 +124,20 @@ const routerAdmin = [
122124
component: Layout,
123125
// redirect: '/article',
124126
name: 'Article-List',
125-
meta: { title: '文章管理', icon: 'article' },
127+
meta: { title: '文章管理', icon: 'article', role: Super },
126128
children: [
127129
{ path: '/article/edit/:id', name: 'EditArticle', component: () => import('@/views/article/edit'), hidden: true },
128130
{
129131
path: '/article/index',
130132
name: 'Article',
131133
component: () => import('@/views/article/index'),
132-
meta: { title: '文章管理', icon: 'ico-article' }
134+
meta: { title: '文章管理', icon: 'ico-article', role: Super }
133135
},
134136
{
135137
path: '/article/add',
136138
name: 'AddArticle',
137139
component: () => import('@/views/article/add'),
138-
meta: { title: '添加文章', icon: 'add' }
140+
meta: { title: '添加文章', icon: 'add', role: Super }
139141
}
140142
]
141143
},
@@ -145,7 +147,7 @@ const routerAdmin = [
145147
component: Layout,
146148
redirect: '/task',
147149
name: '公交',
148-
meta: { title: '公交管理', icon: 'bus' },
150+
meta: { title: '公交管理', icon: 'bus', role: Super },
149151
children: [
150152
{ path: '/task/search', name: 'search', component: () => import('@/views/task/search'), hidden: true },
151153
{ path: '/task/newBus', name: 'NewBus', component: () => import('@/views/task/newBus'), hidden: true },
@@ -154,27 +156,27 @@ const routerAdmin = [
154156
path: '/task',
155157
name: '定时任务',
156158
component: () => import('@/views/task/index'),
157-
meta: { title: '定时任务', icon: 'task' }
159+
meta: { title: '定时任务', icon: 'task', role: Super }
158160
},
159161
{ path: 'lines/add', name: 'linesAdd', component: () => import('@/views/lines/add'), hidden: true },
160162
{ path: 'lines/edit/:id', name: 'linesEdit', component: () => import('@/views/lines/edit'), hidden: true },
161163
{
162164
path: 'lines',
163165
name: '公交列表',
164166
component: () => import('@/views/lines/index'),
165-
meta: { title: '公交列表', icon: 'table' }
167+
meta: { title: '公交列表', icon: 'table', role: Super }
166168
},
167169
{
168170
path: 'config',
169171
name: '配置管理',
170172
component: () => import('@/views/lines/index'),
171-
meta: { title: '配置列表', icon: 'table' }
173+
meta: { title: '配置列表', icon: 'table', role: Super }
172174
},
173175
{
174176
path: 'tree',
175177
name: 'Tree',
176178
component: () => import('@/views/tree/index'),
177-
meta: { title: 'Tree', icon: 'tree' },
179+
meta: { title: 'Tree', icon: 'tree', role: Super },
178180
hidden: true
179181
}
180182
]
@@ -185,7 +187,7 @@ const routerAdmin = [
185187
component: Layout,
186188
redirect: '/user',
187189
name: '权限',
188-
meta: { title: '权限管理', icon: 'auth' },
190+
meta: { title: '权限管理', icon: 'auth', role: Super },
189191
children: [
190192
{ path: 'index', name: 'userIndex', component: () => import('@/views/user/index'), hidden: true },
191193
{ path: 'password', name: 'userPassword', component: () => import('@/views/user/password'), hidden: true },
@@ -195,15 +197,15 @@ const routerAdmin = [
195197
path: '/user',
196198
name: '用户管理',
197199
component: () => import('@/views/user/index'),
198-
meta: { title: '用户列表', icon: 'user' }
200+
meta: { title: '用户列表', icon: 'user', role: Super }
199201
},
200202
{ path: '/permission/add', name: 'AddPermission', component: () => import('@/views/permission/add'), hidden: true },
201203
{ path: '/permission/edit/:id', name: 'EditPermission', component: () => import('@/views/permission/edit'), hidden: true },
202204
{
203205
path: '/permission',
204206
name: '权限列表',
205207
component: () => import('@/views/permission/index'),
206-
meta: { title: '权限列表', icon: 'permission' }
208+
meta: { title: '权限列表', icon: 'permission', role: Super }
207209
},
208210

209211
{ path: '/role/add', name: 'AddRole', component: () => import('@/views/role/add'), hidden: true },
@@ -212,7 +214,7 @@ const routerAdmin = [
212214
path: '/role',
213215
name: '角色管理',
214216
component: () => import('@/views/role/index'),
215-
meta: { title: '角色管理', icon: 'role' }
217+
meta: { title: '角色管理', icon: 'role', role: Super }
216218
}
217219
]
218220
}

0 commit comments

Comments
 (0)