import Vue from 'vue'; import store from '@/store/index_APP_TARGET'; import router from './router/index_APP_TARGET'; import {PermissionParam} from '@/scripts/ProjectConfig'; import NProgress from 'nprogress'; import 'nprogress/nprogress.css'; // import { admin } from './router/index_APP_TARGET'; import { getToken, removeToken, getSessionStorage } from '@/utils/auth'; import localStore from 'storejs'; import { loginInfo } from '@/scripts/ProjectConfig'; // function hasPermission(roles, permissionRoles) { // if (roles.indexOf(admin) >= 0) return true; // if (!permissionRoles) return true; // return roles.some(role => permissionRoles.indexOf(role) >= 0); // } const whiteList = ['/login', '/design/login', '/gzzbxy/relay']; // 不重定向白名单 // const designPageRegex = [/^\/design/, /^\/scriptDisplay/, /^\/publish/, /^\/orderauthor/, /^\/system/, /^\/iscs/, /^\/display\/record/, /^\/display\/manage/, /^\/apply/, /^\/plan/, /^\/display\/plan/, /^\/displayNew\/record/, /^\/displayNew\/manage/, /^\/displayNew\/plan/, /^\/practiceDisplayNew/, /^\/bigSplitScreen/]; // function isDesignPage(toRoutePath) { // return designPageRegex.some(item => item.test(toRoutePath) ); // } for (const val in PermissionParam) { if (PermissionParam[val].whitePage && !whiteList.includes(PermissionParam[val].whitePage)) { whiteList.push(PermissionParam[val].whitePage); } } // const loginPage = whiteList[0]; // const loginDesignPage = whiteList[1]; // 获取路径数据 function getRouteInfo(to) { let loginPath = ''; const toRoutePath = to.redirectedFrom || to.path; const current_session = getSessionStorage('project'); if (whiteList.includes(toRoutePath)) { // 登陆页面清空 token removeToken(); } // if (isDesignPage(toRoutePath)) { // const whitePage = PermissionParam[current_session] ? PermissionParam[current_session].whitePage : ''; // loginPath = whitePage || loginDesignPage; // } else { // const whitePage = PermissionParam[current_session] ? PermissionParam[current_session].whitePage : ''; // loginPath = whitePage || loginPage; // } loginPath = PermissionParam[current_session] ? PermissionParam[current_session].whitePage : whiteList[0]; if (to.query.projectDevice && to.query.type) { loginPath = `${loginPath}&projectDevice=${to.query.projectDevice}&type=${to.query.type}`; } if (current_session && current_session == 'jsxt' || current_session && current_session == 'refereeJsxt') { const raceId = getSessionStorage('raceId'); // 登陆时候保存 竞赛id loginPath = `${loginPath}?raceId=${raceId}`; } 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 (hasPermission(store.getters.roles, to.meta.roles)) { if (to.path === '/404' && to.redirectedFrom === '/') { const project = getSessionStorage('project'); next(localStore.get('trainingPlatformRoute' + store.getters.id + project) || '/trainingPlatform'); } else { next(); } // } } } router.beforeEach((to, from, next) => { const project = getSessionStorage('project'); document.title = loginInfo[project || 'login'].browserTitle || loginInfo[project || 'login'].title; NProgress.start(); const loginPath = getRouteInfo(to); 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 if (to.path == '/jsxtApply') { next(); } else { // 否则全部重定向到登录页 next(loginPath); } } }); NProgress.configure({ easing: 'ease', // 动画方式 speed: 500, // 递增进度条的速度 showSpinner: false, // 是否显示加载ico trickleSpeed: 200, // 自动递增间隔 minimum: 0.3 // 初始化时的最小百分比 }); router.afterEach(() => { // 结束Progress NProgress.done(); });