2019-11-14 16:31:31 +08:00
|
|
|
|
import { asyncRouter, constantRoutes, user, userLesson, userExam, userSimulation, userScreen, userPlan, superAdmin, admin, userTrainingPlatform } 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) {
|
2019-11-14 15:54:58 +08:00
|
|
|
|
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}`;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-07-02 16:29:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 通过meta.role判断是否与当前用户权限匹配
|
|
|
|
|
* @param roles
|
|
|
|
|
* @param route
|
|
|
|
|
*/
|
|
|
|
|
function hasPermission(roles, route, parentsRoles) {
|
2019-11-14 15:54:58 +08:00
|
|
|
|
setHonePagePath(route, roles);
|
|
|
|
|
if (route.meta && route.meta.roles) {
|
|
|
|
|
// 如果存在本级路由,则使用自己的roles过滤
|
2019-09-25 13:45:20 +08:00
|
|
|
|
|
2019-11-14 15:54:58 +08:00
|
|
|
|
if (getSessionStorage('project').startsWith('design')) {
|
|
|
|
|
const result = roles.filter(role => route.meta.roles.indexOf(role) >= 0) || [];
|
2019-11-14 17:10:04 +08:00
|
|
|
|
return result.length && route.meta.roles.indexOf(userTrainingPlatform) < 0;
|
2019-11-14 15:54:58 +08:00
|
|
|
|
} else {
|
|
|
|
|
roles = roles.filter(function (role) {
|
|
|
|
|
return route.meta.roles.indexOf(role) >= 0;
|
|
|
|
|
});
|
2019-11-14 16:31:31 +08:00
|
|
|
|
return roles.some(role => route.meta.roles.indexOf(role) >= 0) && route.meta.roles.indexOf(userTrainingPlatform) > 0;
|
2019-11-14 15:54:58 +08:00
|
|
|
|
}
|
|
|
|
|
} else if (parentsRoles) {
|
|
|
|
|
// 如果没有本级路由,有父级路由,则使用父级路由过滤
|
|
|
|
|
return roles.some(role => parentsRoles.indexOf(role) >= 0);
|
|
|
|
|
} else {
|
|
|
|
|
// 如果父级和本级都没有则默认不需要过滤
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2019-07-02 16:29:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 根据roles和系统类型重新设置权限列表
|
|
|
|
|
* @param roles
|
|
|
|
|
* @param type
|
|
|
|
|
*/
|
|
|
|
|
function convertRouterRoles({ roles, permissionType }) {
|
2019-11-14 15:54:58 +08:00
|
|
|
|
if (roles && roles.indexOf(user) >= 0) {
|
|
|
|
|
switch (permissionType) {
|
|
|
|
|
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
|
|
|
|
|
2019-11-14 15:54:58 +08:00
|
|
|
|
return { roles };
|
2019-07-02 16:29:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 递归过滤异步路由表,返回符合用户角色权限的路由表
|
|
|
|
|
* @param asyncRouter
|
|
|
|
|
* @param roles
|
|
|
|
|
*/
|
|
|
|
|
function filterAsyncRouter(asyncRouter, roles, parentsRoles) {
|
2019-11-14 15:54:58 +08:00
|
|
|
|
return asyncRouter.filter(route => {
|
2019-11-14 13:59:33 +08:00
|
|
|
|
if (route.name === '大屏系统' && roles.indexOf(admin) == -1 && roles.indexOf(userScreen) == -1) {
|
2019-11-14 15:54:58 +08:00
|
|
|
|
route.hidden = true;
|
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
});
|
2019-07-02 16:29:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const permission = {
|
2019-11-14 15:54:58 +08:00
|
|
|
|
state: {
|
|
|
|
|
routes: constantRoutes,
|
|
|
|
|
addRouters: []
|
|
|
|
|
},
|
|
|
|
|
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);
|
|
|
|
|
if (roles.indexOf(superAdmin) >= 0 && roles.indexOf(admin) < 0) {
|
|
|
|
|
roles.push(admin);
|
|
|
|
|
}
|
2019-11-14 13:59:33 +08:00
|
|
|
|
|
|
|
|
|
const accessedRouters = filterAsyncRouter(asyncRouter, roles);
|
2019-11-14 15:54:58 +08:00
|
|
|
|
commit('SET_ROUTERS', accessedRouters);
|
|
|
|
|
resolve();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-07-02 16:29:52 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default permission;
|