2019-09-23 17:47:54 +08:00
|
|
|
|
import { asyncRouter, constantRoutes, user, userLesson, userExam, userSimulation, userScreen, userPlan, superAdmin, admin, userDesign } from '@/router';
|
2019-07-02 16:29:52 +08:00
|
|
|
|
import { PermissionType } from '@/utils/PermissionType';
|
|
|
|
|
import { UrlConfig } from '@/router/index';
|
2019-09-23 17:47:54 +08:00
|
|
|
|
import { getSessionStorage } from '@/utils/auth';
|
2019-07-02 16:29:52 +08:00
|
|
|
|
|
|
|
|
|
function setHonePagePath(route, roles) {
|
|
|
|
|
if (roles && roles.length === 2 && roles.indexOf(user) >= 0 && (route.path === '/' || route.path === 'dashboard')) {
|
|
|
|
|
if (roles.indexOf(userLesson) >= 0) {
|
|
|
|
|
route.redirect = `${UrlConfig.teach.home}`;
|
|
|
|
|
} else if (roles.indexOf(userExam) >= 0) {
|
|
|
|
|
route.redirect = `${UrlConfig.exam.home}`;
|
|
|
|
|
} else if (roles.indexOf(userSimulation) >= 0) {
|
|
|
|
|
route.redirect = `${UrlConfig.demonstration.home}`;
|
|
|
|
|
} else if (roles.indexOf(userScreen) >= 0) {
|
|
|
|
|
route.redirect = `${UrlConfig.dp.home}`;
|
|
|
|
|
} else if (roles.indexOf(userPlan) >= 0) {
|
|
|
|
|
route.redirect = `${UrlConfig.plan.home}`;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 通过meta.role判断是否与当前用户权限匹配
|
|
|
|
|
* @param roles
|
|
|
|
|
* @param route
|
|
|
|
|
*/
|
|
|
|
|
function hasPermission(roles, route, parentsRoles) {
|
|
|
|
|
setHonePagePath(route, roles);
|
|
|
|
|
if (route.meta && route.meta.roles) {
|
2019-07-25 10:30:30 +08:00
|
|
|
|
// 如果存在本级路由,则使用自己的roles过滤
|
2019-09-25 13:45:20 +08:00
|
|
|
|
// debugger;
|
2019-10-10 13:45:51 +08:00
|
|
|
|
// if (getSessionStorage('design')) {
|
|
|
|
|
// if (route.meta.roles.indexOf(userDesign)===-1) {
|
|
|
|
|
// route.hidden = true;
|
|
|
|
|
// } else {
|
|
|
|
|
// if ((route.meta.roles.indexOf(admin)>=0 && roles.indexOf(admin)>=0)) {
|
|
|
|
|
// route.hidden = false;
|
|
|
|
|
// } else if (route.meta.roles.indexOf(user)>=0 && roles.indexOf(user)>=0) {
|
|
|
|
|
// route.hidden = false;
|
|
|
|
|
// } else {
|
|
|
|
|
// route.hidden = true;
|
|
|
|
|
// }
|
|
|
|
|
// }
|
2019-09-25 13:45:20 +08:00
|
|
|
|
|
2019-10-10 13:45:51 +08:00
|
|
|
|
// } else {
|
|
|
|
|
// if (route.meta.roles.indexOf(userDesign)>0) {
|
|
|
|
|
// route.hidden = true;
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// return roles.some(role => route.meta.roles.indexOf(role) >= 0);
|
2019-10-25 12:59:24 +08:00
|
|
|
|
if (getSessionStorage('project')==='design') {
|
2019-10-10 13:45:51 +08:00
|
|
|
|
roles= roles.filter(function (role) {
|
|
|
|
|
return route.meta.roles.indexOf(role) >= 0;
|
|
|
|
|
});
|
|
|
|
|
const result=roles.every(role => route.meta.roles.indexOf(role) >= 0);
|
|
|
|
|
return result && ((roles.indexOf(admin)>=0 && route.meta.roles.indexOf(admin)>=0) || (roles.indexOf(user)>=0 && route.meta.roles.indexOf(user)>=0 ));
|
2019-09-23 17:47:54 +08:00
|
|
|
|
} else {
|
2019-10-10 13:45:51 +08:00
|
|
|
|
roles= roles.filter(function (role) {
|
|
|
|
|
return route.meta.roles.indexOf(role) >= 0;
|
|
|
|
|
});
|
|
|
|
|
return roles.some(role => route.meta.roles.indexOf(role) >= 0) && route.meta.roles.indexOf(userDesign)<0;
|
2019-09-23 17:47:54 +08:00
|
|
|
|
}
|
2019-07-02 16:29:52 +08:00
|
|
|
|
} else if (parentsRoles) {
|
2019-07-25 10:30:30 +08:00
|
|
|
|
// 如果没有本级路由,有父级路由,则使用父级路由过滤
|
2019-07-02 16:29:52 +08:00
|
|
|
|
return roles.some(role => parentsRoles.indexOf(role) >= 0);
|
|
|
|
|
} else {
|
2019-07-25 10:30:30 +08:00
|
|
|
|
// 如果父级和本级都没有则默认不需要过滤
|
2019-07-02 16:29:52 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 根据roles和系统类型重新设置权限列表
|
|
|
|
|
* @param roles
|
|
|
|
|
* @param type
|
|
|
|
|
*/
|
|
|
|
|
function convertRouterRoles({ roles, permissionType }) {
|
|
|
|
|
if (roles && roles.indexOf(user) >= 0) {
|
|
|
|
|
switch (permissionType) {
|
2019-07-25 10:30:30 +08:00
|
|
|
|
case PermissionType.LESSON: roles.push(userLesson); break;
|
|
|
|
|
case PermissionType.EXAM: roles.push(userExam); break;
|
|
|
|
|
case PermissionType.SIMULATION: roles.push(userSimulation); break;
|
|
|
|
|
case PermissionType.SCREEN: roles.push(userScreen); break;
|
|
|
|
|
case PermissionType.PLAN: roles.push(userPlan); break;
|
2019-07-02 16:29:52 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return { roles };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 递归过滤异步路由表,返回符合用户角色权限的路由表
|
|
|
|
|
* @param asyncRouter
|
|
|
|
|
* @param roles
|
|
|
|
|
*/
|
|
|
|
|
function filterAsyncRouter(asyncRouter, roles, parentsRoles) {
|
|
|
|
|
return asyncRouter.filter(route => {
|
2019-08-12 15:39:29 +08:00
|
|
|
|
if (route.name === '大屏系统' && roles.indexOf(admin)===-1 && roles.indexOf(userScreen) ===-1) {
|
|
|
|
|
route.hidden = true;
|
|
|
|
|
}
|
2019-07-02 16:29:52 +08:00
|
|
|
|
if (hasPermission(roles, route, parentsRoles)) {
|
|
|
|
|
if (route.children && route.children.length) {
|
|
|
|
|
route.children = filterAsyncRouter(route.children, roles, route.meta ? route.meta.roles : undefined);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const permission = {
|
|
|
|
|
state: {
|
|
|
|
|
routes: constantRoutes,
|
2019-07-25 10:30:30 +08:00
|
|
|
|
addRouters: []
|
2019-07-02 16:29:52 +08:00
|
|
|
|
},
|
|
|
|
|
mutations: {
|
|
|
|
|
SET_ROUTERS: (state, routes) => {
|
|
|
|
|
state.addRouters = routes;
|
|
|
|
|
state.routes = constantRoutes.concat(routes);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
actions: {
|
|
|
|
|
// 生成其他权限路由
|
|
|
|
|
GenerateRoutes({ commit }, data) {
|
|
|
|
|
return new Promise(resolve => {
|
|
|
|
|
const { roles } = convertRouterRoles(data);
|
|
|
|
|
let accessedRouters;
|
2019-10-15 11:05:30 +08:00
|
|
|
|
if (roles.indexOf(superAdmin) >= 0 && roles.indexOf(admin) < 0) {
|
|
|
|
|
roles.push(admin);
|
|
|
|
|
}
|
2019-09-23 17:47:54 +08:00
|
|
|
|
// if (roles.indexOf(superAdmin) >= 0) {
|
|
|
|
|
// accessedRouters = asyncRouter;
|
|
|
|
|
// } else {
|
|
|
|
|
// eslint-disable-next-line prefer-const
|
|
|
|
|
accessedRouters = filterAsyncRouter(asyncRouter, roles);
|
|
|
|
|
// }
|
2019-07-02 16:29:52 +08:00
|
|
|
|
commit('SET_ROUTERS', accessedRouters);
|
|
|
|
|
resolve();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default permission;
|