flat: 初始化
This commit is contained in:
@@ -21,7 +21,7 @@ export function setSessionToken(
|
||||
access_token: string | undefined,
|
||||
refresh_token: string | undefined,
|
||||
expireTime: number,
|
||||
): void {
|
||||
): void{
|
||||
if (access_token) {
|
||||
localStorage.setItem('access_token', access_token);
|
||||
} else {
|
||||
|
||||
64
src/app.tsx
64
src/app.tsx
@@ -9,10 +9,29 @@ import { errorConfig } from './requestErrorConfig';
|
||||
import { clearSessionToken, getAccessToken, getRefreshToken, getTokenExpireTime } from './access';
|
||||
import { getRemoteMenu, getRoutersInfo, getUserInfo, patchRouteWithRemoteMenus, setRemoteMenu } from './services/session';
|
||||
import { PageEnum } from './enums/pagesEnums';
|
||||
|
||||
import {logout} from "@/services/system/auth";
|
||||
import { stringify } from 'querystring';
|
||||
|
||||
const isDev = process.env.NODE_ENV === 'development';
|
||||
|
||||
const loginOut = async () => {
|
||||
await logout();
|
||||
clearSessionToken();
|
||||
setRemoteMenu(null);
|
||||
const { search, pathname } = window.location;
|
||||
const urlParams = new URL(window.location.href).searchParams;
|
||||
/** 此方法会跳转到 redirect 参数所在的位置 */
|
||||
const redirect = urlParams.get('redirect');
|
||||
// Note: There may be security issues, please note
|
||||
if (window.location.pathname !== '/user/login' && !redirect) {
|
||||
history.replace({
|
||||
pathname: '/user/login',
|
||||
search: stringify({
|
||||
redirect: pathname + search,
|
||||
}),
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
@@ -63,7 +82,8 @@ export async function getInitialState(): Promise<{
|
||||
// ProLayout 支持的api https://procomponents.ant.design/components/layout
|
||||
export const layout: RunTimeLayoutConfig = ({ initialState, setInitialState }) => {
|
||||
return {
|
||||
actionsRender: () => [<Question key="doc" />, <SelectLang key="SelectLang" />],
|
||||
// actionsRender: () => [<Question key="doc" />, <SelectLang key="SelectLang" />],
|
||||
actionsRender: () => [ <SelectLang key="SelectLang" />],
|
||||
avatarProps: {
|
||||
src: initialState?.currentUser?.avatar,
|
||||
title: <AvatarName />,
|
||||
@@ -115,14 +135,14 @@ export const layout: RunTimeLayoutConfig = ({ initialState, setInitialState }) =
|
||||
width: '331px',
|
||||
},
|
||||
],
|
||||
links: isDev
|
||||
? [
|
||||
<Link key="openapi" to="/umi/plugin/openapi" target="_blank">
|
||||
<LinkOutlined />
|
||||
<span>OpenAPI 文档</span>
|
||||
</Link>,
|
||||
]
|
||||
: [],
|
||||
// links: isDev
|
||||
// ? [
|
||||
// <Link key="openapi" to="/umi/plugin/openapi" target="_blank">
|
||||
// <LinkOutlined />
|
||||
// <span>OpenAPI 文档</span>
|
||||
// </Link>,
|
||||
// ]
|
||||
// : [],
|
||||
menuHeaderRender: undefined,
|
||||
// 自定义 403 页面
|
||||
// unAccessible: <div>unAccessible</div>,
|
||||
@@ -169,14 +189,14 @@ export async function patchClientRoutes({ routes }) {
|
||||
patchRouteWithRemoteMenus(routes);
|
||||
}
|
||||
|
||||
export function render(oldRender: () => void) {
|
||||
export async function render(oldRender: () => void) {
|
||||
// console.log('render get routers', oldRender)
|
||||
const token = getAccessToken();
|
||||
if(!token || token?.length === 0) {
|
||||
oldRender();
|
||||
return;
|
||||
}
|
||||
getRoutersInfo().then(res => {
|
||||
await getRoutersInfo().then(res => {
|
||||
setRemoteMenu(res);
|
||||
oldRender()
|
||||
});
|
||||
@@ -220,14 +240,16 @@ export const request = {
|
||||
},
|
||||
],
|
||||
responseInterceptors: [
|
||||
// (response) =>
|
||||
// {
|
||||
// // // 不再需要异步处理读取返回体内容,可直接在data中读出,部分字段可在 config 中找到
|
||||
// // const { data = {} as any, config } = response;
|
||||
// // // do something
|
||||
// // console.log('data: ', data)
|
||||
// // console.log('config: ', config)
|
||||
// return response
|
||||
// },
|
||||
(response) => {
|
||||
// 不再需要异步处理读取返回体内容,可直接在data中读出,部分字段可在 config 中找到
|
||||
const { data = {} as any, config } = response;
|
||||
switch (data.code) {
|
||||
case 401:
|
||||
loginOut()
|
||||
}
|
||||
// console.log('data: ', data)
|
||||
// console.log('config: ', config)
|
||||
return response
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
@@ -10,22 +10,9 @@ const Footer: React.FC = () => {
|
||||
}}
|
||||
links={[
|
||||
{
|
||||
key: 'Ant Design Pro',
|
||||
title: 'Ant Design Pro',
|
||||
href: 'https://pro.ant.design',
|
||||
blankTarget: true,
|
||||
},
|
||||
{
|
||||
key: 'github',
|
||||
title: <GithubOutlined />,
|
||||
href: 'https://github.com/ant-design/ant-design-pro',
|
||||
blankTarget: true,
|
||||
},
|
||||
{
|
||||
key: 'Ant Design',
|
||||
title: 'Ant Design',
|
||||
href: 'https://ant.design',
|
||||
blankTarget: true,
|
||||
key: '青岛智慧就业服务系统',
|
||||
title: '青岛智慧就业服务系统',
|
||||
href: 'http://localhost:8000/',
|
||||
},
|
||||
]}
|
||||
/>
|
||||
|
||||
@@ -135,7 +135,6 @@ const Login: React.FC = () => {
|
||||
});
|
||||
const current = new Date();
|
||||
const expireTime = current.setTime(current.getTime() + 1000 * 12 * 60 * 60);
|
||||
console.log('login response: ', response);
|
||||
setSessionToken(response?.token, response?.token, expireTime);
|
||||
message.success(defaultLoginSuccessMessage);
|
||||
await fetchUserInfo();
|
||||
@@ -144,18 +143,19 @@ const Login: React.FC = () => {
|
||||
history.push(urlParams.get('redirect') || '/');
|
||||
return;
|
||||
} else {
|
||||
console.log(response.msg);
|
||||
message.error(response.msg);
|
||||
clearSessionToken();
|
||||
// 如果失败去设置用户错误信息
|
||||
setUserLoginState({ ...response, type });
|
||||
getCaptchaCode();
|
||||
}
|
||||
} catch (error) {
|
||||
// message.error(response.msg);
|
||||
const defaultLoginFailureMessage = intl.formatMessage({
|
||||
id: 'pages.login.failure',
|
||||
defaultMessage: '登录失败,请重试!',
|
||||
});
|
||||
console.log(error);
|
||||
// console.log(error);
|
||||
message.error(defaultLoginFailureMessage);
|
||||
}
|
||||
};
|
||||
@@ -190,19 +190,19 @@ const Login: React.FC = () => {
|
||||
maxWidth: '75vw',
|
||||
}}
|
||||
logo={<img alt="logo" src="/logo.svg" />}
|
||||
title="Ant Design"
|
||||
subTitle={intl.formatMessage({ id: 'pages.layouts.userLayout.title' })}
|
||||
title="青岛智慧就业服务系统"
|
||||
// subTitle={intl.formatMessage({ id: 'pages.layouts.userLayout.title' })}
|
||||
initialValues={{
|
||||
autoLogin: true,
|
||||
}}
|
||||
actions={[
|
||||
<FormattedMessage
|
||||
key="loginWith"
|
||||
id="pages.login.loginWith"
|
||||
defaultMessage="其他登录方式"
|
||||
/>,
|
||||
<ActionIcons key="icons" />,
|
||||
]}
|
||||
// actions={[
|
||||
// <FormattedMessage
|
||||
// key="loginWith"
|
||||
// id="pages.login.loginWith"
|
||||
// defaultMessage="其他登录方式"
|
||||
// />,
|
||||
// <ActionIcons key="icons" />,
|
||||
// ]}
|
||||
onFinish={async (values) => {
|
||||
await handleSubmit(values as API.LoginParams);
|
||||
}}
|
||||
@@ -229,14 +229,14 @@ const Login: React.FC = () => {
|
||||
]}
|
||||
/>
|
||||
|
||||
{code !== 200 && loginType === 'account' && (
|
||||
<LoginMessage
|
||||
content={intl.formatMessage({
|
||||
id: 'pages.login.accountLogin.errorMessage',
|
||||
defaultMessage: '账户或密码错误(admin/admin123)',
|
||||
})}
|
||||
/>
|
||||
)}
|
||||
{/*{code !== 200 && loginType === 'account' && (*/}
|
||||
{/* <LoginMessage*/}
|
||||
{/* content={intl.formatMessage({*/}
|
||||
{/* id: 'pages.login.accountLogin.errorMessage',*/}
|
||||
{/* defaultMessage: '账户或密码错误(admin/admin123)',*/}
|
||||
{/* })}*/}
|
||||
{/* />*/}
|
||||
{/*)}*/}
|
||||
{type === 'account' && (
|
||||
<>
|
||||
<ProFormText
|
||||
|
||||
@@ -29,6 +29,7 @@ export const errorConfig: RequestConfig = {
|
||||
errorConfig: {
|
||||
// 错误抛出
|
||||
errorThrower: (res) => {
|
||||
console.log('errorThrower')
|
||||
const { success, data, errorCode, errorMessage, showType } =
|
||||
res as unknown as ResponseStructure;
|
||||
if (!success) {
|
||||
@@ -99,7 +100,6 @@ export const errorConfig: RequestConfig = {
|
||||
(response) => {
|
||||
// 拦截响应数据,进行个性化处理
|
||||
const { data } = response as unknown as ResponseStructure;
|
||||
console.log(data)
|
||||
if (data?.success === false) {
|
||||
message.error('请求失败!');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user