rt-sim-training-client/src/permission.js
2022-08-02 18:01:42 +08:00

160 lines
6.1 KiB
JavaScript

import Vue from 'vue';
import store from '@/store/index';
import router from './router/index';
import {loginInfo, ProjectCode, localPackageProject} from '@/scripts/ProjectConfig';
import NProgress from 'nprogress';
import 'nprogress/nprogress.css';
import { setToken, getToken, removeToken, getSessionStorage, setSessionStorage } from '@/utils/auth';
import { getBaseUrlByProject } from '@/api/projectServer';
import localStore from 'storejs';
import { handlerUrl } from '@/utils/baseUrl';
const whiteList = ['/login', '/design/login', '/gzzbxy/relay', '/authorization', '/AUSline', '/AUStool', '/demo', '/thirdLogin']; // 不重定向白名单
const loginList = ['/login', '/design/login']; // 登陆页面
for (const val in loginInfo) {
if (loginInfo[val].loginPath && !whiteList.includes(loginInfo[val].loginPath)) {
whiteList.push(loginInfo[val].loginPath);
}
}
// 登录路径判断获取
function getRouteInfo(to) {
let loginPath = '';
const toRoutePath = to.redirectedFrom || to.path;
const current_session = getSessionStorage('project');
if (loginList.includes(toRoutePath)) { // 登陆页面清空 token
removeToken();
}
const localLogin = process.env.VUE_APP_PRO == 'local' || process.env.VUE_APP_PRO == 'ntyl';
loginPath = loginInfo[current_session] && !localLogin ? loginInfo[current_session].loginPath : whiteList[0];
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 handleRoute(to, next, loginPath) {
if (store.getters.roles.length == 0) {
store.dispatch('GetInfo', getToken()).then(res => {
// 根据roles权限生成可访问的路由表
const roles = res.roles;
store.dispatch('GenerateRoutes', { roles }).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 });
});
});
} else {
// 除没有动态改变权限的需求可直接next() 删下方权限判断
if (to.path === '/404' && to.redirectedFrom === '/') {
const project = getSessionStorage('project');
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);
}
let project = '';
if (to.path.includes('login')) {
project = to.query.project;
const split = to.path.split('/')[1];
if (process.env.VUE_APP_PRO && process.env.VUE_APP_PRO !== 'local-test') {
project = localPackageProject[process.env.VUE_APP_PRO + split];
} else if (split == 'design') {
project = project ? split + project : split;
} else if (split == 'login') {
project = project || 'login';
} else {
project = split;
}
} else if (to.path.includes('thirdLogin')) {
project = to.query.project;
} else {
project = getSessionStorage('project');
}
if (!store.state.user.baseUrl && project && loginInfo[project]) {
try {
const resp = await getBaseUrlByProject(ProjectCode[project]);
const urlData = handlerUrl(resp.data);
store.dispatch('setBaseUrl', urlData.BASE_API);
store.dispatch('setResourcesUrl', urlData.VOICE_API);
store.dispatch('setUploadUrl', urlData.UPLOAD_API);
store.dispatch('setBaseSite', urlData.BASE_SITE);
subscribe();
} catch (e) {
const urlData = handlerUrl();
store.dispatch('setBaseUrl', urlData.BASE_API);
console.error(e);
}
}
document.title = loginInfo[project || 'login'] ? loginInfo[project || 'login'].browserTitle || loginInfo[project || 'login'].title : '';
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) {
// 登录页面不拦截
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();
});