Login.js 5.5 KB
Newer Older
1 2
import React, { Component } from 'react';
import { connect } from 'dva';
陈小聪 已提交
3
import { formatMessage, FormattedMessage } from 'umi-plugin-react/locale';
Z
zinkey 已提交
4
import Link from 'umi/link';
陈帅 已提交
5
import { Checkbox, Alert, Modal, Icon } from 'antd';
6
import Login from '@/components/Login';
7 8
import styles from './Login.less';

D
ddcat1115 已提交
9
const { Tab, UserName, Password, Mobile, Captcha, Submit } = Login;
10

A
Andreas Cederström 已提交
11 12 13
@connect(({ login, loading }) => ({
  login,
  submitting: loading.effects['login/login'],
14
}))
A
afc163 已提交
15
class LoginPage extends Component {
16 17
  state = {
    type: 'account',
D
ddcat1115 已提交
18
    autoLogin: true,
J
jim 已提交
19
  };
20

J
jim 已提交
21
  onTabChange = type => {
A
afc163 已提交
22
    this.setState({ type });
J
jim 已提交
23
  };
24

25 26
  onGetCaptcha = () =>
    new Promise((resolve, reject) => {
27 28 29 30
      this.loginForm.validateFields(['mobile'], {}, (err, values) => {
        if (err) {
          reject(err);
        } else {
陈帅 已提交
31 32 33 34 35
          const { dispatch } = this.props;
          dispatch({
            type: 'login/getCaptcha',
            payload: values.mobile,
          })
36 37
            .then(resolve)
            .catch(reject);
陈帅 已提交
38 39 40 41

          Modal.info({
            title: formatMessage({ id: 'app.login.verification-code-warning' }),
          });
42 43 44
        }
      });
    });
陈帅 已提交
45

D
ddcat1115 已提交
46 47 48
  handleSubmit = (err, values) => {
    const { type } = this.state;
    if (!err) {
陈帅 已提交
49 50
      const { dispatch } = this.props;
      dispatch({
D
ddcat1115 已提交
51 52 53 54 55 56 57
        type: 'login/login',
        payload: {
          ...values,
          type,
        },
      });
    }
J
jim 已提交
58
  };
59

J
jim 已提交
60
  changeAutoLogin = e => {
D
ddcat1115 已提交
61 62 63
    this.setState({
      autoLogin: e.target.checked,
    });
J
jim 已提交
64
  };
65

66 67 68
  renderMessage = content => (
    <Alert style={{ marginBottom: 24 }} message={content} type="error" showIcon />
  );
69 70

  render() {
A
Andreas Cederström 已提交
71
    const { login, submitting } = this.props;
陈帅 已提交
72
    const { type, autoLogin } = this.state;
73 74
    return (
      <div className={styles.main}>
75 76 77 78 79 80 81 82
        <Login
          defaultActiveKey={type}
          onTabChange={this.onTabChange}
          onSubmit={this.handleSubmit}
          ref={form => {
            this.loginForm = form;
          }}
        >
83
          <Tab key="account" tab={formatMessage({ id: 'app.login.tab-login-credentials' })}>
J
jim 已提交
84
            {login.status === 'error' &&
D
ddcat1115 已提交
85
              login.type === 'account' &&
86
              !submitting &&
87
              this.renderMessage(formatMessage({ id: 'app.login.message-invalid-credentials' }))}
88 89 90 91 92 93 94 95 96 97
            <UserName
              name="userName"
              placeholder={`${formatMessage({ id: 'app.login.userName' })}: admin or user`}
              rules={[
                {
                  required: true,
                  message: formatMessage({ id: 'validation.userName.required' }),
                },
              ]}
            />
陈帅 已提交
98 99
            <Password
              name="password"
100 101 102 103 104 105 106
              placeholder={`${formatMessage({ id: 'app.login.password' })}: ant.design`}
              rules={[
                {
                  required: true,
                  message: formatMessage({ id: 'validation.password.required' }),
                },
              ]}
107 108 109 110
              onPressEnter={e => {
                e.preventDefault();
                this.loginForm.validateFields(this.handleSubmit);
              }}
陈帅 已提交
111
            />
D
ddcat1115 已提交
112
          </Tab>
113
          <Tab key="mobile" tab={formatMessage({ id: 'app.login.tab-login-mobile' })}>
J
jim 已提交
114
            {login.status === 'error' &&
D
ddcat1115 已提交
115
              login.type === 'mobile' &&
116
              !submitting &&
R
Rayron Victor 已提交
117 118 119
              this.renderMessage(
                formatMessage({ id: 'app.login.message-invalid-verification-code' })
              )}
120 121 122 123 124 125 126 127 128 129 130 131 132 133
            <Mobile
              name="mobile"
              placeholder={formatMessage({ id: 'form.phone-number.placeholder' })}
              rules={[
                {
                  required: true,
                  message: formatMessage({ id: 'validation.phone-number.required' }),
                },
                {
                  pattern: /^1\d{10}$/,
                  message: formatMessage({ id: 'validation.phone-number.wrong-format' }),
                },
              ]}
            />
陈帅 已提交
134 135
            <Captcha
              name="captcha"
136
              placeholder={formatMessage({ id: 'form.verification-code.placeholder' })}
陈帅 已提交
137 138
              countDown={120}
              onGetCaptcha={this.onGetCaptcha}
139
              getCaptchaButtonText={formatMessage({ id: 'form.get-captcha' })}
陈帅 已提交
140
              getCaptchaSecondText={formatMessage({ id: 'form.captcha.second' })}
141 142 143 144 145 146
              rules={[
                {
                  required: true,
                  message: formatMessage({ id: 'validation.verification-code.required' }),
                },
              ]}
陈帅 已提交
147
            />
D
ddcat1115 已提交
148 149
          </Tab>
          <div>
陈帅 已提交
150
            <Checkbox checked={autoLogin} onChange={this.changeAutoLogin}>
151
              <FormattedMessage id="app.login.remember-me" />
J
jim 已提交
152 153
            </Checkbox>
            <a style={{ float: 'right' }} href="">
154
              <FormattedMessage id="app.login.forgot-password" />
J
jim 已提交
155
            </a>
D
ddcat1115 已提交
156
          </div>
157 158 159
          <Submit loading={submitting}>
            <FormattedMessage id="app.login.login" />
          </Submit>
D
ddcat1115 已提交
160
          <div className={styles.other}>
161
            <FormattedMessage id="app.login.sign-in-with" />
陈帅 已提交
162 163 164
            <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" />
陈帅 已提交
165
            <Link className={styles.register} to="/user/register">
166
              <FormattedMessage id="app.login.signup" />
J
jim 已提交
167
            </Link>
D
ddcat1115 已提交
168 169
          </div>
        </Login>
170 171 172 173
      </div>
    );
  }
}
陈帅 已提交
174 175

export default LoginPage;