123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- 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('LogOut').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()
- })
|