@@ -4,134 +4,143 @@ import getPageTitle from '@/utils/page'
4
4
import router from '@/router'
5
5
import Nprogress from 'nprogress'
6
6
import 'nprogress/nprogress.css'
7
- Nprogress . configure ( { showSpinner : false , ease : 'ease' , speed : 500 } )
8
7
9
- const whiteList = [ 'Login' , 'Init' ]
8
+ // 配置 NProgress
9
+ Nprogress . configure ( {
10
+ showSpinner : false ,
11
+ ease : 'ease' ,
12
+ speed : 500
13
+ } )
10
14
11
- const getRouter = async ( userStore ) => {
12
- const routerStore = useRouterStore ( )
13
- await routerStore . SetAsyncRouter ( )
14
- await userStore . GetUserInfo ( )
15
- const asyncRouters = routerStore . asyncRouters
16
- asyncRouters . forEach ( ( asyncRouter ) => {
17
- router . addRoute ( asyncRouter )
18
- } )
15
+ // 白名单路由
16
+ const WHITE_LIST = [ 'Login' , 'Init' , 'Iframe' ]
17
+
18
+ // 处理路由加载
19
+ const setupRouter = async ( userStore ) => {
20
+ try {
21
+ const routerStore = useRouterStore ( )
22
+ await Promise . all ( [ routerStore . SetAsyncRouter ( ) , userStore . GetUserInfo ( ) ] )
23
+
24
+ routerStore . asyncRouters . forEach ( ( route ) => router . addRoute ( route ) )
25
+ return true
26
+ } catch ( error ) {
27
+ console . error ( 'Setup router failed:' , error )
28
+ return false
29
+ }
19
30
}
20
31
32
+ // 移除加载动画
21
33
const removeLoading = ( ) => {
22
34
const element = document . getElementById ( 'gva-loading-box' )
23
- if ( element ) {
24
- element . remove ( )
25
- }
35
+ element ?. remove ( )
26
36
}
27
37
28
- async function handleKeepAlive ( to ) {
29
- if ( to . matched . some ( ( item ) => item . meta . keepAlive ) ) {
30
- if ( to . matched && to . matched . length > 2 ) {
31
- for ( let i = 1 ; i < to . matched . length ; i ++ ) {
32
- const element = to . matched [ i - 1 ]
33
- if ( element . name === 'layout' ) {
34
- to . matched . splice ( i , 1 )
35
- await handleKeepAlive ( to )
36
- }
37
- // 如果没有按需加载完成则等待加载
38
- if ( typeof element . components . default === 'function' ) {
39
- await element . components . default ( )
40
- await handleKeepAlive ( to )
41
- }
38
+ // 处理组件缓存
39
+ const handleKeepAlive = async ( to ) => {
40
+ if ( ! to . matched . some ( ( item ) => item . meta . keepAlive ) ) return
41
+
42
+ if ( to . matched ?. length > 2 ) {
43
+ for ( let i = 1 ; i < to . matched . length ; i ++ ) {
44
+ const element = to . matched [ i - 1 ]
45
+
46
+ if ( element . name === 'layout' ) {
47
+ to . matched . splice ( i , 1 )
48
+ await handleKeepAlive ( to )
49
+ continue
50
+ }
51
+
52
+ if ( typeof element . components . default === 'function' ) {
53
+ await element . components . default ( )
54
+ await handleKeepAlive ( to )
42
55
}
43
56
}
44
57
}
45
58
}
46
59
60
+ // 处理路由重定向
61
+ const handleRedirect = ( to , userStore ) => {
62
+ if ( router . hasRoute ( userStore . userInfo . authority . defaultRouter ) ) {
63
+ return { ...to , replace : true }
64
+ }
65
+ return { path : '/layout/404' }
66
+ }
67
+
68
+ // 路由守卫
47
69
router . beforeEach ( async ( to , from ) => {
70
+ const userStore = useUserStore ( )
48
71
const routerStore = useRouterStore ( )
72
+ const token = userStore . token
73
+
49
74
Nprogress . start ( )
50
- const userStore = useUserStore ( )
75
+
76
+ // 处理元数据和缓存
51
77
to . meta . matched = [ ...to . matched ]
52
- handleKeepAlive ( to )
53
- const token = userStore . token
54
- // 在白名单中的判断情况
78
+ await handleKeepAlive ( to )
79
+
80
+ // 设置页面标题
55
81
document . title = getPageTitle ( to . meta . title , to )
82
+
56
83
if ( to . meta . client ) {
57
84
return true
58
85
}
59
- if ( whiteList . indexOf ( to . name ) > - 1 ) {
60
- if ( token ) {
61
- if ( ! routerStore . asyncRouterFlag && whiteList . indexOf ( from . name ) < 0 ) {
62
- await getRouter ( userStore )
63
- }
64
- // token 可以解析但是却是不存在的用户 id 或角色 id 会导致无限调用
65
- if ( userStore . userInfo ?. authority ?. defaultRouter != null ) {
66
- if ( router . hasRoute ( userStore . userInfo . authority . defaultRouter ) ) {
67
- return { name : userStore . userInfo . authority . defaultRouter }
68
- } else {
69
- return { path : '/layout/404' }
70
- }
71
- } else {
72
- // 强制退出账号
73
- userStore . ClearStorage ( )
74
- return {
75
- name : 'Login' ,
76
- query : {
77
- redirect : document . location . hash
78
- }
79
- }
80
- }
81
- } else {
82
- return true
86
+
87
+ // 白名单路由处理
88
+ if ( WHITE_LIST . includes ( to . name ) ) {
89
+ if (
90
+ token &&
91
+ ! routerStore . asyncRouterFlag &&
92
+ ! WHITE_LIST . includes ( from . name )
93
+ ) {
94
+ await setupRouter ( userStore )
83
95
}
84
- } else {
85
- // 不在白名单中并且已经登录的时候
86
- if ( token ) {
87
- if ( sessionStorage . getItem ( 'needToHome' ) === 'true' ) {
88
- sessionStorage . removeItem ( 'needToHome' )
89
- return { path : '/' }
90
- }
91
- // 添加flag防止多次获取动态路由和栈溢出
92
- if ( ! routerStore . asyncRouterFlag && whiteList . indexOf ( from . name ) < 0 ) {
93
- await getRouter ( userStore )
94
- if ( userStore . token ) {
95
- if ( router . hasRoute ( userStore . userInfo . authority . defaultRouter ) ) {
96
- return { ...to , replace : true }
97
- } else {
98
- return { path : '/layout/404' }
99
- }
100
- } else {
101
- return {
102
- name : 'Login' ,
103
- query : { redirect : to . href }
104
- }
105
- }
106
- } else {
107
- if ( to . matched . length ) {
108
- return true
109
- } else {
110
- return { path : '/layout/404' }
111
- }
112
- }
96
+ return true
97
+ }
98
+
99
+ // 需要登录的路由处理
100
+ if ( token ) {
101
+ // 处理需要跳转到首页的情况
102
+ if ( sessionStorage . getItem ( 'needToHome' ) === 'true' ) {
103
+ sessionStorage . removeItem ( 'needToHome' )
104
+ return { path : '/' }
113
105
}
114
- // 不在白名单中并且未登录的时候
115
- if ( ! token ) {
106
+
107
+ // 处理异步路由
108
+ if ( ! routerStore . asyncRouterFlag && ! WHITE_LIST . includes ( from . name ) ) {
109
+ const setupSuccess = await setupRouter ( userStore )
110
+
111
+ if ( setupSuccess && userStore . token ) {
112
+ return handleRedirect ( to , userStore )
113
+ }
114
+
116
115
return {
117
116
name : 'Login' ,
118
- query : {
119
- redirect : document . location . hash
120
- }
117
+ query : { redirect : to . href }
121
118
}
122
119
}
120
+
121
+ return to . matched . length ? true : { path : '/layout/404' }
122
+ }
123
+
124
+ // 未登录跳转登录页
125
+ return {
126
+ name : 'Login' ,
127
+ query : {
128
+ redirect : document . location . hash
129
+ }
123
130
}
124
131
} )
125
132
133
+ // 路由加载完成
126
134
router . afterEach ( ( ) => {
127
- // 路由加载完成后关闭进度条
128
- document . getElementsByClassName ( 'main-cont main-right' ) [ 0 ] ?. scrollTo ( 0 , 0 )
135
+ document . querySelector ( '.main-cont.main-right' ) ?. scrollTo ( 0 , 0 )
129
136
Nprogress . done ( )
130
137
} )
131
138
132
- router . onError ( ( ) => {
133
- // 路由发生错误后销毁进度条
139
+ // 路由错误处理
140
+ router . onError ( ( error ) => {
141
+ console . error ( 'Router error:' , error )
134
142
Nprogress . remove ( )
135
143
} )
136
144
145
+ // 移除初始加载动画
137
146
removeLoading ( )
0 commit comments