import { Alert, Checkbox, Icon } from 'antd'; import { FormattedMessage, formatMessage } from 'umi-plugin-react/locale'; import React, { Component } from 'react'; import { CheckboxChangeEvent } from 'antd/es/checkbox'; import { Dispatch } from 'redux'; import { FormComponentProps } from 'antd/es/form'; import Link from 'umi/link'; import { connect } from 'dva'; import { StateType } from './model'; import LoginComponents from './components/Login'; import styles from './style.less'; const { Tab, UserName, Password, Mobile, Captcha, Submit } = LoginComponents; interface LoginProps { dispatch: Dispatch; userLogin: StateType; submitting: boolean; } interface LoginState { type: string; autoLogin: boolean; } export interface FromDataType { userName: string; password: string; mobile: string; captcha: string; } @connect( ({ userLogin, loading, }: { userLogin: StateType; loading: { effects: { [key: string]: string; }; }; }) => ({ userLogin, submitting: loading.effects['userLogin/login'], }), ) class Login extends Component { loginForm: FormComponentProps['form'] | undefined | null = undefined; state: LoginState = { type: 'account', autoLogin: true, }; changeAutoLogin = (e: CheckboxChangeEvent) => { this.setState({ autoLogin: e.target.checked, }); }; handleSubmit = (err: any, values: FromDataType) => { const { type } = this.state; if (!err) { const { dispatch } = this.props; dispatch({ type: 'userLogin/login', payload: { ...values, type, }, }); } }; onTabChange = (type: string) => { this.setState({ type }); }; onGetCaptcha = () => new Promise((resolve, reject) => { if (!this.loginForm) { return; } this.loginForm.validateFields(['mobile'], {}, (err: any, values: FromDataType) => { if (err) { reject(err); } else { const { dispatch } = this.props; ((dispatch({ type: 'userLogin/getCaptcha', payload: values.mobile, }) as unknown) as Promise) .then(resolve) .catch(reject); } }); }); renderMessage = (content: string) => ( ); render() { const { userLogin, submitting } = this.props; const { status, type: loginType } = userLogin; const { type, autoLogin } = this.state; return (
{ this.loginForm = form; }} > {status === 'error' && loginType === 'account' && !submitting && this.renderMessage( formatMessage({ id: 'user-login.login.message-invalid-credentials' }), )} this.loginForm && this.loginForm.validateFields(this.handleSubmit) } /> {status === 'error' && loginType === 'mobile' && !submitting && this.renderMessage( formatMessage({ id: 'user-login.login.message-invalid-verification-code' }), )}
); } } export default Login;