import router from './router' import store from './store' import NProgress from 'nprogress' // progress bar 进度条 import 'nprogress/nprogress.css' // progress bar style 进度条样式 import { Message } from 'element-ui' import { getToken } from '@/utils/auth' // getToken from cookie import axios from 'axios' import { getCurrentSys } from './utils/auth' import Vue from 'vue' import { extractURLParameters } from '@/utils/validate' import { fetchToken } from '@/api/system/user' NProgress.configure({ showSpinner: false })// NProgress configuration const whiteList = ['/login', '/applogin', '/appSubject', '/appFaceLogin', '/404', 'config/baseconfig', '/full/storageTopic', '/full/waterThreat', '/full/highConsequence', '/full/constructionThreat', '/full/routeTopic', '/full/waterThreat', '/full/sinkThreat', '/full/vip', '/full/weather', '/full/needTopic', '/full/supplyTopic', '/full/pipeOverview', '/full/overview', '/full/overview_no_button', '/full/overview_new'] // 不重定向白名单 // 全局钩子 router.beforeEach((to, from, next) => { NProgress.start() // 加载进度条 console.log('to Path:' + to.path) // 如果有token if (getToken()) { console.log('hasToken') // 登录后进入登录页 if (to.path === '/login') { next({ path: '/login' }) NProgress.done() // if current page is dashboard will not trigger afterEach hook, so manually handle it } else if (to.path === '/applogin') { next() NProgress.done() // if current page is dashboard will not trigger afterEach hook, so manually handle it } else if (to.path === '/dashboard') { if (/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) { const baseConfig = Vue.prototype.baseConfig if (baseConfig.toDashboard) { next('/appIndex') // 否则全部重定向到登录页 } else { next({ path: '/applogin' }) } } else { next() } NProgress.done() // if current page is dashboard will not trigger afterEach hook, so manually handle it } else { // 当进入非登陆页或非主页时 if (store.getters.roleList.length === 0) { // 判断当前用户是否已拉取完user_info信息 store.dispatch('GetInfo').then(res => { // 拉取用户信息 let currentSys = getCurrentSys() // 根据路由去找当前子系统 if (to.query.sys) { currentSys = { code: to.query.code, name: to.query.sys, url: to.query.url } // 将当前系统信息保存在localStorage中 store.commit('SET_SYSTEM', currentSys) } // 如果已有当前可进入的子系统,刷新子系统菜单 if (currentSys) { console.log('hasCurrentSystem') // 远程访问获取权限列表(菜单&按钮) store.dispatch('GetMenus', currentSys).then(() => { store.commit('SET_CHANGEFLAG', '0') // 获取所有地址列表 router.addRoutes(store.getters.addRouters) // 动态添加可访问路由表 // 判断路由是否被允许,不允许则重定向 next({ ...to, replace: true }) // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record }) } else { // 没有当前子系统信息,跳转到选择子系统页面 console.log('dontHasCurrentSystem') next({ path: '/dashboard' }) } }).catch((err) => { console.log('toFedLogOut') store.dispatch('FedLogOut').then(() => { Message.error(err || '权限验证失败,请重新登录系统') next({ path: '/' }) }) }) } else { // TODO: 判断路由是否被允许,不允许则重定向 next() } } } else { if (whiteList.indexOf(to.path) !== -1) { // 免登录白名单,直接进入 console.log('is In WhiteList') next() } else { debugger // 获取链接里的token const token = to.query.token console.log('token' + token) // // 携带token 需要去掉 // const zfToken = to.query['ZF-TOKEN'] let zfToken = '' // 携带token 需要去掉 const hrefIdx = window.location.href.indexOf('ZF-TOKEN') // url中是否携带token if (hrefIdx > -1) { // const params = queryString(window.location.href) // zfToken = stripscript(params.token) console.log('hrefIdx', extractURLParameters(window.location.href)) const params = extractURLParameters(window.location.href) zfToken = params['ZF-TOKEN'] } console.log('zfToken' + zfToken) if (zfToken) { const sendParams = { zf_token: zfToken } // eslint-disable-next-line no-unused-vars debugger // window.location.href = 'https://www.baidu.com/?tn=25017023_17_dg' fetchToken(sendParams).then((resToken) => { if (resToken.code === 200) { axios.get('./static/project.config.json').then((result) => { const params = '?token=' + resToken.data.token + '&url=' + result.data.singleSignLastUrl const loginUrl = result.data.singleSignFirstUrl + '/sso/integrationConfig/login' // next({ // path: '/appSubject', // query: { name: resToken.data.title, url: loginUrl + params } // }) // window.open(loginUrl + params, '_blank') window.location.href = loginUrl + params }) } else { console.log('用户验证失败') next(`/login?redirect=${to.path}`) // 否则全部重定向到登录页 } }) } else { debugger if (token) { // 调用自动登录接口,否则跳转到登录页 const params = token store.dispatch('AppLogin', params).then(() => { console.log('自动登录成功') console.log(to.path) next({ ...to, replace: true }) // 登录成功继续 }).catch(() => { next('/401') // 否则全部重定向到401没有权限页面 }) } else { if (/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) { next(`/applogin`) // 否则全部重定向到登录页 } else { next(`/login?redirect=${to.path}`) // 否则全部重定向到登录页 } } } NProgress.done() } } }) router.afterEach(() => { NProgress.done() // 结束Progress })