@@ -5,11 +5,11 @@ import store from './store'
5
5
import NProgress from 'nprogress' // progress bar
6
6
import 'nprogress/nprogress.css' // progress bar style
7
7
import notification from 'ant-design-vue/es/notification'
8
- import { ACCESS_TOKEN } from " @/store/mutation-types"
8
+ import { ACCESS_TOKEN } from ' @/store/mutation-types'
9
9
10
- NProgress . configure ( { showSpinner : false } ) // NProgress Configuration
10
+ NProgress . configure ( { showSpinner : false } ) // NProgress Configuration
11
11
12
- const whiteList = [ '/user/login' , '/user/register' , '/user/register-result' ] // no redirect whitelist
12
+ const whiteList = [ '/user/login' , '/user/register' , '/user/register-result' ] // no redirect whitelist
13
13
14
14
router . beforeEach ( ( to , from , next ) => {
15
15
NProgress . start ( ) // start progress bar
@@ -21,35 +21,48 @@ router.beforeEach((to, from, next) => {
21
21
NProgress . done ( )
22
22
} else {
23
23
if ( store . getters . roles . length === 0 ) {
24
- store . dispatch ( 'GetInfo' ) . then ( res => {
25
- const roles = res . result && res . result . role
26
- store . dispatch ( 'GenerateRoutes' , { roles } ) . then ( ( ) => { // 根据roles权限生成可访问的路由表
27
- router . addRoutes ( store . getters . addRouters ) // 动态添加可访问路由表
28
- next ( { ...to , replace : true } ) // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
24
+ store
25
+ . dispatch ( 'GetInfo' )
26
+ . then ( res => {
27
+ const roles = res . result && res . result . role
28
+ store . dispatch ( 'GenerateRoutes' , { roles } ) . then ( ( ) => {
29
+ // 根据roles权限生成可访问的路由表
30
+ // 动态添加可访问路由表
31
+ router . addRoutes ( store . getters . addRouters )
32
+ const redirect = decodeURIComponent ( from . query . redirect || to . path )
33
+ if ( to . path === redirect ) {
34
+ // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
35
+ next ( { ...to , replace : true } )
36
+ } else {
37
+ // 跳转到目的路由
38
+ next ( { path : redirect } )
39
+ }
40
+ } )
29
41
} )
30
- } ) . catch ( ( ) => {
31
- notification . error ( { message : '错误' , description : '请求用户信息失败,请重试' } )
32
- store . dispatch ( 'Logout' ) . then ( ( ) => {
33
- next ( { path : '/user/login' } )
42
+ . catch ( ( ) => {
43
+ notification . error ( {
44
+ message : '错误' ,
45
+ description : '请求用户信息失败,请重试'
46
+ } )
47
+ store . dispatch ( 'Logout' ) . then ( ( ) => {
48
+ next ( { path : '/user/login' , query : { redirect : to . fullPath } } )
49
+ } )
34
50
} )
35
- } )
36
-
37
51
} else {
38
52
next ( )
39
53
}
40
54
}
41
55
} else {
42
- if ( whiteList . indexOf ( to . path ) !== - 1 ) { // 在免登录白名单,直接进入
56
+ if ( whiteList . indexOf ( to . path ) !== - 1 ) {
57
+ // 在免登录白名单,直接进入
43
58
next ( )
44
59
} else {
45
- next ( '/user/login' )
60
+ next ( { path : '/user/login' , query : { redirect : to . fullPath } } )
46
61
NProgress . done ( ) // if current page is login will not trigger afterEach hook, so manually handle it
47
62
}
48
-
49
63
}
50
-
51
64
} )
52
65
53
66
router . afterEach ( ( ) => {
54
67
NProgress . done ( ) // finish progress bar
55
- } )
68
+ } )
0 commit comments