|
|
|
@ -1,5 +1,10 @@
|
|
|
|
|
import { asyncRouterMap, constantRouterMap } from 'src/router';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 通过meta.role判断是否与当前用户权限匹配
|
|
|
|
|
* @param roles
|
|
|
|
|
* @param route
|
|
|
|
|
*/
|
|
|
|
|
function hasPermission(roles, route) {
|
|
|
|
|
if (route.meta && route.meta.role) {
|
|
|
|
|
return roles.some(role => route.meta.role.indexOf(role) >= 0)
|
|
|
|
@ -12,12 +17,11 @@ function hasPermission(roles, route) {
|
|
|
|
|
* 递归过滤异步路由表,返回符合用户角色权限的路由表
|
|
|
|
|
* @param asyncRouterMap
|
|
|
|
|
* @param roles
|
|
|
|
|
* @returns {Array.<T>|*}
|
|
|
|
|
*/
|
|
|
|
|
function filterAsyncRouter(asyncRouterMap, roles) {
|
|
|
|
|
let accessedRouters = asyncRouterMap.filter(route => {
|
|
|
|
|
if(hasPermission(roles, route)) {
|
|
|
|
|
if(route.children && route.children.length) {
|
|
|
|
|
const accessedRouters = asyncRouterMap.filter(route => {
|
|
|
|
|
if (hasPermission(roles, route)) {
|
|
|
|
|
if (route.children && route.children.length) {
|
|
|
|
|
route.children = filterAsyncRouter(route.children, roles)
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
@ -27,24 +31,21 @@ function filterAsyncRouter(asyncRouterMap, roles) {
|
|
|
|
|
return accessedRouters
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const permission = {
|
|
|
|
|
state: {
|
|
|
|
|
routers: constantRouterMap,
|
|
|
|
|
addRouters: []
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
mutations: {
|
|
|
|
|
SET_ROUTERS: (state, routers) => {
|
|
|
|
|
state.addRouters = routers;
|
|
|
|
|
state.routers = constantRouterMap.concat(routers);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
actions: {
|
|
|
|
|
GenerateRoutes({ commit }, data) {
|
|
|
|
|
return new Promise(resolve => {
|
|
|
|
|
const { roles } = data;
|
|
|
|
|
const { roles } = data
|
|
|
|
|
let accessedRouters
|
|
|
|
|
if (roles.indexOf('admin') >= 0) {
|
|
|
|
|
accessedRouters = asyncRouterMap
|
|
|
|
@ -58,5 +59,4 @@ const permission = {
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export default permission;
|
|
|
|
|