12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- import router from './router'
- import store from './store'
- import { Message } from 'element-ui'
- import NProgress from 'nprogress'
- import 'nprogress/nprogress.css'
- import { getToken } from '@/utils/auth'
- NProgress.configure({ showSpinner: false })
- const whiteList = ['/login', '/auth-redirect', '/bind', '/register']
- router.beforeEach((to, from, next) => {
- NProgress.start()
- if (getToken()) {
-
- if (to.path === '/login') {
- next({ path: '/' })
- NProgress.done()
- } else {
- if (store.getters.roles.length === 0) {
-
- store.dispatch('GetInfo').then(res => {
-
- const roles = res.roles
- store.dispatch('GenerateRoutes', { roles }).then(accessRoutes => {
-
-
-
- router.addRoutes(accessRoutes)
- next({ ...to, replace: true })
- })
- })
- .catch(err => {
- store.dispatch('FedLogOut').then(() => {
- Message.error(err)
- next({ path: '/' })
- })
- })
- } else {
- next()
-
-
-
-
-
-
-
- }
- }
- } else {
-
- if (whiteList.indexOf(to.path) !== -1) {
-
- next()
- } else {
- next(`/login?redirect=${to.fullPath}`)
- NProgress.done()
- }
- }
- })
- router.afterEach(() => {
- NProgress.done()
- })
|