parent
88c7653dab
commit
d4261bc401
7 changed files with 121 additions and 89 deletions
@ -0,0 +1,44 @@ |
|||||||
|
import {hasPermission, hasRole} from '@/utils/authority-utils' |
||||||
|
import {loginIgnore} from '@/router/index' |
||||||
|
import {checkAuthorization} from '@/utils/request' |
||||||
|
|
||||||
|
/** |
||||||
|
* 登录守卫 |
||||||
|
* @param to |
||||||
|
* @param form |
||||||
|
* @param next |
||||||
|
* @param options |
||||||
|
*/ |
||||||
|
const loginGuard = (to, from, next, options) => { |
||||||
|
const {message} = options |
||||||
|
if (!loginIgnore.includes(to) && !checkAuthorization()) { |
||||||
|
message.warning('登录已失效,请重新登录') |
||||||
|
next({path: '/login'}) |
||||||
|
} else { |
||||||
|
next() |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 权限守卫 |
||||||
|
* @param to |
||||||
|
* @param form |
||||||
|
* @param next |
||||||
|
* @param options |
||||||
|
*/ |
||||||
|
const authorityGuard = (to, from, next, options) => { |
||||||
|
const {store, message} = options |
||||||
|
const permissions = store.getters['account/permissions'] |
||||||
|
const roles = store.getters['account/roles'] |
||||||
|
if (!hasPermission(to, permissions) && !hasRole(to, roles)) { |
||||||
|
message.warning(`对不起,您无权访问页面: ${to.fullPath},请联系管理员`) |
||||||
|
next({path: '/403'}) |
||||||
|
} else { |
||||||
|
next() |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
export default { |
||||||
|
beforeEach: [loginGuard, authorityGuard], |
||||||
|
afterEach: [] |
||||||
|
} |
@ -0,0 +1,50 @@ |
|||||||
|
/** |
||||||
|
* 判断是否有路由的权限 |
||||||
|
* @param route 路由 |
||||||
|
* @param permissions 用户权限集合 |
||||||
|
* @returns {boolean|*} |
||||||
|
*/ |
||||||
|
function hasPermission(route, permissions) { |
||||||
|
const authority = route.meta.authority || '*' |
||||||
|
let required = '*' |
||||||
|
if (typeof authority === 'string') { |
||||||
|
required = authority |
||||||
|
} else if (typeof authority === 'object') { |
||||||
|
required = authority.permission |
||||||
|
} |
||||||
|
return required === '*' || (permissions && permissions.findIndex(item => item === required || item.id === required) !== -1) |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 判断是否有路由需要的角色 |
||||||
|
* @param route 路由 |
||||||
|
* @param roles 用户角色集合 |
||||||
|
*/ |
||||||
|
function hasRole(route, roles) { |
||||||
|
const authority = route.meta.authority || '*' |
||||||
|
let required = undefined |
||||||
|
if (typeof authority === 'object') { |
||||||
|
required = authority.role |
||||||
|
} |
||||||
|
return authority === '*' || hasAnyRole(required, roles) |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 判断是否有需要的任意一个角色 |
||||||
|
* @param required {String | Array[String]} 需要的角色,可以是单个角色或者一个角色数组 |
||||||
|
* @param roles 拥有的角色 |
||||||
|
* @returns {boolean} |
||||||
|
*/ |
||||||
|
function hasAnyRole(required, roles) { |
||||||
|
if (!required) { |
||||||
|
return false |
||||||
|
} else if(Array.isArray(required)) { |
||||||
|
return roles.findIndex(role => { |
||||||
|
return required.findIndex(item => item === role || item === role.id) !== -1 |
||||||
|
}) !== -1 |
||||||
|
} else { |
||||||
|
return roles.findIndex(role => role === required || role.id === required) !== -1 |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
export {hasPermission, hasRole} |
Loading…
Reference in new issue