fix: initialState refresh
This commit is contained in:
+1
-6
@@ -8,7 +8,6 @@ import {
|
|||||||
import { RequestConfig, history } from '@umijs/max';
|
import { RequestConfig, history } from '@umijs/max';
|
||||||
|
|
||||||
const loginPath = '/login';
|
const loginPath = '/login';
|
||||||
let currentUserInfo: any = {};
|
|
||||||
|
|
||||||
// 运行时配置
|
// 运行时配置
|
||||||
export async function getInitialState(): Promise<{
|
export async function getInitialState(): Promise<{
|
||||||
@@ -42,9 +41,7 @@ export async function getInitialState(): Promise<{
|
|||||||
|
|
||||||
if (![loginPath].includes(location.pathname)) {
|
if (![loginPath].includes(location.pathname)) {
|
||||||
const userInfo = await fetchUserInfo();
|
const userInfo = await fetchUserInfo();
|
||||||
currentUserInfo = {
|
|
||||||
...userInfo
|
|
||||||
};
|
|
||||||
return {
|
return {
|
||||||
fetchUserInfo,
|
fetchUserInfo,
|
||||||
currentUser: userInfo
|
currentUser: userInfo
|
||||||
@@ -55,8 +52,6 @@ export async function getInitialState(): Promise<{
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('app.tsx');
|
|
||||||
|
|
||||||
export const request: RequestConfig = {
|
export const request: RequestConfig = {
|
||||||
baseURL: ' /v1',
|
baseURL: ' /v1',
|
||||||
...requestConfig
|
...requestConfig
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ const Exception: React.FC<{
|
|||||||
}> = (props) => {
|
}> = (props) => {
|
||||||
const intl = useIntl();
|
const intl = useIntl();
|
||||||
// render custom 404
|
// render custom 404
|
||||||
|
console.log('exception====', props.route);
|
||||||
return (
|
return (
|
||||||
(!props.route && (props.noFound || props.notFound)) ||
|
(!props.route && (props.noFound || props.notFound)) ||
|
||||||
// render custom 403
|
// render custom 403
|
||||||
|
|||||||
@@ -30,6 +30,11 @@ import { getRightRenderContent } from './rightRender';
|
|||||||
import { patchRoutes } from './runtime';
|
import { patchRoutes } from './runtime';
|
||||||
const loginPath = '/login';
|
const loginPath = '/login';
|
||||||
|
|
||||||
|
type InitialStateType = {
|
||||||
|
fetchUserInfo: () => Promise<Global.UserInfo>;
|
||||||
|
currentUser?: Global.UserInfo;
|
||||||
|
};
|
||||||
|
|
||||||
// 过滤出需要显示的路由, 这里的filterFn 指 不希望显示的层级
|
// 过滤出需要显示的路由, 这里的filterFn 指 不希望显示的层级
|
||||||
const filterRoutes = (
|
const filterRoutes = (
|
||||||
routes: IRoute[],
|
routes: IRoute[],
|
||||||
@@ -93,6 +98,9 @@ export default (props: any) => {
|
|||||||
loading: false,
|
loading: false,
|
||||||
setInitialState: null
|
setInitialState: null
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// initialState: InitialStateType
|
||||||
|
|
||||||
const { initialState, loading, setInitialState } = initialInfo;
|
const { initialState, loading, setInitialState } = initialInfo;
|
||||||
|
|
||||||
const userConfig = {
|
const userConfig = {
|
||||||
@@ -121,7 +129,6 @@ export default (props: any) => {
|
|||||||
const runtimeConfig = {
|
const runtimeConfig = {
|
||||||
...initialInfo,
|
...initialInfo,
|
||||||
logout: async (userInfo) => {
|
logout: async (userInfo) => {
|
||||||
console.log('logout', userInfo);
|
|
||||||
await logout();
|
await logout();
|
||||||
navigate(loginPath);
|
navigate(loginPath);
|
||||||
},
|
},
|
||||||
@@ -181,8 +188,7 @@ export default (props: any) => {
|
|||||||
onPageChange={(route) => {
|
onPageChange={(route) => {
|
||||||
const { location } = history;
|
const { location } = history;
|
||||||
|
|
||||||
// 如果没有修改密码,重定向到修改密码
|
console.log('onPageChange', userInfo, initialState);
|
||||||
console.log('onPageChange', initialState);
|
|
||||||
if (
|
if (
|
||||||
location.pathname !== loginPath &&
|
location.pathname !== loginPath &&
|
||||||
userInfo?.require_password_change
|
userInfo?.require_password_change
|
||||||
|
|||||||
@@ -144,11 +144,9 @@ export function getRightRenderContent(opts: {
|
|||||||
),
|
),
|
||||||
onClick() {
|
onClick() {
|
||||||
if (item.key === 'version') {
|
if (item.key === 'version') {
|
||||||
// opts.runtimeConfig.showVersion();
|
|
||||||
opts.runtimeConfig.showVersion();
|
opts.runtimeConfig.showVersion();
|
||||||
}
|
}
|
||||||
if (item.key === 'shortcuts') {
|
if (item.key === 'shortcuts') {
|
||||||
// opts.runtimeConfig.showShortcuts();
|
|
||||||
opts.runtimeConfig.showShortcuts();
|
opts.runtimeConfig.showShortcuts();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,52 +12,13 @@ import { history, useIntl, useModel } from '@umijs/max';
|
|||||||
import { Button, Checkbox, Form } from 'antd';
|
import { Button, Checkbox, Form } from 'antd';
|
||||||
import CryptoJS from 'crypto-js';
|
import CryptoJS from 'crypto-js';
|
||||||
import { useAtom } from 'jotai';
|
import { useAtom } from 'jotai';
|
||||||
|
import { memo, useEffect, useMemo } from 'react';
|
||||||
import { flushSync } from 'react-dom';
|
import { flushSync } from 'react-dom';
|
||||||
import { login } from '../apis';
|
import { login } from '../apis';
|
||||||
|
|
||||||
const REMEMBER_ME_KEY = 'r_m';
|
const REMEMBER_ME_KEY = 'r_m';
|
||||||
const CRYPT_TEXT = 'seal';
|
const CRYPT_TEXT = 'seal';
|
||||||
|
|
||||||
const renderLogo = () => {
|
|
||||||
return (
|
|
||||||
<div
|
|
||||||
style={{
|
|
||||||
display: 'flex',
|
|
||||||
marginBottom: 0,
|
|
||||||
padding: '15px 0',
|
|
||||||
justifyContent: 'center',
|
|
||||||
alignItems: 'center'
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
<img src={LogoIcon} alt="logo" style={{ height: '26px' }} />
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
const renderWelCome = (intl: any) => {
|
|
||||||
return (
|
|
||||||
<div
|
|
||||||
style={{
|
|
||||||
display: 'flex',
|
|
||||||
marginBottom: 32,
|
|
||||||
fontSize: 20,
|
|
||||||
justifyContent: 'center',
|
|
||||||
alignItems: 'center'
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
<div className="flex-center">
|
|
||||||
<span>
|
|
||||||
{intl?.formatMessage({ id: 'users.login.title' }, { name: '' })}
|
|
||||||
</span>
|
|
||||||
<img
|
|
||||||
src={LogoIcon}
|
|
||||||
alt="logo"
|
|
||||||
style={{ height: '24px', marginLeft: 10 }}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
const LoginForm = () => {
|
const LoginForm = () => {
|
||||||
const [userInfo, setUserInfo] = useAtom(userAtom);
|
const [userInfo, setUserInfo] = useAtom(userAtom);
|
||||||
const [initialPassword, setInitialPassword] = useAtom(initialPasswordAtom);
|
const [initialPassword, setInitialPassword] = useAtom(initialPasswordAtom);
|
||||||
@@ -65,6 +26,32 @@ const LoginForm = () => {
|
|||||||
const intl = useIntl();
|
const intl = useIntl();
|
||||||
const [form] = Form.useForm();
|
const [form] = Form.useForm();
|
||||||
|
|
||||||
|
console.log('initialState+++++++=login', userInfo, initialState);
|
||||||
|
const renderWelCome = useMemo(() => {
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
style={{
|
||||||
|
display: 'flex',
|
||||||
|
marginBottom: 32,
|
||||||
|
fontSize: 20,
|
||||||
|
justifyContent: 'center',
|
||||||
|
alignItems: 'center'
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<div className="flex-center">
|
||||||
|
<span>
|
||||||
|
{intl?.formatMessage({ id: 'users.login.title' }, { name: '' })}
|
||||||
|
</span>
|
||||||
|
<img
|
||||||
|
src={LogoIcon}
|
||||||
|
alt="logo"
|
||||||
|
style={{ height: '24px', marginLeft: 10 }}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}, []);
|
||||||
|
|
||||||
const gotoDefaultPage = (userInfo: any) => {
|
const gotoDefaultPage = (userInfo: any) => {
|
||||||
const pathname =
|
const pathname =
|
||||||
userInfo && userInfo?.is_admin ? '/dashboard' : '/playground';
|
userInfo && userInfo?.is_admin ? '/dashboard' : '/playground';
|
||||||
@@ -140,7 +127,9 @@ const LoginForm = () => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
callGetRememberMe();
|
useEffect(() => {
|
||||||
|
callGetRememberMe();
|
||||||
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
@@ -160,7 +149,7 @@ const LoginForm = () => {
|
|||||||
style={{ width: '400px', margin: '0 auto' }}
|
style={{ width: '400px', margin: '0 auto' }}
|
||||||
onFinish={handleLogin}
|
onFinish={handleLogin}
|
||||||
>
|
>
|
||||||
{renderWelCome(intl)}
|
{renderWelCome}
|
||||||
<Form.Item
|
<Form.Item
|
||||||
name="username"
|
name="username"
|
||||||
rules={[
|
rules={[
|
||||||
@@ -218,4 +207,4 @@ const LoginForm = () => {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
export default LoginForm;
|
export default memo(LoginForm);
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { userAtom } from '@/atoms/user';
|
import { userAtom } from '@/atoms/user';
|
||||||
import Footer from '@/components/footer';
|
import Footer from '@/components/footer';
|
||||||
import { history } from '@umijs/max';
|
import { history, useModel } from '@umijs/max';
|
||||||
import { useAtom } from 'jotai';
|
import { useAtom } from 'jotai';
|
||||||
import { useEffect } from 'react';
|
import { useEffect } from 'react';
|
||||||
import LoginForm from './components/login-form';
|
import LoginForm from './components/login-form';
|
||||||
@@ -9,11 +9,18 @@ import styles from './components/styles.less';
|
|||||||
|
|
||||||
const Login = () => {
|
const Login = () => {
|
||||||
const [userInfo, setUserInfo] = useAtom(userAtom);
|
const [userInfo, setUserInfo] = useAtom(userAtom);
|
||||||
|
const { initialState, setInitialState } = useModel('@@initialState') || {};
|
||||||
|
|
||||||
const gotoDefaultPage = (userInfo: any) => {
|
const gotoDefaultPage = (userInfo: any) => {
|
||||||
if (!userInfo || userInfo?.require_password_change) {
|
if (!userInfo || userInfo?.require_password_change) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (!initialState?.currentUser) {
|
||||||
|
setInitialState((s: any) => ({
|
||||||
|
...s,
|
||||||
|
currentUser: userInfo
|
||||||
|
}));
|
||||||
|
}
|
||||||
const pathname = userInfo?.is_admin ? '/dashboard' : '/playground';
|
const pathname = userInfo?.is_admin ? '/dashboard' : '/playground';
|
||||||
|
|
||||||
history.push(pathname, { replace: true });
|
history.push(pathname, { replace: true });
|
||||||
|
|||||||
Reference in New Issue
Block a user