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 进度条样式
|
|
|
|
import { admin } from './router';
|
|
|
|
import { getToken, getScreenToken, getPlanToken } from '@/utils/auth'; // 验权
|
|
|
|
import { LoginParams } from '@/utils/login';
|
|
|
|
|
|
|
|
function hasPermission(roles, permissionRoles) {
|
|
|
|
if (roles.indexOf(admin) >= 0) return true;
|
|
|
|
if (!permissionRoles) return true;
|
|
|
|
return roles.some(role => permissionRoles.indexOf(role) >= 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
const isDev = process.env.NODE_ENV === 'development';
|
|
|
|
|
|
|
|
const whiteList = ['/login', '/login1', '/dp/login', '/dp/login1', '/plan/login', '/plan/login1']; // 不重定向白名单
|
|
|
|
|
|
|
|
const loginPage = isDev ? whiteList[1] : whiteList[0];
|
|
|
|
|
|
|
|
const loginScreenPage = isDev ? whiteList[3] : whiteList[2];
|
|
|
|
|
|
|
|
const loginPlanPage = isDev ? whiteList[5] : whiteList[4];
|
|
|
|
|
|
|
|
// 获取路径数据
|
|
|
|
function getRouteInfo(to) {
|
|
|
|
let loginPath = '/';
|
|
|
|
let getTokenInfo = () => { };
|
|
|
|
let clientId = '';
|
2019-07-04 10:59:40 +08:00
|
|
|
const toRoutePath = to.redirectedFrom || to.path;
|
2019-07-02 16:29:52 +08:00
|
|
|
|
|
|
|
if (/^\/dp/.test(toRoutePath) || /^\/display\/dp/.test(toRoutePath)) {
|
2019-07-04 10:59:40 +08:00
|
|
|
loginPath = loginScreenPage;
|
2019-07-02 16:29:52 +08:00
|
|
|
getTokenInfo = getScreenToken;
|
|
|
|
clientId = LoginParams.DaPing.clientId;
|
|
|
|
} else if (/^\/plan/.test(toRoutePath) || /^\/display\/plan/.test(toRoutePath) || /^\/planEdit/.test(toRoutePath)) {
|
2019-07-04 10:59:40 +08:00
|
|
|
loginPath = loginPlanPage;
|
2019-07-02 16:29:52 +08:00
|
|
|
getTokenInfo = getPlanToken;
|
|
|
|
clientId = LoginParams.LianJiHua.clientId;
|
|
|
|
} 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;
|
|
|
|
|
|
|
|
store.dispatch('GenerateRoutes', { roles, clientId: routeInfo.clientId }).then(() => {
|
|
|
|
// 动态添加可访问路由表
|
|
|
|
router.addRoutes(store.getters.addRouters);
|
|
|
|
if (to.redirectedFrom) {
|
|
|
|
next({ path: to.redirectedFrom, replace: true });
|
|
|
|
} else {
|
|
|
|
next({ ...to, replace: true });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}).catch(() => {
|
|
|
|
store.dispatch('FedLogOut', routeInfo.clientId).then(() => {
|
2019-07-26 13:32:43 +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)) {
|
|
|
|
next();
|
|
|
|
} 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 {
|
|
|
|
// 未登录情况下
|
|
|
|
if (whiteList.indexOf(to.path) !== -1) {
|
|
|
|
// 在免登录白名单,直接进入
|
|
|
|
next();
|
|
|
|
} else {
|
|
|
|
// 否则全部重定向到登录页
|
|
|
|
next(routeInfo.loginPath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
router.afterEach(() => {
|
|
|
|
// 结束Progress
|
|
|
|
NProgress.done();
|
|
|
|
});
|