rt-sim-training-client/src/permission.js

133 lines
4.7 KiB
JavaScript
Raw Normal View History

2019-07-02 16:29:52 +08:00
import Vue from 'vue';
2020-04-01 13:47:29 +08:00
import store from '@/store/index_APP_TARGET';
import router from './router/index_APP_TARGET';
import {PermissionParam} from '@/scripts/ProjectConfig';
2019-11-14 13:59:33 +08:00
import NProgress from 'nprogress';
import 'nprogress/nprogress.css';
import { admin} from './router/index_APP_TARGET';
2019-11-25 15:57:11 +08:00
import { getToken, removeToken} from '@/utils/auth';
2020-05-20 13:25:31 +08:00
// import { LoginParams } from '@/utils/login';
2019-09-23 17:47:54 +08:00
import { getSessionStorage } from '@/utils/auth';
2019-10-23 19:00:35 +08:00
import localStore from 'storejs';
2020-05-20 13:25:31 +08:00
import { loginInfo } from '@/scripts/ProjectConfig';
2019-07-02 16:29:52 +08:00
function hasPermission(roles, permissionRoles) {
2019-10-29 15:06:15 +08:00
if (roles.indexOf(admin) >= 0) return true;
if (!permissionRoles) return true;
return roles.some(role => permissionRoles.indexOf(role) >= 0);
2019-07-02 16:29:52 +08:00
}
const whiteList = ['/login', '/design/login', '/gzzbxy/relay']; // 不重定向白名单
2019-07-02 16:29:52 +08:00
2020-03-27 13:52:14 +08:00
const designPageRegex = [/^\/design/, /^\/scriptDisplay/, /^\/publish/, /^\/orderauthor/, /^\/system/, /^\/iscs/, /^\/display\/record/, /^\/display\/manage/, /^\/apply/, /^\/plan/, /^\/display\/plan/, /^\/displayNew\/record/, /^\/displayNew\/manage/, /^\/displayNew\/plan/];
2019-11-14 16:22:39 +08:00
function isDesignPage(toRoutePath) {
2020-05-20 13:25:31 +08:00
return designPageRegex.some(item => item.test(toRoutePath) );
2019-11-14 16:22:39 +08:00
}
for (const val in PermissionParam) {
whiteList.push(PermissionParam[val].whitePage);
}
2019-10-17 14:31:59 +08:00
const loginPage = whiteList[0];
2019-10-24 14:03:19 +08:00
const loginDesignPage = whiteList[1];
2019-07-02 16:29:52 +08:00
// 获取路径数据
function getRouteInfo(to) {
let loginPath = '';
2019-10-29 15:06:15 +08:00
const toRoutePath = to.redirectedFrom || to.path;
2019-11-25 18:58:39 +08:00
const current_session = getSessionStorage('project');
2020-05-20 13:25:31 +08:00
if (whiteList.includes(toRoutePath)) { // 登陆页面清空 token
2020-05-18 17:59:38 +08:00
removeToken();
}
2020-05-19 16:56:36 +08:00
if (isDesignPage(toRoutePath)) {
2020-05-20 13:25:31 +08:00
loginPath = loginDesignPage;
2019-11-25 15:57:11 +08:00
if (current_session && !current_session.startsWith('design')) {
removeToken();
}
2019-10-29 15:06:15 +08:00
} else {
const whitePage = PermissionParam[current_session] ? PermissionParam[current_session].whitePage : '';
loginPath = whitePage || loginPage;
2019-11-25 15:57:11 +08:00
if (current_session && current_session.startsWith('design')) {
removeToken();
}
2019-10-29 15:06:15 +08:00
}
2019-07-02 16:29:52 +08:00
2020-05-20 13:25:31 +08:00
return { loginPath };
2019-07-02 16:29:52 +08:00
}
function handleRoute(to, from, next, routeInfo) {
2019-10-29 15:06:15 +08:00
if (store.getters.roles.length === 0) {
2019-11-14 13:59:33 +08:00
store.dispatch('GetInfo', getToken()).then(res => {
2019-10-29 15:06:15 +08:00
// 根据roles权限生成可访问的路由表
const roles = res.roles;
2020-05-20 13:25:31 +08:00
store.dispatch('GenerateRoutes', { roles }).then(() => {
router.addRoutes(store.getters.addRouters); // 动态添加可访问路由表
2019-10-29 15:06:15 +08:00
if (to.redirectedFrom) {
next({ path: to.redirectedFrom, replace: true });
} else {
next({ ...to, replace: true });
}
});
2019-07-02 16:29:52 +08:00
2019-10-29 15:06:15 +08:00
}).catch(() => {
2019-11-14 13:59:33 +08:00
store.dispatch('FedLogOut').then(() => {
2019-10-29 15:06:15 +08:00
Vue.prototype.$messageBox('验证失败,请重新登录!');
next({ path: routeInfo.loginPath });
});
});
} else {
// 除没有动态改变权限的需求可直接next() 删下方权限判断
if (hasPermission(store.getters.roles, to.meta.roles)) {
if (to.path === '/404' && to.redirectedFrom === '/') {
next(localStore.get('trainingPlatformRoute' + store.getters.id) || '/trainingPlatform');
} else {
next();
}
} else {
next({ path: '/401', replace: true, query: { noGoBack: true } });
}
}
2019-07-02 16:29:52 +08:00
}
router.beforeEach((to, from, next) => {
2020-05-20 13:25:31 +08:00
const project = getSessionStorage('project');
document.title = loginInfo[project || 'login'].browserTitle || loginInfo[project || 'login'].title;
2019-10-29 15:06:15 +08:00
NProgress.start();
const routeInfo = getRouteInfo(to);
2019-11-14 13:59:33 +08:00
if (getToken()) {
2019-10-29 15:06:15 +08:00
if (to.path === routeInfo.loginPath) {
// 登录页面不拦截
next();
} else {
// 进入系统重新计算路由
handleRoute(to, from, next, routeInfo);
}
} else {
if (whiteList.indexOf(to.path) !== -1) {
// 在免登录白名单,直接进入
next();
} else if (to.path.substr(0, 13) == whiteList[2]) {
next();
2020-05-19 16:56:36 +08:00
} else if (to.path == '/jsxtApply') {
next();
2020-04-03 13:33:33 +08:00
} else {
2019-10-29 15:06:15 +08:00
// 否则全部重定向到登录页
next(routeInfo.loginPath);
}
}
2019-07-02 16:29:52 +08:00
});
2019-11-12 17:20:23 +08:00
NProgress.configure({
easing: 'ease', // 动画方式
speed: 500, // 递增进度条的速度
showSpinner: false, // 是否显示加载ico
trickleSpeed: 200, // 自动递增间隔
minimum: 0.3 // 初始化时的最小百分比
});
2019-07-02 16:29:52 +08:00
router.afterEach(() => {
2019-10-29 15:06:15 +08:00
// 结束Progress
NProgress.done();
2019-07-02 16:29:52 +08:00
});