Skip to content

Commit 30b1c02

Browse files
committed
fix: 修复未登录时获取用户失败后无法跳转到登录页面
1 parent c33685e commit 30b1c02

File tree

5 files changed

+90
-57
lines changed

5 files changed

+90
-57
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"lodash": "^4.17.21",
5858
"moment": "^2.29.4",
5959
"omit.js": "^2.0.2",
60+
"query-string": "^8.1.0",
6061
"querystring": "^0.2.1",
6162
"rc-menu": "^9.9.2",
6263
"rc-util": "^5.32.2",

pnpm-lock.yaml

+37-34
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/app.tsx

+29-23
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,31 @@
1-
import { Footer, Question, SelectLang, AvatarDropdown, AvatarName } from '@/components';
1+
import { AvatarDropdown, AvatarName, Footer, Question, SelectLang } from '@/components';
22
import { currentUser as queryCurrentUser } from '@/services/system/login';
33
import { LinkOutlined } from '@ant-design/icons';
4+
import queryString from 'query-string';
45
import type { Settings as LayoutSettings } from '@ant-design/pro-components';
56
import { PageLoading, SettingDrawer } from '@ant-design/pro-components';
67
import type { RunTimeLayoutConfig } from '@umijs/max';
78
import { history, Link } from '@umijs/max';
89
import defaultSettings from '../config/defaultSettings';
910
import { errorConfig } from './requestErrorConfig';
1011
import { App } from 'antd';
12+
import { isLoginPath, isSessionExpiredPath } from '@/utils/is';
13+
import { PageEnum } from '@/enums';
1114

1215
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+
};
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: [

src/enums/pageEnum.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
export enum PageEnum {
22
// basic login path
33
BASE_LOGIN = '/login',
4+
// session expired
5+
SESSION_EXPIRED = '/session-expired',
46
// basic home path
57
BASE_HOME = '/dashboard',
68
// error page path

src/utils/is.ts

+21
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { history, matchPath } from '@umijs/max';
2+
import { PageEnum } from '@/enums';
3+
14
const toString = Object.prototype.toString;
25

36
export function is(val: unknown, type: string) {
@@ -98,3 +101,21 @@ export function isUrl(path: string): boolean {
98101
/^(((^https?:(?:\/\/)?)(?:[-;:&=+$,\w]+@)?[A-Za-z0-9.-]+(?::\d+)?|(?:www.|[-;:&=+$,\w]+@)[A-Za-z0-9.-]+)((?:\/[+~%/.\w-_]*)?(\/#\/)?(?:\/[+~%/.\w-_]*)?\??(?:[-+=&;%@.\w_]*)#?(?:[\w]*))?)$/;
99102
return reg.test(path);
100103
}
104+
105+
export const isLoginPath = () => {
106+
return matchPath(
107+
{
108+
path: PageEnum.BASE_LOGIN,
109+
},
110+
history.location.pathname,
111+
);
112+
};
113+
114+
export const isSessionExpiredPath = () => {
115+
return matchPath(
116+
{
117+
path: PageEnum.SESSION_EXPIRED,
118+
},
119+
history.location.pathname,
120+
);
121+
};

0 commit comments

Comments
 (0)