import Vue from 'vue' import router from './router' import store from './store' import NProgress from 'nprogress' // progress bar import 'nprogress/nprogress.css' // progress bar style import notification from 'ant-design-vue/es/notification' import { setDocumentTitle, domTitle } from '@/utils/domUtil' import { ACCESS_TOKEN } from '@/store/mutation-types' NProgress.configure({ showSpinner: false }) // NProgress Configuration const whiteList = ['login'] // no redirect whitelist router.beforeEach((to, from, next) => { // start progress bar NProgress.start() to.meta && (typeof to.meta.title !== 'undefined' && setDocumentTitle(`${to.meta.title} - ${domTitle}`)) if (Vue.ls.get(ACCESS_TOKEN)) { if (to.path === '/user/login') { next({ path: '/dashboard' }) NProgress.done() } else { if (Object.keys(store.getters.apis).length === 0) { store .dispatch('GetInfo') .then(apis => { store.dispatch('GenerateRoutes', { apis }).then(() => { router.addRoutes(store.getters.addRouters) const redirect = decodeURIComponent(from.query.redirect || to.path) if (to.path === redirect) { next({ ...to, replace: true }) } else { next({ path: redirect }) } }) }) .catch(() => { notification.error({ message: 'Error', description: 'Exception caught while trying to discover APIs' }) store.dispatch('Logout').then(() => { next({ path: '/user/login', query: { redirect: to.fullPath } }) }) }) } else { next() } } } else { if (whiteList.includes(to.name)) { next() } else { next({ path: '/user/login', query: { redirect: to.fullPath } }) NProgress.done() } } }) router.afterEach(() => { NProgress.done() // finish progress bar })