123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- import router from './router'
- import store from './store'
- import storage from 'store'
- import NProgress from 'nprogress'
- import '@/components/NProgress/nprogress.less'
- import { setDocumentTitle, domTitle } from '@/utils/domUtil'
- import { ACCESS_TOKEN } from '@/store/mutation-types'
- import { i18nRender } from '@/locales'
- NProgress.configure({ showSpinner: false })
- const allowList = [
- 'login',
- 'applyLicense',
- 'cargodetail',
- 'dangerouscargodetail',
- 'xraycargodetail',
- 'xraygoodsnamekeysuggest',
- 'riskgoodsnamekeysuggest',
- 'dangerousgoodsnamekeysuggest',
- 'optimizationsuggestionrule',
- 'waybill',
- 'redirect'
- ]
- const loginRoutePath = '/user/login'
- const defaultRoutePath = '/index'
- router.beforeEach((to, from, next) => {
- NProgress.start()
- to.meta && (typeof to.meta.title !== 'undefined' && setDocumentTitle(`${i18nRender(to.meta.title)} - ${domTitle}`))
-
- const paramsJson = JSON.stringify(to.params)
- if (paramsJson !== '{}') {
- localStorage.setItem('routerParams' + to.name, paramsJson)
- }
-
- if (storage.get(ACCESS_TOKEN)) {
- if (to.path === loginRoutePath || to.path === '/') {
- next({ path: defaultRoutePath })
- NProgress.done()
- } else {
-
- if (store.getters.roles.length === 0) {
-
- store
- .dispatch('GetInfo')
- .then(res => {
-
- const roles = res.roles
-
- store.dispatch('GenerateRoutes', { roles }).then(() => {
-
-
- router.addRoutes(store.getters.addRouters)
-
-
- next({ ...to, replace: true })
-
-
-
-
-
-
-
-
- })
- })
- .catch(() => {
-
-
-
-
-
- store.dispatch('Logout').then(() => {
- next({ path: '/' })
- })
- })
- } else {
- next()
- }
- }
- } else {
- if (allowList.includes(to.name)) {
-
- next()
- } else {
- next({ path: loginRoutePath, query: { redirect: to.fullPath } })
- NProgress.done()
- }
- }
- })
- router.afterEach(() => {
- NProgress.done()
- })
|