index.js 5.6 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 7
import { Login } from 'ant-design-pro';
import styles from './style.less';
8

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

11 12 13
@connect(({ BLOCK_NAME_CAMEL_CASE, loading }) => ({
  BLOCK_NAME_CAMEL_CASE,
  submitting: loading.effects['BLOCK_NAME_CAMEL_CASE/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
          const { dispatch } = this.props;
          dispatch({
33
            type: 'BLOCK_NAME_CAMEL_CASE/getCaptcha',
陈帅's avatar
陈帅 committed
34 35
            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({
47
        type: 'BLOCK_NAME_CAMEL_CASE/login',
ddcat1115's avatar
ddcat1115 committed
48 49 50 51 52 53
        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() {
67 68
    const { BLOCK_NAME_CAMEL_CASE, submitting } = this.props;
    const { status, type: loginType } = BLOCK_NAME_CAMEL_CASE;
陈帅'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 81 82
          <Tab key="account" tab={formatMessage({ id: 'BLOCK_NAME.login.tab-login-credentials' })}>
            {status === 'error' &&
              loginType === 'account' &&
Amumu's avatar
Amumu committed
83
              !submitting &&
xiaohuoni's avatar
xiaohuoni committed
84 85 86
              this.renderMessage(
                formatMessage({ id: 'BLOCK_NAME.login.message-invalid-credentials' })
              )}
87 88
            <UserName
              name="userName"
89
              placeholder={`${formatMessage({ id: 'BLOCK_NAME.login.userName' })}: admin or user`}
90 91 92
              rules={[
                {
                  required: true,
93
                  message: formatMessage({ id: 'BLOCK_NAME.userName.required' }),
94 95 96
                },
              ]}
            />
陈帅's avatar
陈帅 committed
97 98
            <Password
              name="password"
99
              placeholder={`${formatMessage({ id: 'BLOCK_NAME.login.password' })}: ant.design`}
100 101 102
              rules={[
                {
                  required: true,
103
                  message: formatMessage({ id: 'BLOCK_NAME.password.required' }),
104 105
                },
              ]}
陈帅's avatar
陈帅 committed
106 107
              onPressEnter={() => this.loginForm.validateFields(this.handleSubmit)}
            />
ddcat1115's avatar
ddcat1115 committed
108
          </Tab>
109 110 111
          <Tab key="mobile" tab={formatMessage({ id: 'BLOCK_NAME.login.tab-login-mobile' })}>
            {status === 'error' &&
              loginType === 'mobile' &&
Amumu's avatar
Amumu committed
112
              !submitting &&
Rayron Victor's avatar
Rayron Victor committed
113
              this.renderMessage(
114
                formatMessage({ id: 'BLOCK_NAME.login.message-invalid-verification-code' })
Rayron Victor's avatar
Rayron Victor committed
115
              )}
116 117
            <Mobile
              name="mobile"
118
              placeholder={formatMessage({ id: 'BLOCK_NAME.phone-number.placeholder' })}
119 120 121
              rules={[
                {
                  required: true,
122
                  message: formatMessage({ id: 'BLOCK_NAME.phone-number.required' }),
123 124 125
                },
                {
                  pattern: /^1\d{10}$/,
126
                  message: formatMessage({ id: 'BLOCK_NAME.phone-number.wrong-format' }),
127 128 129
                },
              ]}
            />
陈帅's avatar
陈帅 committed
130 131
            <Captcha
              name="captcha"
132
              placeholder={formatMessage({ id: 'BLOCK_NAME.verification-code.placeholder' })}
陈帅's avatar
陈帅 committed
133 134
              countDown={120}
              onGetCaptcha={this.onGetCaptcha}
135 136
              getCaptchaButtonText={formatMessage({ id: 'BLOCK_NAME.form.get-captcha' })}
              getCaptchaSecondText={formatMessage({ id: 'BLOCK_NAME.captcha.second' })}
137 138 139
              rules={[
                {
                  required: true,
140
                  message: formatMessage({ id: 'BLOCK_NAME.verification-code.required' }),
141 142
                },
              ]}
陈帅's avatar
陈帅 committed
143
            />
ddcat1115's avatar
ddcat1115 committed
144 145
          </Tab>
          <div>
陈帅's avatar
陈帅 committed
146
            <Checkbox checked={autoLogin} onChange={this.changeAutoLogin}>
147
              <FormattedMessage id="BLOCK_NAME.login.remember-me" />
jim's avatar
jim committed
148 149
            </Checkbox>
            <a style={{ float: 'right' }} href="">
150
              <FormattedMessage id="BLOCK_NAME.login.forgot-password" />
jim's avatar
jim committed
151
            </a>
ddcat1115's avatar
ddcat1115 committed
152
          </div>
153
          <Submit loading={submitting}>
154
            <FormattedMessage id="BLOCK_NAME.login.login" />
155
          </Submit>
ddcat1115's avatar
ddcat1115 committed
156
          <div className={styles.other}>
157
            <FormattedMessage id="BLOCK_NAME.login.sign-in-with" />
陈帅's avatar
陈帅 committed
158 159 160
            <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
161
            <Link className={styles.register} to="/user/register">
162
              <FormattedMessage id="BLOCK_NAME.login.signup" />
jim's avatar
jim committed
163
            </Link>
ddcat1115's avatar
ddcat1115 committed
164 165
          </div>
        </Login>
166 167 168 169
      </div>
    );
  }
}
陈帅's avatar
陈帅 committed
170 171

export default LoginPage;