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

125 lines
4.6 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';
2020-07-30 13:43:00 +08:00
import {loginInfo} from '@/scripts/ProjectConfig';
2019-11-14 13:59:33 +08:00
import NProgress from 'nprogress';
import 'nprogress/nprogress.css';
2020-07-10 17:23:17 +08:00
// import { admin } from './router/index_APP_TARGET';
2020-07-09 10:16:33 +08:00
import { getToken, removeToken, getSessionStorage } from '@/utils/auth';
2019-10-23 19:00:35 +08:00
import localStore from 'storejs';
2019-07-02 16:29:52 +08:00
2020-07-10 17:23:17 +08:00
// function hasPermission(roles, permissionRoles) {
// 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-07-09 10:16:33 +08:00
// const designPageRegex = [/^\/design/, /^\/scriptDisplay/, /^\/publish/, /^\/orderauthor/, /^\/system/, /^\/iscs/, /^\/display\/record/, /^\/display\/manage/, /^\/apply/, /^\/plan/, /^\/display\/plan/, /^\/displayNew\/record/, /^\/displayNew\/manage/, /^\/displayNew\/plan/, /^\/practiceDisplayNew/, /^\/bigSplitScreen/];
2019-11-14 16:22:39 +08:00
2020-07-09 10:16:33 +08:00
// function isDesignPage(toRoutePath) {
// return designPageRegex.some(item => item.test(toRoutePath) );
// }
2020-07-30 13:43:00 +08:00
for (const val in loginInfo) {
if (loginInfo[val].loginPath && !whiteList.includes(loginInfo[val].loginPath)) {
whiteList.push(loginInfo[val].loginPath);
2020-06-09 11:33:42 +08:00
}
}
2020-07-09 10:16:33 +08:00
// const loginPage = whiteList[0];
// 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-07-30 13:43:00 +08:00
loginPath = loginInfo[current_session] ? loginInfo[current_session].loginPath : whiteList[0];
2020-07-09 10:16:33 +08:00
if (to.query.projectDevice && to.query.type) {
loginPath = `${loginPath}&projectDevice=${to.query.projectDevice}&type=${to.query.type}`;
}
if (current_session && current_session == 'jsxt' || current_session && current_session == 'refereeJsxt') {
const raceId = getSessionStorage('raceId'); // 登陆时候保存 竞赛id
loginPath = `${loginPath}?raceId=${raceId}`;
2019-10-29 15:06:15 +08:00
}
2019-07-02 16:29:52 +08:00
2020-07-09 10:16:33 +08:00
return loginPath;
2019-07-02 16:29:52 +08:00
}
2020-07-09 10:16:33 +08:00
function handleRoute(to, next, loginPath) {
2020-07-10 17:23:17 +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('验证失败,请重新登录!');
2020-07-09 10:16:33 +08:00
next({ path: loginPath });
2019-10-29 15:06:15 +08:00
});
});
} else {
// 除没有动态改变权限的需求可直接next() 删下方权限判断
2020-07-10 17:23:17 +08:00
// if (hasPermission(store.getters.roles, to.meta.roles)) {
if (to.path === '/404' && to.redirectedFrom === '/') {
const project = getSessionStorage('project');
next(localStore.get('trainingPlatformRoute' + store.getters.id + project) || '/trainingPlatform');
2019-10-29 15:06:15 +08:00
} else {
2020-07-10 17:23:17 +08:00
next();
2019-10-29 15:06:15 +08:00
}
2020-07-10 17:23:17 +08:00
// }
2019-10-29 15:06:15 +08:00
}
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();
2020-07-09 10:16:33 +08:00
const loginPath = getRouteInfo(to);
2019-11-14 13:59:33 +08:00
if (getToken()) {
2020-07-09 10:16:33 +08:00
if (to.path === loginPath) {
2019-10-29 15:06:15 +08:00
// 登录页面不拦截
next();
} else {
// 进入系统重新计算路由
2020-07-09 10:16:33 +08:00
handleRoute(to, next, loginPath);
2019-10-29 15:06:15 +08:00
}
} 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
// 否则全部重定向到登录页
2020-07-09 10:16:33 +08:00
next(loginPath);
2019-10-29 15:06:15 +08:00
}
}
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
});