huibing.xie | 1f1606f | 2018-08-20 15:46:55 +0800 | [diff] [blame] | 1 | import router from './router' |
| 2 | import store from './store' |
| 3 | import NProgress from 'nprogress' // Progress 进度条 |
| 4 | import 'nprogress/nprogress.css'// Progress 进度条样式 |
| 5 | import { Message } from 'element-ui' |
| 6 | import { getToken } from '@/utils/auth' // 验权 |
| 7 | |
| 8 | const whiteList = ['/login'] // 不重定向白名单 |
| 9 | router.beforeEach((to, from, next) => { |
| 10 | NProgress.start() |
| 11 | if (getToken()) { |
| 12 | if (to.path === '/login') { |
| 13 | next({ path: '/' }) |
| 14 | NProgress.done() // if current page is dashboard will not trigger afterEach hook, so manually handle it |
| 15 | } else { |
| 16 | if (store.getters.roles.length === 0) { |
| 17 | store.dispatch('GetInfo').then(res => { // 拉取用户信息 |
| 18 | next() |
| 19 | }).catch((err) => { |
| 20 | store.dispatch('FedLogOut').then(() => { |
| 21 | Message.error(err || '验证失败,请重新登录') |
| 22 | next({ path: '/' }) |
| 23 | }) |
| 24 | }) |
| 25 | } else { |
| 26 | next() |
| 27 | } |
| 28 | } |
| 29 | } else { |
| 30 | if (whiteList.indexOf(to.path) !== -1) { |
| 31 | next() |
| 32 | } else { |
| 33 | next('/login') |
| 34 | NProgress.done() |
| 35 | } |
| 36 | } |
| 37 | }) |
| 38 | |
| 39 | router.afterEach(() => { |
| 40 | NProgress.done() // 结束Progress |
| 41 | }) |