1
- import { Footer , Question , SelectLang , AvatarDropdown , AvatarName } from '@/components' ;
1
+ import { AvatarDropdown , AvatarName , Footer , Question , SelectLang } from '@/components' ;
2
2
import { currentUser as queryCurrentUser } from '@/services/system/login' ;
3
3
import { LinkOutlined } from '@ant-design/icons' ;
4
+ import queryString from 'query-string' ;
4
5
import type { Settings as LayoutSettings } from '@ant-design/pro-components' ;
5
6
import { PageLoading , SettingDrawer } from '@ant-design/pro-components' ;
6
7
import type { RunTimeLayoutConfig } from '@umijs/max' ;
7
8
import { history , Link } from '@umijs/max' ;
8
9
import defaultSettings from '../config/defaultSettings' ;
9
10
import { errorConfig } from './requestErrorConfig' ;
10
11
import { App } from 'antd' ;
12
+ import { isLoginPath , isSessionExpiredPath } from '@/utils/is' ;
13
+ import { PageEnum } from '@/enums' ;
11
14
12
15
const isDev = process . env . NODE_ENV === 'development' ;
13
- const loginPath = '/login' ;
16
+
17
+ const goLogin = ( ) => {
18
+ const query = queryString . parse ( history . location . search ) ;
19
+ const { redirect } = query as { redirect : string } ;
20
+ history . replace ( {
21
+ pathname : PageEnum . BASE_LOGIN ,
22
+ search :
23
+ redirect &&
24
+ queryString . stringify ( {
25
+ redirect : redirect ,
26
+ } ) ,
27
+ } ) ;
28
+ } ;
14
29
15
30
/**
16
31
* @see https://umijs.org/zh-CN/plugins/plugin-initial-state
@@ -27,27 +42,11 @@ export async function getInitialState(): Promise<{
27
42
// @ts -nocheck
28
43
console . log ( '%c欢迎使用 WeCoding 统一身份认证中心' , 'font-size: 24px;font-weight: bold' ) ;
29
44
const fetchUserInfo = async ( ) => {
30
- try {
31
- return await queryCurrentUser ( {
32
- skipErrorHandler : true ,
33
- } ) ;
34
- } catch ( error ) {
35
- history . push ( loginPath ) ;
36
- }
37
- return undefined ;
45
+ return await queryCurrentUser ( ) . catch ( ( ) => undefined ) ;
38
46
} ;
39
- // 如果不是登录页面,执行
40
- const { location } = history ;
41
- if ( location . pathname !== loginPath ) {
42
- const currentUser = await fetchUserInfo ( ) ;
43
- return {
44
- fetchUserInfo,
45
- currentUser,
46
- settings : defaultSettings as Partial < LayoutSettings > ,
47
- } ;
48
- }
49
47
return {
50
48
fetchUserInfo,
49
+ currentUser : isLoginPath ( ) ? undefined : await fetchUserInfo ( ) ,
51
50
settings : defaultSettings as Partial < LayoutSettings > ,
52
51
} ;
53
52
}
@@ -68,10 +67,17 @@ export const layout: RunTimeLayoutConfig = ({ initialState, setInitialState }) =
68
67
} ,
69
68
footerRender : ( ) => < Footer /> ,
70
69
onPageChange : ( ) => {
71
- const { location } = history ;
70
+ let gotoLogin : boolean = false ;
72
71
// 如果没有登录,重定向到 login
73
- if ( ! initialState ?. currentUser && location . pathname !== loginPath ) {
74
- history . push ( loginPath ) ;
72
+ if ( ! initialState || ! initialState ?. currentUser ) {
73
+ gotoLogin = true ;
74
+ }
75
+ if ( gotoLogin && ( isLoginPath ( ) || isSessionExpiredPath ( ) ) ) {
76
+ return ;
77
+ }
78
+ if ( gotoLogin ) {
79
+ goLogin ( ) ;
80
+ return ;
75
81
}
76
82
} ,
77
83
layoutBgImgList : [
0 commit comments