Login.js 3.88 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' }))}
afc163's avatar
afc163 committed
84
            <UserName name="userName" placeholder="username: admin or user" />
陈帅's avatar
陈帅 committed
85 86
            <Password
              name="password"
afc163's avatar
afc163 committed
87
              placeholder="password: ant.design"
陈帅's avatar
陈帅 committed
88 89
              onPressEnter={() => this.loginForm.validateFields(this.handleSubmit)}
            />
ddcat1115's avatar
ddcat1115 committed
90
          </Tab>
91
          <Tab key="mobile" tab={formatMessage({ id: 'app.login.tab-login-mobile' })}>
jim's avatar
jim committed
92
            {login.status === 'error' &&
ddcat1115's avatar
ddcat1115 committed
93
              login.type === 'mobile' &&
Amumu's avatar
Amumu committed
94
              !submitting &&
Rayron Victor's avatar
Rayron Victor committed
95 96 97
              this.renderMessage(
                formatMessage({ id: 'app.login.message-invalid-verification-code' })
              )}
ddcat1115's avatar
ddcat1115 committed
98
            <Mobile name="mobile" />
99
            <Captcha name="captcha" countDown={120} onGetCaptcha={this.onGetCaptcha} />
ddcat1115's avatar
ddcat1115 committed
100 101
          </Tab>
          <div>
陈帅's avatar
陈帅 committed
102
            <Checkbox checked={autoLogin} onChange={this.changeAutoLogin}>
103
              <FormattedMessage id="app.login.remember-me" />
jim's avatar
jim committed
104 105
            </Checkbox>
            <a style={{ float: 'right' }} href="">
106
              <FormattedMessage id="app.login.forgot-password" />
jim's avatar
jim committed
107
            </a>
ddcat1115's avatar
ddcat1115 committed
108
          </div>
109 110 111
          <Submit loading={submitting}>
            <FormattedMessage id="app.login.login" />
          </Submit>
ddcat1115's avatar
ddcat1115 committed
112
          <div className={styles.other}>
113
            <FormattedMessage id="app.login.sign-in-with" />
陈帅's avatar
陈帅 committed
114 115 116
            <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
117
            <Link className={styles.register} to="/user/register">
118
              <FormattedMessage id="app.login.signup" />
jim's avatar
jim committed
119
            </Link>
ddcat1115's avatar
ddcat1115 committed
120 121
          </div>
        </Login>
122 123 124 125
      </div>
    );
  }
}
陈帅's avatar
陈帅 committed
126 127

export default LoginPage;