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

134 lines
5.3 KiB
JavaScript

import Vue from 'vue';
import store from '@/store/index';
import router from './router/index';
import NProgress from 'nprogress';
import 'nprogress/nprogress.css';
import { setToken, getToken, getSessionStorage, setSessionStorage } from '@/utils/auth';
import { getFrontProjectConfigByLogin} from '@/api/projectConfig';
import localStore from 'storejs';
import { handlerUrl } from '@/utils/baseUrl';
const whiteList = ['/login', '/design/login', '/loginNew', '/gzzbxy/relay', '/authorization', '/AUSline', '/AUStool', '/demo', '/thirdLogin']; // 不重定向白名单
// 登录路径判断获取
function getRouteInfo(to) {
let loginPath = '';
const markKey = store.state.projectConfig.markKey || getSessionStorage('project');
loginPath = !markKey || markKey == 'login' ? '/login' : '/login?project=' + markKey;
if (to.query.projectDevice && to.query.type && loginPath.includes('?')) {
loginPath = `${loginPath}&projectDevice=${to.query.projectDevice}&type=${to.query.type}`;
} else if (to.query.projectDevice && to.query.type) {
loginPath = `${loginPath}?projectDevice=${to.query.projectDevice}&type=${to.query.type}`;
}
return loginPath;
}
function getInfoAndGenerateRoutes(to, next, loginPath) {
store.dispatch('GetInfo', getToken()).then(res => {
// 根据roles权限生成可访问的路由表
// const roles = res.roles;
store.dispatch('GenerateRoutes', res).then(() => {
router.addRoutes(store.getters.addRouters); // 动态添加可访问路由表
if (to.redirectedFrom) {
next({ path: to.redirectedFrom, replace: true });
} else {
next({ ...to, replace: true });
}
});
}).catch(() => {
store.dispatch('FedLogOut').then(() => {
Vue.prototype.$messageBox('验证失败,请重新登录!');
next({ path: loginPath });
});
});
}
function handleRoute(to, next, loginPath) {
const project = getSessionStorage('project');
if (store.getters.roles.length == 0) {
if (!store.state.projectConfig.markKey) {
getFrontProjectConfigByLogin(project).then(resp => {
const urlData = handlerUrl();
const { baseApiUrl, baseSite, ossUrl, loginProLogo, browserTitle} = resp.data.viewSetting;
store.dispatch('setBaseUrl', baseApiUrl || urlData.BASE_API);
store.dispatch('setBaseSite', baseSite || urlData.BASE_SITE);
store.dispatch('setOssUrl', ossUrl || urlData.OSS_URL);
store.dispatch('projectConfig/setMarkKey', resp.data.markKey);
store.dispatch('projectConfig/setProjectCode', resp.data.project);
store.dispatch('projectConfig/setViewSetting', resp.data.viewSetting);
document.title = browserTitle;
if (loginProLogo) {
document.querySelector("link[rel*='icon']").href = (ossUrl || urlData.OSS_URL) + '/logo/' + loginProLogo;
}
getInfoAndGenerateRoutes(to, next, loginPath);
}).catch(e => this.$message.error('获取配置数据失败!'));
} else {
getInfoAndGenerateRoutes(to, next, loginPath);
}
} else {
// 除没有动态改变权限的需求可直接next() 删下方权限判断
if (to.path === '/404' && to.redirectedFrom === '/') {
next(localStore.get('trainingPlatformRoute' + store.getters.id + project) || '/trainingPlatform');
} else {
next();
}
}
}
// function subscribe() {
// const token = getToken();
// const path = window.location.pathname;
// if (token && path != '/404' && !path.endsWith('login')) {
// const header = { group: '', 'X-Token': token };
// store.dispatch('subscribe', {header, type: getSessionStorage('project')});
// }
// }
router.beforeEach( async (to, from, next) => {
// 判断第三方登陆
if (to.query.thirdJump) {
setToken(to.query.key);
setSessionStorage('project', to.query.project);
}
NProgress.start();
const loginPath = getRouteInfo(to);
const token = to.query.token;
if (/demoTraining/.test(to.path) && token) {
const header = { group: to.query.group, 'X-Token': token };
setToken(token);
store.commit('SET_TOKEN', token);
store.commit('SUBSCRIBE', { header, type: '' });
next();
} else if (getToken()) {
if (to.path === loginPath || to.path === '/authorization') {
// 登录页面不拦截
next();
} else {
// 进入系统重新计算路由
handleRoute(to, next, loginPath);
}
} else {
if (whiteList.indexOf(to.path) !== -1) {
// 在免登录白名单,直接进入
next();
} else if (to.path.substr(0, 13) == whiteList[2]) {
next();
} else {
// 否则全部重定向到登录页
next(loginPath);
}
}
});
NProgress.configure({
easing: 'ease', // 动画方式
speed: 500, // 递增进度条的速度
showSpinner: false, // 是否显示加载ico
trickleSpeed: 200, // 自动递增间隔
minimum: 0.3 // 初始化时的最小百分比
});
router.afterEach(() => {
// 结束Progress
NProgress.done();
});