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';
zinkey's avatar
zinkey committed
4
import Link from 'umi/link';
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
5
import { Checkbox, Alert, message, Icon } from 'antd';
6
import Login from '@/components/Login';
7 8
import styles from './Login.less';

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

Andreas CederstrΓΆm's avatar
Andreas CederstrΓΆm committed
11 12 13
@connect(({ login, loading }) => ({
  login,
  submitting: loading.effects['login/login'],
14
}))
afc163's avatar
afc163 committed
15
class LoginPage extends Component {
16 17
  state = {
    type: 'account',
ddcat1115's avatar
ddcat1115 committed
18
    autoLogin: true,
jim's avatar
jim committed
19
  };
20

jim's avatar
jim committed
21
  onTabChange = type => {
afc163's avatar
afc163 committed
22
    this.setState({ type });
jim's avatar
jim committed
23
  };
24

25 26
  onGetCaptcha = () =>
    new Promise((resolve, reject) => {
27 28 29 30
      this.loginForm.validateFields(['mobile'], {}, (err, values) => {
        if (err) {
          reject(err);
        } else {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
31 32 33 34 35
          const { dispatch } = this.props;
          dispatch({
            type: 'login/getCaptcha',
            payload: values.mobile,
          })
36 37
            .then(resolve)
            .catch(reject);
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
38
          message.warning(formatMessage({ id: 'app.login.verification-code-warning' }));
39 40 41
        }
      });
    });
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
42

ddcat1115's avatar
ddcat1115 committed
43 44 45
  handleSubmit = (err, values) => {
    const { type } = this.state;
    if (!err) {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
46 47
      const { dispatch } = this.props;
      dispatch({
ddcat1115's avatar
ddcat1115 committed
48 49 50 51 52 53 54
        type: 'login/login',
        payload: {
          ...values,
          type,
        },
      });
    }
jim's avatar
jim committed
55
  };
56

jim's avatar
jim committed
57
  changeAutoLogin = e => {
ddcat1115's avatar
ddcat1115 committed
58 59 60
    this.setState({
      autoLogin: e.target.checked,
    });
jim's avatar
jim committed
61
  };
62

63 64 65
  renderMessage = content => (
    <Alert style={{ marginBottom: 24 }} message={content} type="error" showIcon />
  );
66 67

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

export default LoginPage;