1- import { Footer , Question , SelectLang , AvatarDropdown , AvatarName } from '@/components' ;
1+ import { AvatarDropdown , AvatarName , Footer , Question , SelectLang } from '@/components' ;
22import { currentUser as queryCurrentUser } from '@/services/system/login' ;
33import { LinkOutlined } from '@ant-design/icons' ;
4+ import queryString from 'query-string' ;
45import type { Settings as LayoutSettings } from '@ant-design/pro-components' ;
56import { PageLoading , SettingDrawer } from '@ant-design/pro-components' ;
67import type { RunTimeLayoutConfig } from '@umijs/max' ;
78import { history , Link } from '@umijs/max' ;
89import defaultSettings from '../config/defaultSettings' ;
910import { errorConfig } from './requestErrorConfig' ;
1011import { App } from 'antd' ;
12+ import { isLoginPath , isSessionExpiredPath } from '@/utils/is' ;
13+ import { PageEnum } from '@/enums' ;
1114
1215const 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+ } ;
1429
1530/**
1631 * @see https://umijs.org/zh-CN/plugins/plugin-initial-state
@@ -27,27 +42,11 @@ export async function getInitialState(): Promise<{
2742 // @ts -nocheck
2843 console . log ( '%c欢迎使用 WeCoding 统一身份认证中心' , 'font-size: 24px;font-weight: bold' ) ;
2944 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 ) ;
3846 } ;
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- }
4947 return {
5048 fetchUserInfo,
49+ currentUser : isLoginPath ( ) ? undefined : await fetchUserInfo ( ) ,
5150 settings : defaultSettings as Partial < LayoutSettings > ,
5251 } ;
5352}
@@ -68,10 +67,17 @@ export const layout: RunTimeLayoutConfig = ({ initialState, setInitialState }) =
6867 } ,
6968 footerRender : ( ) => < Footer /> ,
7069 onPageChange : ( ) => {
71- const { location } = history ;
70+ let gotoLogin : boolean = false ;
7271 // 如果没有登录,重定向到 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 ;
7581 }
7682 } ,
7783 layoutBgImgList : [
0 commit comments