index.tsx 6.63 KB
Newer Older
1 2
import React, { Component } from 'react';
import { connect } from 'dva';
陈帅's avatar
陈帅 committed
3
import { formatMessage, FormattedMessage } from 'umi-plugin-react/locale';
zinkey's avatar
zinkey committed
4
import Link from 'umi/link';
ddcat1115's avatar
ddcat1115 committed
5
import { Checkbox, Alert, Icon } from 'antd';
陈帅's avatar
陈帅 committed
6
import Login from './components/Login';
7
import styles from './style.less';
陈帅's avatar
陈帅 committed
8 9 10 11
import { Dispatch } from 'redux';
import { IStateType } from './model';
import { FormComponentProps } from 'antd/lib/form';
import { CheckboxChangeEvent } from 'antd/lib/checkbox';
陈帅's avatar
陈帅 committed
12

ddcat1115's avatar
ddcat1115 committed
13
const { Tab, UserName, Password, Mobile, Captcha, Submit } = Login;
14

陈帅's avatar
陈帅 committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
interface BLOCK_NAME_CAMEL_CASEProps {
  dispatch: Dispatch;
  BLOCK_NAME_CAMEL_CASE: IStateType;
  submitting: boolean;
}
interface BLOCK_NAME_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 BLOCK_NAME_CAMEL_CASE extends Component<
  BLOCK_NAME_CAMEL_CASEProps,
  BLOCK_NAME_CAMEL_CASEState
