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

142 lines
5.2 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 进度条
2019-11-13 10:15:40 +08:00
// 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) {
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
}
2019-10-28 17:08:22 +08:00
const whiteList = ['/login', '/design/login', '/xty/login', '/designxty/login']; // 不重定向白名单
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
const loginXtyPage = whiteList[2];
2019-10-28 17:08:22 +08:00
const loginDesignXtyPage = whiteList[3];
2019-07-02 16:29:52 +08:00
// 获取路径数据
function getRouteInfo(to) {
2019-10-29 15:06:15 +08:00
let loginPath = '/';
let getTokenInfo = () => { };
let clientId = '';
const toRoutePath = to.redirectedFrom || to.path;
if (/^\/designxty/.test(toRoutePath)) {
loginPath = loginDesignXtyPage;
getTokenInfo = getDesignToken;
clientId = LoginParams.Design.clientId;
} else 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)) {
loginPath = getSessionStorage('project') === 'designxty' ? loginDesignXtyPage : loginDesignPage;
getTokenInfo = getDesignToken;
clientId = LoginParams.Design.clientId;
} else if (/^\/plan/.test(toRoutePath) || /^\/display\/plan/.test(toRoutePath)) {
if (getSessionStorage('project').startsWith('design')) {
loginPath = getSessionStorage('project') === 'designxty' ? loginDesignXtyPage : loginDesignPage;
getTokenInfo = getDesignToken;
clientId = LoginParams.Design.clientId;
} else {
loginPath = getSessionStorage('project') === 'xty' ? loginXtyPage : loginPage;
getTokenInfo = getToken;
clientId = null;
}
} else if ( /^\/xty/.test(toRoutePath)) {
loginPath = loginXtyPage;
getTokenInfo = getToken;
clientId = null;
} else {
loginPath = getSessionStorage('project') === 'xty' ? loginXtyPage : loginPage;
getTokenInfo = getToken;
clientId = null;
}
2019-07-02 16:29:52 +08:00
2019-10-29 15:06:15 +08:00
return { clientId, loginPath, getTokenInfo };
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) {
// 拉取用户信息
store.dispatch('GetInfo', routeInfo.getTokenInfo).then(res => {
// 根据roles权限生成可访问的路由表
const roles = res.roles;
if (getSessionStorage('project').startsWith('design')) {
roles.push(userDesign);
}
store.dispatch('GenerateRoutes', { roles, clientId: routeInfo.clientId }).then(() => {
// 动态添加可访问路由表
router.addRoutes(store.getters.addRouters);
// router.addRoutes(asyncRouter1);
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(() => {
store.dispatch('FedLogOut', routeInfo.clientId).then(() => {
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) => {
2019-10-29 15:06:15 +08:00
NProgress.start();
2019-11-12 17:27:30 +08:00
2019-10-29 15:06:15 +08:00
const routeInfo = getRouteInfo(to);
if (routeInfo.getTokenInfo()) {
// 已登录
if (to.path === routeInfo.loginPath) {
// 登录页面不拦截
next();
} else {
// 进入系统重新计算路由
handleRoute(to, from, next, routeInfo);
}
} else {
// 未登录情况下
if (whiteList.indexOf(to.path) !== -1) {
// 在免登录白名单,直接进入
next();
} else {
// 否则全部重定向到登录页
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
});