Login.js 3.76 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 &&
jim's avatar
jim committed
83
              this.renderMessage('账户或密码错误(admin/888888)')}
ddcat1115's avatar
ddcat1115 committed
84
            <UserName name="userName" placeholder="admin/user" />
陈帅's avatar
陈帅 committed
85 86 87 88 89
            <Password
              name="password"
              placeholder="888888/123456"
              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 &&
jim's avatar
jim committed
95
              this.renderMessage('验证码错误')}
ddcat1115's avatar
ddcat1115 committed
96
            <Mobile name="mobile" />
97
            <Captcha name="captcha" countDown={120} onGetCaptcha={this.onGetCaptcha} />
ddcat1115's avatar
ddcat1115 committed
98 99
          </Tab>
          <div>
陈帅's avatar
陈帅 committed
100
            <Checkbox checked={autoLogin} onChange={this.changeAutoLogin}>
101
              <FormattedMessage id="app.login.remember-me" />
jim's avatar
jim committed
102 103
            </Checkbox>
            <a style={{ float: 'right' }} href="">
104
              <FormattedMessage id="app.login.forgot-password" />
jim's avatar
jim committed
105
            </a>
ddcat1115's avatar
ddcat1115 committed
106
          </div>
107 108 109
          <Submit loading={submitting}>
            <FormattedMessage id="app.login.login" />
          </Submit>
ddcat1115's avatar
ddcat1115 committed
110
          <div className={styles.other}>
111
            <FormattedMessage id="app.login.sign-in-with" />
陈帅's avatar
陈帅 committed
112 113 114
            <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" />
xiaohu's avatar
xiaohu committed
115
            <Link className={styles.register} to="/User/Register">
116
              <FormattedMessage id="app.login.signup" />
jim's avatar
jim committed
117
            </Link>
ddcat1115's avatar
ddcat1115 committed
118 119
          </div>
        </Login>
120 121 122 123
      </div>
    );
  }
}
陈帅's avatar
陈帅 committed
124 125

export default LoginPage;