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