Login.js 5.33 KB
Newer Older
1 2
import React, { Component } from 'react';
import { connect } from 'dva';
3
import { formatMessage, FormattedMessage } from 'umi/locale';
zinkey's avatar
zinkey committed
4
import Link from 'umi/link';
ddcat1115's avatar
ddcat1115 committed
5
import { Checkbox, Alert, 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 38 39 40
            .then(resolve)
            .catch(reject);
        }
      });
    });
陈帅's avatar
陈帅 committed
41

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

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

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

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

export default LoginPage;