> {
  state: BLOCK_NAME_CAMEL_CASEState = {
52
    type: 'account',
ddcat1115's avatar
ddcat1115 committed
53
    autoLogin: true,
jim's avatar
jim committed
54
  };
55

陈帅's avatar
陈帅 committed
56
  onTabChange = (type: string) => {
afc163's avatar
afc163 committed
57
    this.setState({ type });
jim's avatar
jim committed
58
  };
陈帅's avatar
陈帅 committed
59
  loginForm: FormComponentProps['form'] | undefined | null;
60 61
  onGetCaptcha = () =>
    new Promise((resolve, reject) => {
陈帅's avatar
陈帅 committed
62 63 64 65
      if (!this.loginForm) {
        return;
      }
      this.loginForm.validateFields(['mobile'], {}, (err: any, values: FromDataType) => {
66 67 68
        if (err) {
          reject(err);
        } else {
陈帅's avatar
陈帅 committed
69
          const { dispatch } = this.props;
陈帅's avatar
陈帅 committed
70
          ((dispatch({
71
            type: 'BLOCK_NAME_CAMEL_CASE/getCaptcha',
陈帅's avatar
陈帅 committed
72
            payload: values.mobile,
陈帅's avatar
陈帅 committed
73
          }) as unknown) as Promise<any>)
74 75 76 77 78
            .then(resolve)
            .catch(reject);
        }
      });
    });
陈帅's avatar
陈帅 committed
79

陈帅's avatar
陈帅 committed
80
  handleSubmit = (err: any, values: FromDataType) => {
ddcat1115's avatar
ddcat1115 committed
81 82
    const { type } = this.state;
    if (!err) {
陈帅's avatar
陈帅 committed
83 84
      const { dispatch } = this.props;
      dispatch({
85
        type: 'BLOCK_NAME_CAMEL_CASE/login',
ddcat1115's avatar
ddcat1115 committed
86 87 88 89 90 91
        payload: {
          ...values,
          type,
        },
      });
    }
jim's avatar
jim committed
92
  };
93

陈帅's avatar
陈帅 committed
94
  changeAutoLogin = (e: CheckboxChangeEvent) => {
ddcat1115's avatar
ddcat1115 committed
95 96 97
    this.setState({
      autoLogin: e.target.checked,
    });
jim's avatar
jim committed
98
  };
99

陈帅's avatar
陈帅 committed
100
  renderMessage = (content: string) => (
101 102
    <Alert style={{ marginBottom: 24 }} message={content} type="error" showIcon />
  );
103 104

  render() {
105 106
    const { BLOCK_NAME_CAMEL_CASE, submitting } = this.props;
    const { status, type: loginType } = BLOCK_NAME_CAMEL_CASE;
陈帅's avatar
陈帅 committed
107
    const { type, autoLogin } = this.state;
108 109
    return (
      <div className={styles.main}>
110 111 112 113
        <Login
          defaultActiveKey={type}
          onTabChange={this.onTabChange}
          onSubmit={this.handleSubmit}
陈帅's avatar
陈帅 committed
114
          ref={(form: any) => {
115 116 117
            this.loginForm = form;
          }}
        >
118 119 120
          <Tab key="account" tab={formatMessage({ id: 'BLOCK_NAME.login.tab-login-credentials' })}>
            {status === 'error' &&
              loginType === 'account' &&
Amumu's avatar
Amumu committed
121
              !submitting &&
xiaohuoni's avatar
xiaohuoni committed
122 123 124
              this.renderMessage(
                formatMessage({ id: 'BLOCK_NAME.login.message-invalid-credentials' })
              )}
125 126
            <UserName
              name="userName"
127
              placeholder={`${formatMessage({ id: 'BLOCK_NAME.login.userName' })}: admin or user`}
128 129 130
              rules={[
                {
                  required: true,
131
                  message: formatMessage({ id: 'BLOCK_NAME.userName.required' }),
132 133
                },
              ]}
陈帅's avatar
陈帅 committed
134
            />
陈帅's avatar
陈帅 committed
135 136
            <Password
              name="password"
137
              placeholder={`${formatMessage({ id: 'BLOCK_NAME.login.password' })}: ant.design`}
138 139 140
              rules={[
                {
                  required: true,
141
                  message: formatMessage({ id: 'BLOCK_NAME.password.required' }),
142 143
                },
              ]}
陈帅's avatar
陈帅 committed
144 145 146
              onPressEnter={() =>
                this.loginForm && this.loginForm.validateFields(this.handleSubmit)
              }
陈帅's avatar
陈帅 committed
147
            />
ddcat1115's avatar
ddcat1115 committed
148
          </Tab>
149 150 151
          <Tab key="mobile" tab={formatMessage({ id: 'BLOCK_NAME.login.tab-login-mobile' })}>
            {status === 'error' &&
              loginType === 'mobile' &&
Amumu's avatar
Amumu committed
152
              !submitting &&
Rayron Victor's avatar
Rayron Victor committed
153
              this.renderMessage(
154
                formatMessage({ id: 'BLOCK_NAME.login.message-invalid-verification-code' })
Rayron Victor's avatar
Rayron Victor committed
155
              )}
陈帅's avatar
陈帅 committed
156
            <Mobile
157
              name="mobile"
158
              placeholder={formatMessage({ id: 'BLOCK_NAME.phone-number.placeholder' })}
159 160 161
              rules={[
                {
                  required: true,
162
                  message: formatMessage({ id: 'BLOCK_NAME.phone-number.required' }),
163 164 165
                },
                {
                  pattern: /^1\d{10}$/,
166
                  message: formatMessage({ id: 'BLOCK_NAME.phone-number.wrong-format' }),
167 168 169
                },
              ]}
            />
陈帅's avatar
陈帅 committed
170 171
            <Captcha
              name="captcha"
172
              placeholder={formatMessage({ id: 'BLOCK_NAME.verification-code.placeholder' })}
陈帅's avatar
陈帅 committed
173 174
              countDown={120}
              onGetCaptcha={this.onGetCaptcha}
175 176
              getCaptchaButtonText={formatMessage({ id: 'BLOCK_NAME.form.get-captcha' })}
              getCaptchaSecondText={formatMessage({ id: 'BLOCK_NAME.captcha.second' })}
177 178 179
              rules={[
                {
                  required: true,
180
                  message: formatMessage({ id: 'BLOCK_NAME.verification-code.required' }),
181 182
                },
              ]}
陈帅's avatar
陈帅 committed
183
            />
ddcat1115's avatar
ddcat1115 committed
184 185
          </Tab>
          <div>
陈帅's avatar
陈帅 committed
186
            <Checkbox checked={autoLogin} onChange={this.changeAutoLogin}>
187
              <FormattedMessage id="BLOCK_NAME.login.remember-me" />
jim's avatar
jim committed
188 189
            </Checkbox>
            <a style={{ float: 'right' }} href="">
190
              <FormattedMessage id="BLOCK_NAME.login.forgot-password" />
jim's avatar
jim committed
191
            </a>
ddcat1115's avatar
ddcat1115 committed
192
          </div>
193
          <Submit loading={submitting}>
194
            <FormattedMessage id="BLOCK_NAME.login.login" />
195
          </Submit>
ddcat1115's avatar
ddcat1115 committed
196
          <div className={styles.other}>
197
            <FormattedMessage id="BLOCK_NAME.login.sign-in-with" />
陈帅's avatar
陈帅 committed
198 199 200
            <Icon type="alipay-circle" className={styles.icon} theme="outlined" />
            <Icon type="taobao-circle" className={styles.icon} theme="outlined" />
            <Icon type="weibo-circle" className={styles.icon} theme="outlined" />
陈帅's avatar
陈帅 committed
201
            <Link className={styles.register} to="/user/register">
202
              <FormattedMessage id="BLOCK_NAME.login.signup" />
jim's avatar
jim committed
203
            </Link>
ddcat1115's avatar
ddcat1115 committed
204 205
          </div>
        </Login>
206 207 208 209
      </div>
    );
  }
}
陈帅's avatar
陈帅 committed
210

陈帅's avatar
陈帅 committed
211
export default BLOCK_NAME_CAMEL_CASE;