diff --git a/src/app.tsx b/src/app.tsx index b378380..9fac596 100644 --- a/src/app.tsx +++ b/src/app.tsx @@ -23,6 +23,7 @@ export async function getInitialState(): Promise<{ } return undefined; }; + // 如果不是登录页面,执行 const { location } = history; if (location.pathname !== loginPath) { @@ -32,31 +33,18 @@ export async function getInitialState(): Promise<{ }; } return { + currentUser: undefined, }; } - export const layout: RunTimeLayoutConfig = ({ initialState, setInitialState }) => { - - const [data,setData]=React.useState("") - - const fetchData=(async ()=>{ - const res = await getLoginUserUsingGet(); - console.log('data',res) - setData(res) - }) - - React.useEffect(()=>{ - fetchData() - },[]) - return { actionsRender: () => [], avatarProps: { src: initialState?.currentUser?.userAvatar, title: , render: (_, avatarChildren) => { - return {avatarChildren}; + return {avatarChildren}; }, }, waterMarkProps: { @@ -125,8 +113,8 @@ export const layout: RunTimeLayoutConfig = ({ initialState, setInitialState }) = * @doc https://umijs.org/docs/max/request#配置 */ export const request = { - - baseURL:"http://localhost:8101", + + baseURL:"http://172.20.10.2:8101", withCredentials:true, ...errorConfig, }; diff --git a/src/components/RightContent/AvatarDropdown.tsx b/src/components/RightContent/AvatarDropdown.tsx index cf3e930..f4e3194 100644 --- a/src/components/RightContent/AvatarDropdown.tsx +++ b/src/components/RightContent/AvatarDropdown.tsx @@ -8,6 +8,7 @@ import React, { useCallback } from 'react'; import { flushSync } from 'react-dom'; import HeaderDropdown from '../HeaderDropdown'; import { userLogoutUsingPost } from '@/services/hebi/userController'; +import { useEffect } from 'react'; export type GlobalHeaderRightProps = { menu?: boolean; @@ -39,7 +40,20 @@ const useStyles = createStyles(({ token }) => { }; }); -export const AvatarDropdown: React.FC = ({ menu, children,currentUser }) => { +export const AvatarDropdown: React.FC = ({ menu, children }) => { + const { initialState, setInitialState, refresh } = useModel('@@initialState'); +console.log('initialState',initialState) + // 监听路由变化,并且将当前的 url 保存到 localstorage 中 + // 监听路由变化,并且将当前的 url 保存到 localstorage 中 + // 监听路由变化,并且将当前的 url 保存到 localstorage 中 + // 添加 useEffect 来监听路由变化和初始化 + useEffect(() => { + if (!initialState?.currentUser && window.location.pathname !== '/user/login') { + console.log('loginPath',window.location.pathname) + refresh(); + } + }, []); + /** * 退出登录,并且将当前的 url 保存 */ @@ -60,7 +74,7 @@ export const AvatarDropdown: React.FC = ({ menu, childre }; const { styles } = useStyles(); - const { initialState, setInitialState } = useModel('@@initialState'); + // const { initialState, setInitialState } = useModel('@@initialState'); const onMenuClick = useCallback( (event: MenuInfo) => { @@ -90,13 +104,7 @@ export const AvatarDropdown: React.FC = ({ menu, childre ); - if (!initialState) { - return loading; - } - - // const { currentUser } = initialState; - - if (!currentUser || !currentUser.userName) { + if (!initialState?.currentUser || !initialState.currentUser.userName) { return loading; } @@ -136,4 +144,4 @@ export const AvatarDropdown: React.FC = ({ menu, childre {children} ); -}; \ No newline at end of file +};