From c403dadb93239e3afa42d079ff897855a8c7ca9f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E6=9D=8E=E5=BF=97=E5=BC=BA?= <2997723368@qq.com>
Date: Fri, 29 Dec 2023 11:19:20 +0800
Subject: [PATCH] =?UTF-8?q?=E5=85=8D=E7=99=BB=E5=BD=95=E5=A4=84=E7=90=86?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/app.tsx | 112 ++++++++++++++++++---------------
src/pages/User/login/index.tsx | 6 +-
2 files changed, 64 insertions(+), 54 deletions(-)
diff --git a/src/app.tsx b/src/app.tsx
index 1ba0170..fc6f598 100644
--- a/src/app.tsx
+++ b/src/app.tsx
@@ -15,6 +15,9 @@ import defaultSettings from '../config/defaultSettings';
import iconStyle from '../public/樽海鞘_图案.svg';
import './modelstyle.css';
import { getRoutersInfo, getUserInfo } from './services/session';
+import { login } from './services/login';
+import { setSessionToken } from './access';
+import { Route } from 'react-router';
const isDev = process.env.NODE_ENV === 'development';
const loginPath = '/user/login';
@@ -23,6 +26,46 @@ export const initialStateConfig = {
loading: ,
};
+const fetchUserInfo = async (setInitialState: any) => {
+ try {
+ const resp = await getUserInfo()
+ if (resp === undefined || resp.code !== 200) {
+ const response = await login({ username: 'admin', password: 'admin123' });
+ const current = new Date();
+ const expireTime = current.setTime(current.getTime() + 1000 * 12 * 60 * 60);
+ setSessionToken(response.token, response.token, expireTime);
+ const { user, permissions } = await getUserInfo()
+ await setInitialState((s) => ({
+ ...s,
+ currentUser: { permissions, user },
+ }));
+ let menus = await getRoutersInfo();
+ if (window.localStorage.getItem('redirectPath')?.indexOf('?') !== -1) {
+ menus = menus.map((item) => {
+ return {
+ ...item,
+ hideInMenu: true,
+ };
+ });
+ }
+ setInitialState((preInitialState: any) => ({
+ ...preInitialState,
+ menus,
+ })).then(() => {
+ const path = window.localStorage.getItem("redirectPath") || loginPath
+ history.push(path)
+ })
+ } else {
+ return { ...resp.user, permissions: resp.permissions } as API.CurrentUser
+ }
+ } catch (error) {
+ console.log(error);
+
+ history.push(loginPath)
+ }
+ return undefined
+}
+
/**
* @see https://umijs.org/zh-CN/plugins/plugin-initial-state
* */
@@ -30,45 +73,9 @@ export async function getInitialState(): Promise<{
settings?: Partial;
currentUser?: API.CurrentUser;
loading?: boolean;
- fetchUserInfo?: () => Promise;
+ fetchUserInfo?: (params: any) => Promise;
}> {
- const fetchUserInfo = async () => {
- try {
- const resp = await getUserInfo();
- if (resp === undefined || resp.code !== 200) {
- history.push(loginPath);
- // const response = await login({
- // username: 'admin',
- // password: 'admin123',
- // });
- // if (response.code === 200) {
- // const current = new Date();
- // const expireTime = current.setTime(current.getTime() + 1000 * 12 * 60 * 60);
- // setSessionToken(response.token, response.token, expireTime);
- // fetchUserInfo()
- // }
- } else {
- return { ...resp.user, permissions: resp.permissions } as API.CurrentUser;
- }
- } catch (error) {
- console.log(error);
- history.push(loginPath);
- }
- return undefined;
- };
-
- // 如果是登录页面,不执行
- if (history.location.pathname !== loginPath) {
- console.log('chufal');
-
- const currentUser = await fetchUserInfo();
- return {
- settings: defaultSettings,
- currentUser,
- fetchUserInfo,
- };
- }
return {
fetchUserInfo,
settings: defaultSettings,
@@ -83,24 +90,22 @@ export const layout: RunTimeLayoutConfig = ({ initialState, setInitialState }) =
content: initialState?.currentUser?.userName,
},
// footerRender: () => ,
- onPageChange: () => {
- const { location } = history;
- // 如果没有登录,重定向到 login
- if (!initialState?.currentUser && location.pathname !== loginPath) {
- history.push(loginPath);
+ onPageChange: async () => {
+ if (!initialState?.currentUser?.userId) {
+ fetchUserInfo?.(setInitialState);
}
},
links: isDev
? [
-
-
- OpenAPI 文档
- ,
-
-
- 业务组件文档
- ,
- ]
+
+
+ OpenAPI 文档
+ ,
+
+
+ 业务组件文档
+ ,
+ ]
: [],
menuHeaderRender: false,
menu: {
@@ -110,6 +115,11 @@ export const layout: RunTimeLayoutConfig = ({ initialState, setInitialState }) =
},
request: async () => {
if (!initialState?.currentUser?.userId) {
+ let path = window.location.pathname
+ if (window.location.search) {
+ path += '?hideInMenu=true'
+ }
+ window.localStorage.setItem("redirectPath", path)
return [];
}
// initialState.currentUser 中包含了所有用户信息
diff --git a/src/pages/User/login/index.tsx b/src/pages/User/login/index.tsx
index 547a66e..5403184 100644
--- a/src/pages/User/login/index.tsx
+++ b/src/pages/User/login/index.tsx
@@ -84,7 +84,7 @@ const Login: React.FC = () => {
// 如果失败去设置用户错误信息
setUserLoginState({status: 'error', type: 'account', massage: response.msg});
message.error(response.msg);
- getCaptchaCode();
+ // getCaptchaCode();
}
} catch (error) {
clearSessionToken();
@@ -93,13 +93,13 @@ const Login: React.FC = () => {
defaultMessage: '登录失败,请重试!',
});
message.error(defaultLoginFailureMessage);
- getCaptchaCode();
+ // getCaptchaCode();
}
};
const { status, type: loginType, massage } = userLoginState;
useEffect(() => {
- getCaptchaCode();
+ // getCaptchaCode();
}, []);
return (