import React, { Fragment, useEffect, useState } from 'react'; import { Button, Row, Input, Form, message, notification, Statistic, } from 'antd'; import config from '@/utils/config'; import { history, Link } from 'umi'; import styles from './index.less'; import { request } from '@/utils/http'; import { useTheme } from '@/utils/hooks'; import { MobileOutlined } from '@ant-design/icons'; const FormItem = Form.Item; const { Countdown } = Statistic; const Login = () => { const [loading, setLoading] = useState(false); const [waitTime, setWaitTime] = useState(); const { theme } = useTheme(); const [twoFactor, setTwoFactor] = useState(false); const [verifying, setVerifying] = useState(false); const [loginInfo, setLoginInfo] = useState(); const handleOk = (values: any) => { setLoading(true); setTwoFactor(false); setWaitTime(null); request .post(`${config.apiPrefix}login`, { data: { username: values.username, password: values.password, }, }) .then((data) => { if (data.code === 420) { setLoginInfo({ username: values.username, password: values.password, }); setTwoFactor(true); } else { checkResponse(data); } setLoading(false); }) .catch(function (error) { console.log(error); setLoading(false); }); }; const completeTowFactor = (values: any) => { setVerifying(true); request .put(`${config.apiPrefix}user/two-factor/login`, { data: { ...loginInfo, code: values.code }, }) .then((data: any) => { if (data.code === 430) { message.error(data.message); } else { checkResponse(data); } setVerifying(false); }) .catch((error: any) => { console.log(error); setVerifying(false); }); }; const checkResponse = (data: any) => { if (data.code === 200) { const { token, lastip, lastaddr, lastlogon, retries = 0, platform, } = data.data; localStorage.setItem(config.authKey, token); notification.success({ message: '登录成功!', description: ( <>
上次登录时间: {lastlogon ? new Date(lastlogon).toLocaleString() : '-'}
上次登录地点:{lastaddr || '-'}
上次登录IP:{lastip || '-'}
上次登录设备:{platform || '-'}
上次登录状态:{retries > 0 ? `失败${retries}次` : '成功'}
), duration: 5, }); history.push('/crontab'); } else if (data.code === 100) { message.warn(data.message); } else if (data.code === 410) { message.error(data.message); setWaitTime(data.data); } else { message.error(data.message); } }; useEffect(() => { const isAuth = localStorage.getItem(config.authKey); if (isAuth) { history.push('/crontab'); } }, []); return (
logo {twoFactor ? '两步验证' : config.siteName}
{twoFactor ? (
) : (
{waitTime ? ( ) : ( )}
)}
{twoFactor ? (
在您的设备上打开两步验证应用程序以查看您的身份验证代码并验证您的身份。
) : ( '' )}
); }; export default Login;