免登录处理
This commit is contained in:
parent
c7a389af19
commit
c403dadb93
94
src/app.tsx
94
src/app.tsx
@ -15,6 +15,9 @@ import defaultSettings from '../config/defaultSettings';
|
|||||||
import iconStyle from '../public/樽海鞘_图案.svg';
|
import iconStyle from '../public/樽海鞘_图案.svg';
|
||||||
import './modelstyle.css';
|
import './modelstyle.css';
|
||||||
import { getRoutersInfo, getUserInfo } from './services/session';
|
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 isDev = process.env.NODE_ENV === 'development';
|
||||||
const loginPath = '/user/login';
|
const loginPath = '/user/login';
|
||||||
|
|
||||||
@ -23,6 +26,46 @@ export const initialStateConfig = {
|
|||||||
loading: <PageLoading />,
|
loading: <PageLoading />,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
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
|
* @see https://umijs.org/zh-CN/plugins/plugin-initial-state
|
||||||
* */
|
* */
|
||||||
@ -30,45 +73,9 @@ export async function getInitialState(): Promise<{
|
|||||||
settings?: Partial<LayoutSettings>;
|
settings?: Partial<LayoutSettings>;
|
||||||
currentUser?: API.CurrentUser;
|
currentUser?: API.CurrentUser;
|
||||||
loading?: boolean;
|
loading?: boolean;
|
||||||
fetchUserInfo?: () => Promise<API.CurrentUser | undefined>;
|
fetchUserInfo?: (params: any) => Promise<API.CurrentUser | undefined>;
|
||||||
}> {
|
}> {
|
||||||
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 {
|
return {
|
||||||
fetchUserInfo,
|
fetchUserInfo,
|
||||||
settings: defaultSettings,
|
settings: defaultSettings,
|
||||||
@ -83,11 +90,9 @@ export const layout: RunTimeLayoutConfig = ({ initialState, setInitialState }) =
|
|||||||
content: initialState?.currentUser?.userName,
|
content: initialState?.currentUser?.userName,
|
||||||
},
|
},
|
||||||
// footerRender: () => <Footer />,
|
// footerRender: () => <Footer />,
|
||||||
onPageChange: () => {
|
onPageChange: async () => {
|
||||||
const { location } = history;
|
if (!initialState?.currentUser?.userId) {
|
||||||
// 如果没有登录,重定向到 login
|
fetchUserInfo?.(setInitialState);
|
||||||
if (!initialState?.currentUser && location.pathname !== loginPath) {
|
|
||||||
history.push(loginPath);
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
links: isDev
|
links: isDev
|
||||||
@ -110,6 +115,11 @@ export const layout: RunTimeLayoutConfig = ({ initialState, setInitialState }) =
|
|||||||
},
|
},
|
||||||
request: async () => {
|
request: async () => {
|
||||||
if (!initialState?.currentUser?.userId) {
|
if (!initialState?.currentUser?.userId) {
|
||||||
|
let path = window.location.pathname
|
||||||
|
if (window.location.search) {
|
||||||
|
path += '?hideInMenu=true'
|
||||||
|
}
|
||||||
|
window.localStorage.setItem("redirectPath", path)
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
// initialState.currentUser 中包含了所有用户信息
|
// initialState.currentUser 中包含了所有用户信息
|
||||||
|
@ -84,7 +84,7 @@ const Login: React.FC = () => {
|
|||||||
// 如果失败去设置用户错误信息
|
// 如果失败去设置用户错误信息
|
||||||
setUserLoginState({status: 'error', type: 'account', massage: response.msg});
|
setUserLoginState({status: 'error', type: 'account', massage: response.msg});
|
||||||
message.error(response.msg);
|
message.error(response.msg);
|
||||||
getCaptchaCode();
|
// getCaptchaCode();
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
clearSessionToken();
|
clearSessionToken();
|
||||||
@ -93,13 +93,13 @@ const Login: React.FC = () => {
|
|||||||
defaultMessage: '登录失败,请重试!',
|
defaultMessage: '登录失败,请重试!',
|
||||||
});
|
});
|
||||||
message.error(defaultLoginFailureMessage);
|
message.error(defaultLoginFailureMessage);
|
||||||
getCaptchaCode();
|
// getCaptchaCode();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
const { status, type: loginType, massage } = userLoginState;
|
const { status, type: loginType, massage } = userLoginState;
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
getCaptchaCode();
|
// getCaptchaCode();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
Loading…
x
Reference in New Issue
Block a user