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

355 lines
10 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-04 10:56:33 +08:00
import {
AlipayCircleOutlined,
LockOutlined,
MobileOutlined,
TaobaoCircleOutlined,
UserOutlined,
WeiboCircleOutlined,
} from '@ant-design/icons';
import {
LoginForm,
ProFormCaptcha,
ProFormCheckbox,
ProFormText,
} from '@ant-design/pro-components';
import { useEmotionCss } from '@ant-design/use-emotion-css';
2023-04-18 09:57:53 +08:00
import { FormattedMessage, Helmet, history, SelectLang, useModel } from '@umijs/max';
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 ActionIcons = () => {
const langClassName = useEmotionCss(({ token }) => {
return {
marginLeft: '8px',
color: 'rgba(0, 0, 0, 0.2)',
fontSize: '24px',
verticalAlign: 'middle',
cursor: 'pointer',
transition: 'color 0.3s',
'&:hover': {
color: token.colorPrimaryActive,
},
};
});
return (
<>
<AlipayCircleOutlined key="AlipayCircleOutlined" className={langClassName} />
<TaobaoCircleOutlined key="TaobaoCircleOutlined" className={langClassName} />
<WeiboCircleOutlined key="WeiboCircleOutlined" className={langClassName} />
</>
);
};
const Lang = () => {
const langClassName = useEmotionCss(({ token }) => {
return {
width: 42,
height: 42,
lineHeight: '42px',
position: 'fixed',
right: 16,
borderRadius: token.borderRadius,
':hover': {
backgroundColor: token.colorBgTextHover,
},
};
});
return (
<div className={langClassName} data-lang>
{SelectLang && <SelectLang />}
</div>
);
};
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%',
};
});
const fetchUserInfo = async () => {
const userInfo = await initialState?.fetchUserInfo?.();
if (userInfo) {
flushSync(() => {
setInitialState((s) => ({
...s,
currentUser: userInfo,
}));
});
}
};
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',
}}
>
2023-04-07 17:38:15 +08:00
{/* <ChatTime msgtime={1680713999000}></ChatTime>
<ChatTime msgtime={1680763041000}></ChatTime>
<ChatTime msgtime={1680710399000}></ChatTime>
<ChatTime msgtime={1680623999000}></ChatTime>
<ChatTime msgtime={1680537599000}></ChatTime>
<ChatTime msgtime={1680451199000}></ChatTime>
<ChatTime msgtime={1680331041000}></ChatTime>
<ChatTime msgtime={1680195599000}></ChatTime>
<ChatTime msgtime={1680244641000}></ChatTime>
<ChatTime msgtime={1680191999000}></ChatTime>
<ChatTime msgtime={1680158241000}></ChatTime>
<ChatRevoke></ChatRevoke>
<ChatAgreeOrNot />
<ChatVoice></ChatVoice>
<ChatVideo></ChatVideo>
<ChatCard></ChatCard>
<ChatEmotion></ChatEmotion>
<ChatBar></ChatBar> */}
2023-04-04 10:56:33 +08:00
<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;