2025-03-28 09:44:48 +08:00
|
|
|
|
2024-07-31 16:00:30 +08:00
|
|
|
// 引入Provider
|
|
|
|
import { Provider } from 'react-redux';
|
|
|
|
// 引入仓库
|
|
|
|
import store from './store';
|
2024-03-11 19:13:42 +08:00
|
|
|
import Head from './layout/head/Head.tsx';
|
2024-03-12 18:53:51 +08:00
|
|
|
import Body from './layout/body/Body.tsx';
|
2024-05-07 17:00:32 +08:00
|
|
|
// import Foot from './layout/foot/Foot.tsx';
|
2024-03-26 21:09:41 +08:00
|
|
|
import {
|
|
|
|
GlobalContext,
|
|
|
|
GlobalData,
|
|
|
|
GlobalDataAction,
|
|
|
|
GlobalDataActionType,
|
2024-04-02 18:45:46 +08:00
|
|
|
GlobalDispatchContext,
|
2024-03-26 21:09:41 +08:00
|
|
|
} from "./context/GlobalContext.ts";
|
2025-03-28 09:44:48 +08:00
|
|
|
import React, { Reducer, useReducer, useState, useEffect } from "react";
|
2024-03-16 23:12:49 +08:00
|
|
|
|
2025-03-28 09:44:48 +08:00
|
|
|
import {
|
|
|
|
message
|
|
|
|
} from 'antd';
|
|
|
|
import { get } from "./util/AjaxUtils.ts";
|
2024-03-11 19:13:42 +08:00
|
|
|
const App: React.FC = () => {
|
2025-03-28 09:44:48 +08:00
|
|
|
const [isTokenFetched, setIsTokenFetched] = useState(false);
|
2024-03-26 21:09:41 +08:00
|
|
|
const globalDataReducer = (state: GlobalData, action: GlobalDataAction) => {
|
|
|
|
if (action.type == GlobalDataActionType.REFRESH_SELF) {
|
2024-04-19 18:20:51 +08:00
|
|
|
if (action.user) {
|
2024-03-26 21:09:41 +08:00
|
|
|
state.user.balance = action.user.balance;
|
2024-04-19 18:20:51 +08:00
|
|
|
state.user.userId = action.user.userId;
|
2024-03-26 21:09:41 +08:00
|
|
|
state.user.nickname = action.user.nickname;
|
|
|
|
state.user.username = action.user.username;
|
|
|
|
state.user.hasUserInfo = action.user.hasUserInfo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
...state
|
|
|
|
}
|
2024-03-26 11:54:30 +08:00
|
|
|
}
|
2024-03-26 21:09:41 +08:00
|
|
|
const [globalData, dispatch] = useReducer<Reducer<GlobalData, GlobalDataAction>>(globalDataReducer, {
|
|
|
|
user: {
|
|
|
|
balance: '0',
|
2024-04-19 18:20:51 +08:00
|
|
|
userId: '',
|
2024-03-26 21:09:41 +08:00
|
|
|
username: '',
|
|
|
|
nickname: '',
|
|
|
|
hasUserInfo: false
|
|
|
|
}
|
|
|
|
});
|
2025-03-28 09:44:48 +08:00
|
|
|
const [messageApi] = message.useMessage();
|
|
|
|
// const nav = useNavigate();
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
|
|
get<any>({
|
|
|
|
messageApi,
|
|
|
|
url: '/api/user-info/get-user-self',
|
|
|
|
onSuccess({ data }: any) {
|
|
|
|
// const currentToken = sessionStorage.getItem('token');
|
|
|
|
sessionStorage.setItem('token', data.accessToken);
|
|
|
|
const token = sessionStorage.getItem('token');
|
|
|
|
if (token) {
|
|
|
|
// 若 token 存在,设置 isTokenFetched 为 true
|
|
|
|
setIsTokenFetched(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
}, []);
|
|
|
|
// const token = sessionStorage.getItem('token');
|
|
|
|
// useEffect(() => {
|
|
|
|
// if (token) {
|
|
|
|
// setIsTokenFetched(true)
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
// }, [token]);
|
|
|
|
// 如果 token 还未获取,显示加载页面
|
|
|
|
// if (!isTokenFetched) {
|
|
|
|
// return (
|
|
|
|
// <div style={{ display: 'flex', justifyContent: 'center', alignItems: 'center', height: '100vh' }}>
|
|
|
|
// <p>正在加载,请稍候...</p>
|
|
|
|
// </div>
|
|
|
|
// );
|
|
|
|
// }
|
|
|
|
if (isTokenFetched) {
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
|
|
|
|
<Provider store={store}>
|
|
|
|
<GlobalContext.Provider value={globalData}>
|
|
|
|
<GlobalDispatchContext.Provider value={dispatch}>
|
|
|
|
<Head />
|
|
|
|
<Body />
|
|
|
|
{/* <Foot/> */}
|
|
|
|
</GlobalDispatchContext.Provider>
|
|
|
|
</GlobalContext.Provider>
|
|
|
|
</Provider>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2024-03-26 11:54:30 +08:00
|
|
|
|
2024-03-11 19:13:42 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
export default App;
|