Files
scrm.antd/src/pages/User/Login/index.tsx

273 lines
8.0 KiB
TypeScript
Raw Normal View History

2023-04-04 10:56:33 +08:00
import Footer from '@/components/Footer';
2023-04-07 17:38:15 +08:00
import { IAjaxReturn, post } from '@/services/ajax';
2023-04-18 17:28:06 +08:00
import { LockOutlined, MobileOutlined, UserOutlined } from '@ant-design/icons';
2023-04-04 10:56:33 +08:00
import {
LoginForm,
ProFormCaptcha,
ProFormCheckbox,
ProFormText,
} from '@ant-design/pro-components';
import { useEmotionCss } from '@ant-design/use-emotion-css';
2023-04-18 17:28:06 +08:00
import { FormattedMessage, Helmet, history, useModel } from '@umijs/max';
2023-04-18 09:57:53 +08:00
import { Alert, App, Tabs } from 'antd';
2023-04-07 17:38:15 +08:00
import { stringify as qsStringify } from 'qs';
2023-04-04 10:56:33 +08:00
import React, { useState } from 'react';
import { flushSync } from 'react-dom';
2023-04-07 17:38:15 +08:00
import Settings from '../../../../config/defaultSettings';
2023-04-04 10:56:33 +08:00
const LoginMessage: React.FC<{
content: string;
}> = ({ content }) => {
return (
<Alert
style={{
marginBottom: 24,
}}
message={content}
type="error"
showIcon
/>
);
};
const Login: React.FC = () => {
const [userLoginState, setUserLoginState] = useState<API.LoginResult>({});
const [type, setType] = useState<string>('account');
const { initialState, setInitialState } = useModel('@@initialState');
const containerClassName = useEmotionCss(() => {
return {
display: 'flex',
flexDirection: 'column',
height: '100vh',
overflow: 'auto',
backgroundImage:
"url('https://mdn.alipayobjects.com/yuyan_qk0oxh/afts/img/V-_oS6r-i7wAAAAAAAAAAAAAFl94AQBr')",
backgroundSize: '100% 100%',
};
});
2023-04-07 17:38:15 +08:00
const { notification } = App.useApp();
window.NotificationCF = notification;
2023-04-04 10:56:33 +08:00
const handleSubmit = async (values: API.LoginParams) => {
2023-04-07 17:38:15 +08:00
const data = {
user: values.username,
password: values.password,
};
post({ url: '/User/Login', data: qsStringify(data) }).then((res: IAjaxReturn) => {
if (res && res.err_code == 0) {
notification.success({
message: '登录成功',
description: '登录成功',
});
flushSync(() => {
// 登录成功!
setInitialState((s) => ({
...s,
currentUser: res.data,
currentAuthority: 'admin',
}));
2023-04-04 10:56:33 +08:00
});
const urlParams = new URL(window.location.href).searchParams;
history.push(urlParams.get('redirect') || '/');
}
2023-04-07 17:38:15 +08:00
});
2023-04-04 10:56:33 +08:00
};
const { status, type: loginType } = userLoginState;
return (
<div className={containerClassName}>
<Helmet>
2023-04-18 09:57:53 +08:00
<title> - {Settings.title}</title>
2023-04-04 10:56:33 +08:00
</Helmet>
2023-04-07 17:38:15 +08:00
{/* <Lang /> */}
2023-04-04 10:56:33 +08:00
<div
style={{
flex: '1',
padding: '32px 0',
}}
>
<LoginForm
contentStyle={{
minWidth: 280,
maxWidth: '75vw',
}}
logo={<img alt="logo" src="/logo.svg" />}
2023-04-07 17:38:15 +08:00
title="scrm.antd"
subTitle={<div style={{ height: 30 }}></div>}
2023-04-04 10:56:33 +08:00
initialValues={{
autoLogin: true,
}}
2023-04-07 17:38:15 +08:00
// actions={[
// <FormattedMessage
// key="loginWith"
// id="pages.login.loginWith"
// defaultMessage="其他登录方式"
// />,
// <ActionIcons key="icons" />,
// ]}
2023-04-04 10:56:33 +08:00
onFinish={async (values) => {
await handleSubmit(values as API.LoginParams);
}}
>
<Tabs
activeKey={type}
onChange={setType}
centered
items={[
{
key: 'account',
2023-04-18 09:57:53 +08:00
label: '账户密码登录',
2023-04-04 10:56:33 +08:00
},
2023-04-07 17:38:15 +08:00
// {
// key: 'mobile',
2023-04-18 09:57:53 +08:00
// label: '手机号登录',
2023-04-07 17:38:15 +08:00
// },
2023-04-04 10:56:33 +08:00
]}
/>
{status === 'error' && loginType === 'account' && (
2023-04-18 09:57:53 +08:00
<LoginMessage content={'账户或密码错误(admin/ant.design)'} />
2023-04-04 10:56:33 +08:00
)}
{type === 'account' && (
<>
<ProFormText
name="username"
fieldProps={{
size: 'large',
prefix: <UserOutlined />,
}}
2023-04-18 09:57:53 +08:00
placeholder={'用户名'}
2023-04-04 10:56:33 +08:00
rules={[
{
required: true,
message: (
<FormattedMessage
id="pages.login.username.required"
defaultMessage="请输入用户名!"
/>
),
},
]}
/>
<ProFormText.Password
name="password"
fieldProps={{
size: 'large',
prefix: <LockOutlined />,
}}
2023-04-18 09:57:53 +08:00
placeholder={'请输入密码'}
2023-04-04 10:56:33 +08:00
rules={[
{
required: true,
message: (
<FormattedMessage
id="pages.login.password.required"
defaultMessage="请输入密码!"
/>
),
},
]}
/>
</>
)}
{status === 'error' && loginType === 'mobile' && <LoginMessage content="验证码错误" />}
{type === 'mobile' && (
<>
<ProFormText
fieldProps={{
size: 'large',
prefix: <MobileOutlined />,
}}
name="mobile"
2023-04-18 09:57:53 +08:00
placeholder={'手机号'}
2023-04-04 10:56:33 +08:00
rules={[
{
required: true,
message: (
<FormattedMessage
id="pages.login.phoneNumber.required"
defaultMessage="请输入手机号!"
/>
),
},
{
pattern: /^1\d{10}$/,
message: (
<FormattedMessage
id="pages.login.phoneNumber.invalid"
defaultMessage="手机号格式错误!"
/>
),
},
]}
/>
<ProFormCaptcha
fieldProps={{
size: 'large',
prefix: <LockOutlined />,
}}
captchaProps={{
size: 'large',
}}
2023-04-18 09:57:53 +08:00
placeholder={'请输入验证码'}
2023-04-04 10:56:33 +08:00
captchaTextRender={(timing, count) => {
if (timing) {
2023-04-18 09:57:53 +08:00
return `${count} 获取验证码`;
2023-04-04 10:56:33 +08:00
}
2023-04-18 09:57:53 +08:00
return '获取验证码';
2023-04-04 10:56:33 +08:00
}}
name="captcha"
rules={[
{
required: true,
message: (
<FormattedMessage
id="pages.login.captcha.required"
defaultMessage="请输入验证码!"
/>
),
},
]}
onGetCaptcha={async (phone) => {
2023-04-18 09:57:53 +08:00
// const result = await getFakeCaptcha({
// phone,
// });
// if (!result) {
// return;
// }
// message.success('获取验证码成功验证码为1234');
2023-04-04 10:56:33 +08:00
}}
/>
</>
)}
<div
style={{
marginBottom: 24,
}}
>
<ProFormCheckbox noStyle name="autoLogin">
<FormattedMessage id="pages.login.rememberMe" defaultMessage="自动登录" />
</ProFormCheckbox>
<a
style={{
float: 'right',
}}
>
<FormattedMessage id="pages.login.forgotPassword" defaultMessage="忘记密码" />
</a>
</div>
</LoginForm>
</div>
<Footer />
2023-04-07 17:38:15 +08:00
<div style={{ height: 12 }}></div>
2023-04-04 10:56:33 +08:00
</div>
);
};
export default Login;