permission.js 1.14 KB
Newer Older
Pan's avatar
Pan committed
1 2 3 4
import router from './router'
import store from './store'
import NProgress from 'nprogress' // Progress 进度条
import 'nprogress/nprogress.css'// Progress 进度条样式
5
import { Message } from 'element-ui'
Pan's avatar
Pan committed
6 7
import { getToken } from '@/utils/auth' // 验权

8
const whiteList = ['/login'] // 不重定向白名单
Pan's avatar
Pan committed
9 10 11 12 13
router.beforeEach((to, from, next) => {
  NProgress.start()
  if (getToken()) {
    if (to.path === '/login') {
      next({ path: '/' })
14
      NProgress.done() // if current page is dashboard will not trigger	afterEach hook, so manually handle it
Pan's avatar
Pan committed
15 16
    } else {
      if (store.getters.roles.length === 0) {
17 18
        store.dispatch('GetInfo').then(res => { // 拉取用户信息
          next()
19
        }).catch((err) => {
20
          store.dispatch('FedLogOut').then(() => {
21 22
            Message.error(err || 'Verification failed, please login again')
            next({ path: '/' })
23
          })
Pan's avatar
Pan committed
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
        })
      } else {
        next()
      }
    }
  } else {
    if (whiteList.indexOf(to.path) !== -1) {
      next()
    } else {
      next('/login')
      NProgress.done()
    }
  }
})

router.afterEach(() => {
  NProgress.done() // 结束Progress
})