路由权限生成调整逻辑

This commit is contained in:
zyy 2020-05-20 13:25:31 +08:00
parent ce0c5c074c
commit 61c3650621
6 changed files with 226 additions and 331 deletions

View File

@ -6,9 +6,10 @@ import NProgress from 'nprogress';
import 'nprogress/nprogress.css'; import 'nprogress/nprogress.css';
import { admin} from './router/index_APP_TARGET'; import { admin} from './router/index_APP_TARGET';
import { getToken, removeToken} from '@/utils/auth'; import { getToken, removeToken} from '@/utils/auth';
import { LoginParams } from '@/utils/login'; // import { LoginParams } from '@/utils/login';
import { getSessionStorage } from '@/utils/auth'; import { getSessionStorage } from '@/utils/auth';
import localStore from 'storejs'; import localStore from 'storejs';
import { loginInfo } from '@/scripts/ProjectConfig';
function hasPermission(roles, permissionRoles) { function hasPermission(roles, permissionRoles) {
if (roles.indexOf(admin) >= 0) return true; if (roles.indexOf(admin) >= 0) return true;
@ -21,9 +22,7 @@ const whiteList = ['/login', '/design/login', '/gzzbxy/relay']; // 不重定向
const designPageRegex = [/^\/design/, /^\/scriptDisplay/, /^\/publish/, /^\/orderauthor/, /^\/system/, /^\/iscs/, /^\/display\/record/, /^\/display\/manage/, /^\/apply/, /^\/plan/, /^\/display\/plan/, /^\/displayNew\/record/, /^\/displayNew\/manage/, /^\/displayNew\/plan/]; const designPageRegex = [/^\/design/, /^\/scriptDisplay/, /^\/publish/, /^\/orderauthor/, /^\/system/, /^\/iscs/, /^\/display\/record/, /^\/display\/manage/, /^\/apply/, /^\/plan/, /^\/display\/plan/, /^\/displayNew\/record/, /^\/displayNew\/manage/, /^\/displayNew\/plan/];
function isDesignPage(toRoutePath) { function isDesignPage(toRoutePath) {
return designPageRegex.some(item => { return designPageRegex.some(item => item.test(toRoutePath) );
return item.test(toRoutePath);
});
} }
for (const val in PermissionParam) { for (const val in PermissionParam) {
whiteList.push(PermissionParam[val].whitePage); whiteList.push(PermissionParam[val].whitePage);
@ -34,49 +33,35 @@ const loginDesignPage = whiteList[1];
// 获取路径数据 // 获取路径数据
function getRouteInfo(to) { function getRouteInfo(to) {
let loginPath = ''; let loginPath = '';
let clientId = '';
const toRoutePath = to.redirectedFrom || to.path; const toRoutePath = to.redirectedFrom || to.path;
const current_session = getSessionStorage('project'); const current_session = getSessionStorage('project');
for (const val in PermissionParam) { if (whiteList.includes(toRoutePath)) { // 登陆页面清空 token
if (PermissionParam[val].reg.test(toRoutePath)) {
loginPath = PermissionParam[val].whitePage;
clientId = PermissionParam[val].clientId;
break;
}
}
if (whiteList.includes(toRoutePath)) {
removeToken(); removeToken();
} }
if (isDesignPage(toRoutePath)) { if (isDesignPage(toRoutePath)) {
const whitePage = PermissionParam[current_session] ? PermissionParam[current_session].whitePage : ''; loginPath = loginDesignPage;
loginPath = whitePage || loginDesignPage;
clientId = LoginParams.Design.clientId;
if (current_session && !current_session.startsWith('design')) { if (current_session && !current_session.startsWith('design')) {
removeToken(); removeToken();
} }
} else { } else {
const whitePage = PermissionParam[current_session] ? PermissionParam[current_session].whitePage : ''; const whitePage = PermissionParam[current_session] ? PermissionParam[current_session].whitePage : '';
loginPath = whitePage || loginPage; loginPath = whitePage || loginPage;
clientId = null;
if (current_session && current_session.startsWith('design')) { if (current_session && current_session.startsWith('design')) {
removeToken(); removeToken();
} }
} }
return { clientId, loginPath }; return { loginPath };
} }
function handleRoute(to, from, next, routeInfo) { function handleRoute(to, from, next, routeInfo) {
if (store.getters.roles.length === 0) { if (store.getters.roles.length === 0) {
// 拉取用户信息
store.dispatch('GetInfo', getToken()).then(res => { store.dispatch('GetInfo', getToken()).then(res => {
// 根据roles权限生成可访问的路由表 // 根据roles权限生成可访问的路由表
const roles = res.roles; const roles = res.roles;
store.dispatch('GenerateRoutes', { roles, clientId: routeInfo.clientId }).then(() => { store.dispatch('GenerateRoutes', { roles }).then(() => {
// 动态添加可访问路由表 router.addRoutes(store.getters.addRouters); // 动态添加可访问路由表
router.addRoutes(store.getters.addRouters);
// router.addRoutes(asyncRouter1);
if (to.redirectedFrom) { if (to.redirectedFrom) {
next({ path: to.redirectedFrom, replace: true }); next({ path: to.redirectedFrom, replace: true });
} else { } else {
@ -105,10 +90,12 @@ function handleRoute(to, from, next, routeInfo) {
} }
router.beforeEach((to, from, next) => { router.beforeEach((to, from, next) => {
const project = getSessionStorage('project');
document.title = loginInfo[project || 'login'].browserTitle || loginInfo[project || 'login'].title;
NProgress.start(); NProgress.start();
const routeInfo = getRouteInfo(to); const routeInfo = getRouteInfo(to);
if (getToken()) { if (getToken()) {
// 已登录
if (to.path === routeInfo.loginPath) { if (to.path === routeInfo.loginPath) {
// 登录页面不拦截 // 登录页面不拦截
next(); next();
@ -117,8 +104,6 @@ router.beforeEach((to, from, next) => {
handleRoute(to, from, next, routeInfo); handleRoute(to, from, next, routeInfo);
} }
} else { } else {
// 继电器使用
// 未登录情况下
if (whiteList.indexOf(to.path) !== -1) { if (whiteList.indexOf(to.path) !== -1) {
// 在免登录白名单,直接进入 // 在免登录白名单,直接进入
next(); next();

View File

@ -130,7 +130,7 @@ const JsxtApply = () => import('@/views/jsxt/apply/index');
const RefereeList = () => import('@/views/jsxt/refereeList/index'); const RefereeList = () => import('@/views/jsxt/refereeList/index');
const homeJsxt = () => import('@/views/jsxt/home/index'); const homeJsxt = () => import('@/views/jsxt/home/index');
import { loginInfo, GenerateRouteProjectList } from '@/scripts/ProjectConfig'; import { GenerateRouteProjectList } from '@/scripts/ProjectConfig';
import { getSessionStorage } from '@/utils/auth'; import { getSessionStorage } from '@/utils/auth';
/** /**
@ -157,22 +157,17 @@ export const mapCreater = '02'; // 地图创建权限
export const lessonCreater = '03'; // 课程创建权限 export const lessonCreater = '03'; // 课程创建权限
export const admin = '04'; // 管理员 export const admin = '04'; // 管理员
export const superAdmin = '05'; // 超级管理员 export const superAdmin = '05'; // 超级管理员
export const referee = '07'; // 裁判员 // export const referee = '07'; // 裁判员
export const userExam = '011'; // 考试系统 export const projectTrain = '011'; // 城市轨道项目
export const userLesson = '012'; // 教学系统 export const projectXian = '012'; // 西安地铁项目
export const userSimulation = '013'; // 仿真系统 export const projectXty = '013'; // 西铁院
export const userScreen = '014'; // 大屏系统 export const projectGzzb = '014'; // 贵州装备
export const userPlan = '015'; // 计划系统 export const projectJsxt = '015'; // 竞赛系统
export const userTrainingPlatform = '016'; // 实训平台
export const refereePlatform = '017'; export const userTrainingPlatform = '016'; // 实训系统
// const isDev = process.env.NODE_ENV === 'development'; // export const refereePlatform = '017'; // 裁判系统
/**
* constantRoutes
* a base page that does not have permission requirements
* all roles can be accessed
*/
export const constantRoutes = [ export const constantRoutes = [
// 实训平台登录 // 实训平台登录
{ {
@ -249,6 +244,77 @@ GenerateRouteProjectList.forEach(item => {
hidden: true hidden: true
}); });
}); });
// 公共路由不需要过滤 直接可以访问
export const publicAsyncRoute = [
{ // 全屏战场图
path: '/display/:mode',
component: Display,
hidden: true
},
{
path: '/displayNew/:mode',
component: DisplayNew,
hidden: true
},
{
path: '/design/display/:mode',
component: DesignDisplay,
hidden: true
},
{
path: '/design/displayNew/:mode',
component: DesignDisplayNew,
hidden: true
},
{ // 剧本编辑 战场图
path: '/scriptDisplay/:mode',
component: ScriptDisplay,
hidden: true
},
{ // 剧本编辑 战场图
path: '/scriptDisplayNew/:mode',
component: ScriptDisplayNew,
hidden: true
},
{
path: '/displayBigScreen/:mapId',
component: BigScreen,
hidden: true
},
{ // 综合演练室
path: '/trainroom',
component: TrainRoom,
hidden: true
},
{
path: '/jointTraining',
component: JointTraining,
hidden: true
},
{
path: '/jointTrainingNew',
component: JointTrainingNew,
hidden: true
},
{
path: '/jlmap3d',
component: Jlmap3d,
hidden: true
},
{
path: '/displayIscs/system',
component: IscsSystem,
hidden: true,
children: [
{
path: 'config/:mode',
component: IscsConfig,
hidden: true
}
]
}
];
// 城市轨道项目
export const asyncRouter = [ export const asyncRouter = [
{ // 公共地图 { // 公共地图
path: '/design', path: '/design',
@ -363,136 +429,12 @@ export const asyncRouter = [
} }
] ]
}, },
// { // 个人地图
// path: '/design/userlist',
// redirect: '/design/userlist/home',
// component: Layout,
// meta: {
// roles: [admin]
// },
// children: [
// {
// path: '',
// redirect: '/design/userlist/home',
// component: DesignPlatformUser,
// meta: {
// i18n: 'router.designUserPage',
// icon: 'design'
// },
// children: [
// {
// path: 'home',
// component: DesignPlatformHome,
// meta: {
// }
// },
// { // 地图 绘制
// path: 'map/draw/:mapId/:view',
// component: Mapedit,
// hidden: true
// },
// { // 运行图设计
// path: 'runPlan/detail/:mapId',
// component: PlanMonitorDetail,
// hidden: true
// }
// ]
// }
// ]
// },
{ // 全屏战场图
path: '/display/:mode',
component: Display,
meta: {
},
hidden: true
},
{
path: '/displayNew/:mode',
component: DisplayNew,
meta: {
},
hidden: true
},
{
path: '/design/display/:mode',
component: DesignDisplay,
meta: {
},
hidden: true
},
{
path: '/design/displayNew/:mode',
component: DesignDisplayNew,
meta: {
},
hidden: true
},
{ // 剧本编辑 战场图
path: '/scriptDisplay/:mode',
component: ScriptDisplay,
meta: {
},
hidden: true
},
{ // 剧本编辑 战场图
path: '/scriptDisplayNew/:mode',
component: ScriptDisplayNew,
meta: {
},
hidden: true
},
{
path: '/displayIscs/system',
component: IscsSystem,
meta: {
},
hidden: true,
children: [
{
path: 'config/:mode',
component: IscsConfig,
hidden: true
}
]
},
{
path: '/displayBigScreen/:mapId',
component: BigScreen,
meta: {
},
hidden: true
},
{ // 综合演练室
path: '/trainroom',
component: TrainRoom,
meta: {
},
hidden: true
},
{
path: '/jointTraining',
component: JointTraining,
hidden: true
},
{
path: '/jointTrainingNew',
component: JointTrainingNew,
hidden: true
},
{
path: '/jlmap3d',
component: Jlmap3d,
meta: {
},
hidden: true
},
{ // 实训平台 { // 实训平台
path: '/trainingPlatform', path: '/trainingPlatform',
component: Layout, component: Layout,
hidden: true, hidden: true,
meta: { meta: {
roles: [admin, userSimulation, user, userTrainingPlatform] roles: [admin, user, userTrainingPlatform]
}, },
children: [ children: [
{ {
@ -975,6 +917,7 @@ export const asyncRouter = [
] ]
} }
]; ];
export const projectRoute = { export const projectRoute = {
designgzb: [ designgzb: [
{ {
@ -999,6 +942,7 @@ export const projectRoute = {
} }
] ]
}; };
// 竞赛系统
export const JSXT = [ export const JSXT = [
{ {
path: '/jsxt', path: '/jsxt',
@ -1006,7 +950,7 @@ export const JSXT = [
component: Layout, component: Layout,
meta: { meta: {
i18n: 'router.competitionManage', i18n: 'router.competitionManage',
roles: [user, userTrainingPlatform, referee] roles: [user, admin, userTrainingPlatform]
}, },
hidden: getSessionStorage('project') ? !getSessionStorage('project').endsWith('jsxt') && !window.document.location.pathname.includes('jsxt') : !window.document.location.pathname.includes('jsxt'), hidden: getSessionStorage('project') ? !getSessionStorage('project').endsWith('jsxt') && !window.document.location.pathname.includes('jsxt') : !window.document.location.pathname.includes('jsxt'),
children: [ children: [
@ -1058,7 +1002,7 @@ export const JSXT = [
component: Layout, component: Layout,
meta: { meta: {
i18n: 'router.homeJsxt', i18n: 'router.homeJsxt',
roles: [user, referee, refereePlatform] roles: [user, admin]
}, },
hidden: getSessionStorage('project') ? !getSessionStorage('project').endsWith('refereeJsxt') && !window.document.location.pathname.includes('refereeJsxt') : !window.document.location.pathname.includes('refereeJsxt'), hidden: getSessionStorage('project') ? !getSessionStorage('project').endsWith('refereeJsxt') && !window.document.location.pathname.includes('refereeJsxt') : !window.document.location.pathname.includes('refereeJsxt'),
children: [ children: [
@ -1078,7 +1022,7 @@ export const JSXT = [
component: Layout, component: Layout,
meta: { meta: {
i18n: 'router.refereeJManage', i18n: 'router.refereeJManage',
roles: [referee, refereePlatform] roles: [user, admin]
}, },
hidden: getSessionStorage('project') ? !getSessionStorage('project').endsWith('refereeJsxt') && !window.document.location.pathname.includes('refereeJsxt') : !window.document.location.pathname.includes('refereeJsxt'), hidden: getSessionStorage('project') ? !getSessionStorage('project').endsWith('refereeJsxt') && !window.document.location.pathname.includes('refereeJsxt') : !window.document.location.pathname.includes('refereeJsxt'),
children: [ children: [
@ -1102,12 +1046,6 @@ const createRouter = () => new Router({
const router = createRouter(); const router = createRouter();
router.beforeEach((to, from, next) => {
const project = getSessionStorage('project');
document.title = loginInfo[project || 'login'].browserTitle || loginInfo[project || 'login'].title;
next();
});
// 兼容 vue-router在3.1.0版本以上的路由跳转使用的是 promise 的方式 // 兼容 vue-router在3.1.0版本以上的路由跳转使用的是 promise 的方式
const originalPush = Router.prototype.push; const originalPush = Router.prototype.push;
Router.prototype.push = function push(location) { Router.prototype.push = function push(location) {

View File

@ -136,13 +136,14 @@ export const lessonCreater = '03'; // 课程创建权限
export const admin = '04'; // 管理员 export const admin = '04'; // 管理员
export const superAdmin = '05'; // 超级管理员 export const superAdmin = '05'; // 超级管理员
export const userExam = '011'; // 考试系统 export const projectTrain = '011'; // 城市轨道项目
export const userLesson = '012'; // 教学系统 export const projectXian = '012'; // 西安地铁项目
export const userSimulation = '013'; // 仿真系统 export const projectXty = '013'; // 西铁院
export const userScreen = '014'; // 大屏系统 export const projectGzzb = '014'; // 贵州装备
export const userPlan = '015'; // 计划系统 export const projectJsxt = '015'; // 竞赛系统
export const userTrainingPlatform = '016'; // 实训平台
// const isDev = process.env.NODE_ENV === 'development'; export const userTrainingPlatform = '016'; // 实训系统
export const refereePlatform = '017'; // 裁判系统
/** /**
* constantRoutes * constantRoutes
@ -200,7 +201,77 @@ export const constantRoutes = [
}, },
{ path: '*', redirect: '/404', hidden: true } { path: '*', redirect: '/404', hidden: true }
]; ];
// 公共路由不需要过滤 直接可以访问
export const publicAsyncRoute = [
{ // 全屏战场图
path: '/display/:mode',
component: Display,
hidden: true
},
{
path: '/displayNew/:mode',
component: DisplayNew,
hidden: true
},
{
path: '/design/display/:mode',
component: DesignDisplay,
hidden: true
},
{ // 剧本编辑 战场图
path: '/scriptDisplay/:mode',
component: ScriptDisplay,
hidden: true
},
{ // 剧本编辑 战场图
path: '/scriptDisplayNew/:mode',
component: ScriptDisplayNew,
hidden: true
},
{ // 综合演练室
path: '/trainroom',
component: TrainRoom,
hidden: true
},
{
path: '/jointTraining',
component: JointTraining,
hidden: true
},
{
path: '/jointTrainingNew',
component: JointTrainingNew,
hidden: true
},
{
path: '/jlmap3d',
component: Jlmap3d,
hidden: true
},
{ // 运行图编辑
path: '/plan/usertool',
component: PlanMonitorEditUserTool,
hidden: true
},
{ // 运行图编辑
path: '/plan/tool',
component: PlanMonitorEditTool,
hidden: true
},
{
path: '/displayIscs/system',
component: IscsSystem,
hidden: true,
children: [
{
path: 'config/:mode',
component: IscsConfig,
hidden: true
}
]
}
];
export const asyncRouter = [ export const asyncRouter = [
{ // 公共地图 { // 公共地图
path: '/design', path: '/design',
@ -305,85 +376,12 @@ export const asyncRouter = [
} }
] ]
}, },
{ // 全屏战场图
path: '/display/:mode',
component: Display,
meta: {
},
hidden: true
},
{
path: '/displayNew/:mode',
component: DisplayNew,
meta: {
},
hidden: true
},
{
path: '/design/display/:mode',
component: DesignDisplay,
meta: {
},
hidden: true
},
{ // 剧本编辑 战场图
path: '/scriptDisplay/:mode',
component: ScriptDisplay,
meta: {
},
hidden: true
},
{ // 剧本编辑 战场图
path: '/scriptDisplayNew/:mode',
component: ScriptDisplayNew,
meta: {
},
hidden: true
},
{
path: '/displayIscs/system',
component: IscsSystem,
meta: {
},
hidden: true,
children: [
{
path: 'config/:mode',
component: IscsConfig,
hidden: true
}
]
},
{ // 综合演练室
path: '/trainroom',
component: TrainRoom,
meta: {
},
hidden: true
},
{
path: '/jointTraining',
component: JointTraining,
hidden: true
},
{
path: '/jointTrainingNew',
component: JointTrainingNew,
hidden: true
},
{
path: '/jlmap3d',
component: Jlmap3d,
meta: {
},
hidden: true
},
{ // 实训平台 { // 实训平台
path: '/trainingPlatform', path: '/trainingPlatform',
component: Layout, component: Layout,
hidden: true, hidden: true,
meta: { meta: {
roles: [admin, userSimulation, user, userTrainingPlatform] roles: [admin, user, userTrainingPlatform]
}, },
children: [ children: [
{ {
@ -451,20 +449,6 @@ export const asyncRouter = [
} }
] ]
}, },
{ // 运行图编辑
path: '/plan/usertool',
component: PlanMonitorEditUserTool,
meta: {
},
hidden: true
},
{ // 运行图编辑
path: '/plan/tool',
component: PlanMonitorEditTool,
meta: {
},
hidden: true
},
{ // 发布内容管理 { // 发布内容管理
path: '/publish', path: '/publish',
component: Layout, component: Layout,
@ -764,11 +748,11 @@ const createRouter = () => new VueRouter({
const router = createRouter(); const router = createRouter();
router.beforeEach((to, from, next) => { // router.beforeEach((to, from, next) => {
const project = getSessionStorage('project'); // const project = getSessionStorage('project');
document.title = loginInfo[project || 'login'].title; // document.title = loginInfo[project || 'login'].title;
next(); // next();
}); // });
// 兼容 vue-router在3.1.0版本以上的路由跳转使用的是 promise 的方式 // 兼容 vue-router在3.1.0版本以上的路由跳转使用的是 promise 的方式
const originalPush = VueRouter.prototype.push; const originalPush = VueRouter.prototype.push;

View File

@ -104,44 +104,58 @@ export const goOtherPlatformMenu = { // 导航栏快速切换平台
designxadt: '/xadt/login' designxadt: '/xadt/login'
}; };
export const PermissionParam = { // 路径权限处理所需参数配置(跳转白名单&路径正则匹配&clientId export const PermissionParam = { // 路径权限处理所需参数配置(跳转白名单&路径正则匹配&clientId
login: {
systemType: '011'
},
design: {
systemType: '011'
},
designxty: { designxty: {
whitePage: '/designxty/login', whitePage: '/designxty/login',
reg: /^\/designxty/, reg: /^\/designxty/,
systemType: '013',
clientId: LoginParams.Design.clientId clientId: LoginParams.Design.clientId
}, },
designgzb: { designgzb: {
whitePage: '/designgzb/login', whitePage: '/designgzb/login',
reg: /^\/designgzb/, reg: /^\/designgzb/,
systemType: '014',
clientId: LoginParams.Design.clientId clientId: LoginParams.Design.clientId
}, },
designxadt: { designxadt: {
whitePage: '/designxadt/login', whitePage: '/designxadt/login',
reg: /^\/designxadt/, reg: /^\/designxadt/,
systemType: '012',
clientId: LoginParams.Design.clientId clientId: LoginParams.Design.clientId
}, },
xty: { xty: {
whitePage: '/xty/login', whitePage: '/xty/login',
reg: /^\/xty/, reg: /^\/xty/,
systemType: '013',
clientId: null clientId: null
}, },
gzb: { gzb: {
whitePage: '/gzb/login', whitePage: '/gzb/login',
reg: /^\/gzb/, reg: /^\/gzb/,
systemType: '014',
clientId: null clientId: null
}, },
xadt: { xadt: {
whitePage: '/xadt/login', whitePage: '/xadt/login',
reg: /^\/xadt/, reg: /^\/xadt/,
systemType: '012',
clientId: null clientId: null
}, },
jsxt: { jsxt: {
whitePage: '/jsxt/login', whitePage: '/jsxt/login',
reg: /^\/jsxt/, reg: /^\/jsxt/,
systemType: '015',
clientId: null clientId: null
}, },
refereeJsxt: { refereeJsxt: {
whitePage: '/refereeJsxt/login', whitePage: '/refereeJsxt/login',
reg: /^\/refereeJsxt/, reg: /^\/refereeJsxt/,
systemType: '015',
clientId: LoginParams.Design.clientId clientId: LoginParams.Design.clientId
} }
}; };

View File

@ -1,46 +1,23 @@
import { asyncRouter, constantRoutes, user, userLesson, userExam, userSimulation, userScreen, userPlan, superAdmin, admin, userTrainingPlatform, JSXT, refereePlatform, projectRoute } from '@/router/index_APP_TARGET'; import { publicAsyncRoute, asyncRouter, constantRoutes, user, projectTrain, projectXian, projectXty, projectGzzb, projectJsxt, superAdmin, admin, userTrainingPlatform, JSXT, projectRoute } from '@/router/index_APP_TARGET';
import { PermissionType } from '@/utils/PermissionType'; import { PermissionParam } from '@/scripts/ProjectConfig';
import { UrlConfig } from '@/scripts/ConstDic';
import { getSessionStorage } from '@/utils/auth'; import { getSessionStorage } from '@/utils/auth';
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判断是否与当前用户权限匹配 * 通过meta.role判断是否与当前用户权限匹配及子系统生成
* @param roles * @param roles
* @param route * @param route
*/ */
function hasPermission(roles, route, parentsRoles) { function hasPermission(roles, route, parentsRoles) {
setHonePagePath(route, roles);
if (!getSessionStorage('project')) { if (!getSessionStorage('project')) {
return false; return false;
} }
if (route.meta && route.meta.roles) { if (route.meta && route.meta.roles) {
// 如果存在本级路由则使用自己的roles过滤 // 如果存在本级路由则使用自己的roles过滤
if (getSessionStorage('project').startsWith('design')) { if (getSessionStorage('project').startsWith('design') || getSessionStorage('project').startsWith('refereeJsxt')) {
const result = roles.filter(role => route.meta.roles.indexOf(role) >= 0) || []; const result = roles.filter(role => route.meta.roles.indexOf(role) >= 0) || [];
return result.length && route.meta.roles.indexOf(userTrainingPlatform) < 0; return result.length && route.meta.roles.indexOf(userTrainingPlatform) < 0;
} else if (getSessionStorage('project').startsWith('refereeJsxt')) {
const result = roles.filter(role => route.meta.roles.indexOf(role) >= 0) || [];
return result.length && route.meta.roles.indexOf(refereePlatform) >= 0;
} else { } else {
roles = roles.filter(function (role) { roles = roles.filter((role) => route.meta.roles.indexOf(role) >= 0);
return route.meta.roles.indexOf(role) >= 0;
});
return roles.some(role => route.meta.roles.indexOf(role) >= 0) && route.meta.roles.indexOf(userTrainingPlatform) >= 0; return roles.some(role => route.meta.roles.indexOf(role) >= 0) && route.meta.roles.indexOf(userTrainingPlatform) >= 0;
} }
} else if (parentsRoles) { } else if (parentsRoles) {
@ -53,22 +30,24 @@ function hasPermission(roles, route, parentsRoles) {
} }
/** /**
* 根据roles和系统类型重新设置权限列表 * 根据项目重置 路由
* @param roles * @param systemType 项目类型
* @param type
*/ */
function convertRouterRoles({ roles, permissionType }) { function resetAsyncRouter({ systemType }) {
if (roles && roles.indexOf(user) >= 0) { console.log(systemType);
switch (permissionType) { let list = publicAsyncRoute;
case PermissionType.LESSON: roles.push(userLesson); break; if (systemType == projectTrain) {
case PermissionType.EXAM: roles.push(userExam); break; list = [...list, ...asyncRouter];
case PermissionType.SIMULATION: roles.push(userSimulation); break; } else if (systemType == projectXian) {
case PermissionType.SCREEN: roles.push(userScreen); break; list = [...list, ...asyncRouter];
case PermissionType.PLAN: roles.push(userPlan); break; } else if (systemType == projectXty) {
list = [...list, ...asyncRouter];
} else if (systemType == projectGzzb) {
list = [...list, ...asyncRouter, ...projectRoute.designgzb];
} else if (systemType == projectJsxt) {
list = [...list, ...JSXT];
} }
} return list;
return { roles };
} }
/** /**
@ -78,9 +57,6 @@ function convertRouterRoles({ roles, permissionType }) {
*/ */
function filterAsyncRouter(asyncRouter, roles, parentsRoles) { function filterAsyncRouter(asyncRouter, roles, parentsRoles) {
return asyncRouter.filter(route => { return asyncRouter.filter(route => {
if (route.name === '大屏系统' && roles.indexOf(admin) == -1 && roles.indexOf(userScreen) == -1) {
route.hidden = true;
}
if (hasPermission(roles, route, parentsRoles)) { if (hasPermission(roles, route, parentsRoles)) {
if (route.children && route.children.length) { if (route.children && route.children.length) {
route.children = filterAsyncRouter(route.children, roles, route.meta ? route.meta.roles : undefined); route.children = filterAsyncRouter(route.children, roles, route.meta ? route.meta.roles : undefined);
@ -106,18 +82,16 @@ const permission = {
// 生成其他权限路由 // 生成其他权限路由
GenerateRoutes({ commit }, data) { GenerateRoutes({ commit }, data) {
return new Promise(resolve => { return new Promise(resolve => {
const { roles } = convertRouterRoles(data); const { roles } = data;
if (roles.indexOf(superAdmin) >= 0 && roles.indexOf(admin) < 0) { if (roles.includes(superAdmin) && !roles.includes(admin)) { // 只拥有超级管理员 没有管理员权限
roles.push(admin); roles.push(admin);
} }
const proRoute = projectRoute[getSessionStorage('project')]; if (roles.includes(admin) && !roles.includes(user)) { // 只拥有管理员 不拥有普通用户权限 则增加用户权限
let routeArr = []; roles.push(user);
if (proRoute && proRoute.constructor === Array) {
routeArr = [...asyncRouter, ...JSXT, ...proRoute];
} else {
routeArr = [...asyncRouter, ...JSXT];
} }
const accessedRouters = filterAsyncRouter(routeArr, roles); const routeList = resetAsyncRouter(PermissionParam[getSessionStorage('project')]);
const accessedRouters = filterAsyncRouter(routeList, roles);
accessedRouters.forEach(route => { accessedRouters.forEach(route => {
if (route.children && route.children.length == 0) { if (route.children && route.children.length == 0) {
route.hidden = true; route.hidden = true;

View File

@ -2,10 +2,10 @@ export function getBaseUrl() {
let BASE_API; let BASE_API;
if (process.env.NODE_ENV === 'development') { if (process.env.NODE_ENV === 'development') {
// BASE_API = 'https://joylink.club/jlcloud'; // BASE_API = 'https://joylink.club/jlcloud';
// BASE_API = 'https://test.joylink.club/jlcloud'; BASE_API = 'https://test.joylink.club/jlcloud';
// BASE_API = 'http://192.168.3.5:9000'; // 袁琪 // BASE_API = 'http://192.168.3.5:9000'; // 袁琪
// BASE_API = 'http://192.168.3.6:9000'; // 旭强 // BASE_API = 'http://192.168.3.6:9000'; // 旭强
BASE_API = 'http://192.168.3.41:9000'; // 张赛 // BASE_API = 'http://192.168.3.41:9000'; // 张赛
// BASE_API = 'http://192.168.3.82:9000'; // 杜康 // BASE_API = 'http://192.168.3.82:9000'; // 杜康
// BASE_API = 'http://192.168.3.41:9000'; // 张S赛 // BASE_API = 'http://192.168.3.41:9000'; // 张S赛
// BASE_API = 'http://b29z135112.zicp.vip'; // BASE_API = 'http://b29z135112.zicp.vip';