提交 | 用户 | 时间
|
0ca254
|
1 |
import router from './router' |
A |
2 |
import store from './store' |
|
3 |
import { Message } from 'element-ui' |
|
4 |
import NProgress from 'nprogress' |
|
5 |
import 'nprogress/nprogress.css' |
|
6 |
import { getToken } from '@/utils/auth' |
|
7 |
import { isRelogin } from '@/utils/request' |
|
8 |
|
|
9 |
NProgress.configure({ showSpinner: false }) |
|
10 |
|
|
11 |
const whiteList = ['/login', '/register'] |
|
12 |
|
|
13 |
router.beforeEach((to, from, next) => { |
|
14 |
NProgress.start() |
|
15 |
if (getToken()) { |
|
16 |
to.meta.title && store.dispatch('settings/setTitle', to.meta.title) |
|
17 |
/* has token*/ |
|
18 |
if (to.path === '/login') { |
|
19 |
next({ path: '/' }) |
|
20 |
NProgress.done() |
|
21 |
} else if (whiteList.indexOf(to.path) !== -1) { |
|
22 |
next() |
|
23 |
} else { |
|
24 |
if (store.getters.roles.length === 0) { |
|
25 |
isRelogin.show = true |
|
26 |
// 判断当前用户是否已拉取完user_info信息 |
|
27 |
store.dispatch('GetInfo').then(() => { |
|
28 |
isRelogin.show = false |
|
29 |
store.dispatch('GenerateRoutes').then(accessRoutes => { |
|
30 |
// 根据roles权限生成可访问的路由表 |
|
31 |
router.addRoutes(accessRoutes) // 动态添加可访问路由表 |
|
32 |
next({ ...to, replace: true }) // hack方法 确保addRoutes已完成 |
|
33 |
}) |
|
34 |
}).catch(err => { |
|
35 |
store.dispatch('LogOut').then(() => { |
|
36 |
Message.error(err) |
|
37 |
next({ path: '/' }) |
|
38 |
}) |
|
39 |
}) |
|
40 |
} else { |
|
41 |
next() |
|
42 |
} |
|
43 |
} |
|
44 |
} else { |
|
45 |
// 没有token |
|
46 |
if (whiteList.indexOf(to.path) !== -1) { |
|
47 |
// 在免登录白名单,直接进入 |
|
48 |
next() |
|
49 |
} else { |
|
50 |
next(`/login?redirect=${encodeURIComponent(to.fullPath)}`) // 否则全部重定向到登录页 |
|
51 |
NProgress.done() |
|
52 |
} |
|
53 |
} |
|
54 |
}) |
|
55 |
|
|
56 |
router.afterEach(() => { |
|
57 |
NProgress.done() |
|
58 |
}) |