Skip to content

Commit 54acf1e

Browse files
committed
perf[permission]:set role => roles
1 parent de08e49 commit 54acf1e

File tree

6 files changed

+28
-25
lines changed

6 files changed

+28
-25
lines changed

src/mock/login.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ import { param2Obj } from '@/utils'
22

33
const userMap = {
44
admin: {
5-
role: ['admin'],
5+
roles: ['admin'],
66
token: 'admin',
77
introduction: '我是超级管理员',
88
avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
99
name: 'Super Admin'
1010
},
1111
editor: {
12-
role: ['editor'],
12+
roles: ['editor'],
1313
token: 'editor',
1414
introduction: '我是编辑',
1515
avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',

src/permission.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import router from './router'
22
import store from './store'
3+
import { Message } from 'element-ui'
34
import NProgress from 'nprogress' // progress bar
45
import 'nprogress/nprogress.css'// progress bar style
56
import { getToken } from '@/utils/auth' // getToken from cookie
6-
import { Message } from 'element-ui'
77

8-
// permissiom judge
8+
NProgress.configure({ showSpinner: false })// NProgress Configuration
9+
10+
// permissiom judge function
911
function hasPermission(roles, permissionRoles) {
1012
if (roles.indexOf('admin') >= 0) return true // admin permission passed directly
1113
if (!permissionRoles) return true
@@ -16,15 +18,16 @@ const whiteList = ['/login', '/authredirect']// no redirect whitelist
1618

1719
router.beforeEach((to, from, next) => {
1820
NProgress.start() // start progress bar
19-
if (getToken()) { // 判断是否有token
21+
if (getToken()) { // determine if there has token
22+
/* has token*/
2023
if (to.path === '/login') {
2124
next({ path: '/' })
22-
NProgress.done() // router在hash模式下 手动改变hash 重定向回来 不会触发afterEach 暂时hack方案 ps:history模式下无问题,可删除该行!
25+
NProgress.done() // if current page is dashboard will not trigger afterEach hook, so manually handle it
2326
} else {
2427
if (store.getters.roles.length === 0) { // 判断当前用户是否已拉取完user_info信息
2528
store.dispatch('GetUserInfo').then(res => { // 拉取user_info
26-
const roles = res.data.role
27-
store.dispatch('GenerateRoutes', { roles }).then(() => { // 生成可访问的路由表
29+
const roles = res.data.roles // note: roles must be a array! such as: ['editor','develop']
30+
store.dispatch('GenerateRoutes', { roles }).then(() => { // 根据roles权限生成可访问的路由表
2831
router.addRoutes(store.getters.addRouters) // 动态添加可访问路由表
2932
next({ ...to, replace: true }) // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
3033
})
@@ -36,21 +39,21 @@ router.beforeEach((to, from, next) => {
3639
})
3740
} else {
3841
// 没有动态改变权限的需求可直接next() 删除下方权限判断 ↓
39-
if (hasPermission(store.getters.roles, to.meta.role)) {
42+
if (hasPermission(store.getters.roles, to.meta.roles)) {
4043
next()//
4144
} else {
42-
next({ path: '/401', query: { noGoBack: true }})
43-
NProgress.done() // router在hash模式下 手动改变hash 重定向回来 不会触发afterEach 暂时hack方案 ps:history模式下无问题,可删除该行!
45+
next({ path: '/401', replace: true, query: { noGoBack: true }})
4446
}
4547
// 可删 ↑
4648
}
4749
}
4850
} else {
51+
/* has no token*/
4952
if (whiteList.indexOf(to.path) !== -1) { // 在免登录白名单,直接进入
5053
next()
5154
} else {
5255
next('/login') // 否则全部重定向到登录页
53-
NProgress.done() // router在hash模式下 手动改变hash 重定向回来 不会触发afterEach 暂时hack方案 ps:history模式下无问题,可删除该行!
56+
NProgress.done() // if current page is login will not trigger afterEach hook, so manually handle it
5457
}
5558
}
5659
})

src/router/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import Layout from '../views/layout/Layout'
1818
* redirect: noredirect if `redirect:noredirect` will no redirct in the breadcrumb
1919
* name:'router-name' the name is used by <keep-alive> (must set!!!)
2020
* meta : {
21-
role: ['admin','editor'] will control the page role (you can set multiple roles)
21+
roles: ['admin','editor'] will control the page roles (you can set multiple roles)
2222
title: 'title' the name show in submenu and breadcrumb (recommend set)
2323
icon: 'svg-name' the icon show in the sidebar,
2424
noCache: true if fasle ,the page will no be cached(default is false)
@@ -54,7 +54,7 @@ export const constantRouterMap = [
5454
]
5555

5656
export default new Router({
57-
// mode: 'history', //后端支持可开
57+
// mode: 'history', // require service support
5858
scrollBehavior: () => ({ y: 0 }),
5959
routes: constantRouterMap
6060
})
@@ -64,15 +64,15 @@ export const asyncRouterMap = [
6464
path: '/permission',
6565
component: Layout,
6666
redirect: '/permission/index',
67-
meta: { role: ['admin'] },
67+
meta: { roles: ['admin'] }, // you can set roles in root nav
6868
children: [{
6969
path: 'index',
7070
component: _import('permission/index'),
7171
name: 'permission',
7272
meta: {
7373
title: 'permission',
7474
icon: 'lock',
75-
role: ['admin']
75+
roles: ['admin'] // or you can only set roles in sub nav
7676
}
7777
}]
7878
},

src/store/modules/permission.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import { asyncRouterMap, constantRouterMap } from '@/router'
66
* @param route
77
*/
88
function hasPermission(roles, route) {
9-
if (route.meta && route.meta.role) {
10-
return roles.some(role => route.meta.role.indexOf(role) >= 0)
9+
if (route.meta && route.meta.roles) {
10+
return roles.some(role => route.meta.roles.indexOf(role) >= 0)
1111
} else {
1212
return true
1313
}

src/store/modules/user.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ const user = {
6767
reject('error')
6868
}
6969
const data = response.data
70-
commit('SET_ROLES', data.role)
70+
commit('SET_ROLES', data.roles)
7171
commit('SET_NAME', data.name)
7272
commit('SET_AVATAR', data.avatar)
7373
commit('SET_INTRODUCTION', data.introduction)
@@ -116,13 +116,13 @@ const user = {
116116
},
117117

118118
// 动态修改权限
119-
ChangeRole({ commit }, role) {
119+
ChangeRoles({ commit }, role) {
120120
return new Promise(resolve => {
121121
commit('SET_TOKEN', role)
122122
setToken(role)
123123
getUserInfo(role).then(response => {
124124
const data = response.data
125-
commit('SET_ROLES', data.role)
125+
commit('SET_ROLES', data.roles)
126126
commit('SET_NAME', data.name)
127127
commit('SET_AVATAR', data.avatar)
128128
commit('SET_INTRODUCTION', data.introduction)

src/views/permission/index.vue

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<div class="app-container">
33
<div style="margin-bottom:15px;">{{$t('permission.roles')}}: {{roles}}</div>
44
{{$t('permission.switchRoles')}}:
5-
<el-radio-group v-model="role">
5+
<el-radio-group v-model="switchRoles">
66
<el-radio-button label="editor"></el-radio-button>
77
</el-radio-group>
88
</div>
@@ -15,7 +15,7 @@ export default{
1515
name: 'permission',
1616
data() {
1717
return {
18-
role: ''
18+
switchRoles: ''
1919
}
2020
},
2121
computed: {
@@ -24,8 +24,8 @@ export default{
2424
])
2525
},
2626
watch: {
27-
role(val) {
28-
this.$store.dispatch('ChangeRole', val).then(() => {
27+
switchRoles(val) {
28+
this.$store.dispatch('ChangeRoles', val).then(() => {
2929
this.$router.push({ path: '/permission/index?' + +new Date() })
3030
})
3131
}

0 commit comments

Comments
 (0)