import React, {Component} from 'react'; import {Button, Card, Checkbox, Form, Input, Modal, Typography} from "antd"; import './Login.css' import request from "../common/request"; import {message} from "antd/es"; import {withRouter} from "react-router-dom"; import {LockOutlined, OneToOneOutlined, UserOutlined} from '@ant-design/icons'; const {Title} = Typography; class LoginForm extends Component { formRef = React.createRef(); totpInputRef = React.createRef(); state = { inLogin: false, height: window.innerHeight, width: window.innerWidth, loginAccount: undefined, totpModalVisible: false, confirmLoading: false }; componentDidMount() { window.addEventListener('resize', () => { this.setState({ height: window.innerHeight, width: window.innerWidth }) }); } handleSubmit = async params => { this.setState({ inLogin: true }); try { let result = await request.post('/login', params); if (result.code === 0) { // 进行双因子认证 this.setState({ loginAccount: params, totpModalVisible: true }) this.totpInputRef.current.focus(); return; } if (result.code !== 1) { throw new Error(result.message); } // 跳转登录 sessionStorage.removeItem('current'); sessionStorage.removeItem('openKeys'); localStorage.setItem('X-Auth-Token', result['data']); // this.props.history.push(); window.location.href = "/" } catch (e) { message.error(e.message); } finally { this.setState({ inLogin: false }); } }; handleOk = async (values) => { this.setState({ confirmLoading: true }) let loginAccount = this.state.loginAccount; loginAccount['totp'] = values['totp']; try { let result = await request.post('/loginWithTotp', loginAccount); if (result['code'] !== 1) { message.error(result['message']); return; } // 跳转登录 sessionStorage.removeItem('current'); sessionStorage.removeItem('openKeys'); localStorage.setItem('X-Auth-Token', result['data']); // this.props.history.push(); window.location.href = "/" } catch (e) { message.error(e.message); } finally { this.setState({ confirmLoading: false }); } } handleCancel = () => { this.setState({ totpModalVisible: false }) } render() { return (
Next Terminal
} placeholder="登录账号"/> } placeholder="登录密码"/> 记住登录
{ this.formRef.current .validateFields() .then(values => { this.handleOk(values); // this.formRef.current.resetFields(); }) .catch(info => { }); }} onCancel={this.handleCancel}>
} placeholder="请输入双因素认证APP中显示的授权码"/>
); } } export default withRouter(LoginForm);