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

125 lines
3.8 KiB
JavaScript
Raw Normal View History

2019-07-02 16:29:52 +08:00
import Vue from 'vue';
import store from '@/store';
import router from './router';
import NProgress from 'nprogress'; // Progress 进度条
import 'nprogress/nprogress.css';// Progress 进度条样式
2019-09-23 17:47:54 +08:00
import { admin, userDesign} from './router';
2019-10-24 14:03:19 +08:00
import { getToken, getDesignToken} from '@/utils/auth'; // 验权
2019-07-02 16:29:52 +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';
2019-07-02 16:29:52 +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-10-24 14:03:19 +08:00
const whiteList = ['/login', '/design/login', '/login/xty']; // 不重定向白名单
2019-07-02 16:29:52 +08:00
2019-10-17 14:31:59 +08:00
const loginPage = whiteList[0];
2019-07-02 16:29:52 +08:00
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 = '/';
let getTokenInfo = () => { };
let clientId = '';
2019-07-04 10:59:40 +08:00
const toRoutePath = to.redirectedFrom || to.path;
2019-10-24 14:03:19 +08:00
if (/^\/design/.test(toRoutePath) || /^\/scriptDisplay/.test(toRoutePath) || /^\/publish/.test(toRoutePath) || /^\/orderauthor/.test(toRoutePath) || /^\/system/.test(toRoutePath)|| /^\/display\/record/.test(toRoutePath) || /^\/display\/manage/.test(toRoutePath) || /^\/apply/.test(toRoutePath)) {
2019-09-24 10:51:41 +08:00
loginPath = loginDesignPage;
2019-09-30 17:31:40 +08:00
getTokenInfo = getDesignToken;
2019-09-24 10:51:41 +08:00
clientId = LoginParams.Design.clientId;
2019-10-17 14:35:10 +08:00
} else if (/^\/plan/.test(toRoutePath) || /^\/display\/plan/.test(toRoutePath)) {
2019-10-25 12:59:24 +08:00
if (getSessionStorage('project')==='design') {
2019-10-16 16:58:40 +08:00
loginPath = loginDesignPage;
getTokenInfo = getDesignToken;
clientId = LoginParams.Design.clientId;
} else {
loginPath = loginPage;
getTokenInfo = getToken;
clientId = null;
}
2019-07-02 16:29:52 +08:00
} else {
loginPath = loginPage;
getTokenInfo = getToken;
clientId = null;
}
2019-07-04 10:59:40 +08:00
return { clientId, loginPath, getTokenInfo };
2019-07-02 16:29:52 +08:00
}
function handleRoute(to, from, next, routeInfo) {
if (store.getters.roles.length === 0) {
// 拉取用户信息
store.dispatch('GetInfo', routeInfo.getTokenInfo).then(res => {
// 根据roles权限生成可访问的路由表
const roles = res.roles;
2019-10-25 12:59:24 +08:00
if (getSessionStorage('project')==='design') {
2019-09-23 17:47:54 +08:00
roles.push(userDesign);
}
2019-07-02 16:29:52 +08:00
store.dispatch('GenerateRoutes', { roles, clientId: routeInfo.clientId }).then(() => {
// 动态添加可访问路由表
router.addRoutes(store.getters.addRouters);
2019-09-23 17:47:54 +08:00
// router.addRoutes(asyncRouter1);
2019-07-02 16:29:52 +08:00
if (to.redirectedFrom) {
next({ path: to.redirectedFrom, replace: true });
} else {
next({ ...to, replace: true });
}
});
}).catch(() => {
store.dispatch('FedLogOut', routeInfo.clientId).then(() => {
2019-10-17 16:04:07 +08:00
Vue.prototype.$messageBox('验证失败,请重新登录!');
2019-07-02 16:29:52 +08:00
next({ path: routeInfo.loginPath });
});
});
} else {
2019-07-04 10:59:40 +08:00
// 除没有动态改变权限的需求可直接next() 删下方权限判断
2019-07-02 16:29:52 +08:00
if (hasPermission(store.getters.roles, to.meta.roles)) {
2019-10-23 19:00:35 +08:00
if (to.path==='/404' && to.redirectedFrom==='/') {
2019-10-25 12:59:24 +08:00
if (getSessionStorage('project') === 'design') {
2019-10-23 19:00:35 +08:00
next('/design/home');
} else {
next(localStore.get('trainingPlatformRoute'+store.getters.id) ||'/trainingPlatform');
}
} else {
next();
}
2019-07-02 16:29:52 +08:00
} else {
next({ path: '/401', replace: true, query: { noGoBack: true } });
}
}
}
router.beforeEach((to, from, next) => {
NProgress.start();
2019-07-04 10:59:40 +08:00
const routeInfo = getRouteInfo(to);
2019-07-02 16:29:52 +08:00
if (routeInfo.getTokenInfo()) {
// 已登录
if (to.path === routeInfo.loginPath) {
// 登录页面不拦截
next();
} else {
// 进入系统重新计算路由
handleRoute(to, from, next, routeInfo);
}
} else {
// 未登录情况下
2019-10-17 16:04:07 +08:00
if (whiteList.indexOf(to.path) !== -1) {
2019-07-02 16:29:52 +08:00
// 在免登录白名单,直接进入
next();
} else {
// 否则全部重定向到登录页
next(routeInfo.loginPath);
}
}
});
router.afterEach(() => {
// 结束Progress
NProgress.done();
});