Login.js 2.69 KB
Newer Older
1 2
import React, { Component } from 'react';
import { connect } from 'dva';
afc163's avatar
afc163 committed
3
import { Link } from 'dva/router';
ddcat1115's avatar
ddcat1115 committed
4
import { Checkbox, Alert, Icon } from 'antd';
niko's avatar
niko committed
5
import Login from 'components/Login';
6 7
import styles from './Login.less';

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

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

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

ddcat1115's avatar
ddcat1115 committed
24 25
  handleSubmit = (err, values) => {
    const { type } = this.state;
26
    const { dispatch } = this.props;
ddcat1115's avatar
ddcat1115 committed
27
    if (!err) {
28
      dispatch({
ddcat1115's avatar
ddcat1115 committed
29 30 31 32 33 34 35
        type: 'login/login',
        payload: {
          ...values,
          type,
        },
      });
    }
jim's avatar
jim committed
36
  };
37

jim's avatar
jim committed
38
  changeAutoLogin = e => {
ddcat1115's avatar
ddcat1115 committed
39 40 41
    this.setState({
      autoLogin: e.target.checked,
    });
jim's avatar
jim committed
42
  };
43

jim's avatar
jim committed
44 45 46
  renderMessage = content => {
    return <Alert style={{ marginBottom: 24 }} message={content} type="error" showIcon />;
  };
47 48

  render() {
Andreas Cederström's avatar
Andreas Cederström committed
49
    const { login, submitting } = this.props;
50
    const { type, autoLogin } = this.state;
51 52
    return (
      <div className={styles.main}>
jim's avatar
jim committed
53
        <Login defaultActiveKey={type} onTabChange={this.onTabChange} onSubmit={this.handleSubmit}>
ddcat1115's avatar
ddcat1115 committed
54
          <Tab key="account" tab="账户密码登录">
jim's avatar
jim committed
55
            {login.status === 'error' &&
ddcat1115's avatar
ddcat1115 committed
56
              login.type === 'account' &&
Amumu's avatar
Amumu committed
57
              !submitting &&
jim's avatar
jim committed
58
              this.renderMessage('账户或密码错误(admin/888888)')}
ddcat1115's avatar
ddcat1115 committed
59 60
            <UserName name="userName" placeholder="admin/user" />
            <Password name="password" placeholder="888888/123456" />
ddcat1115's avatar
ddcat1115 committed
61 62
          </Tab>
          <Tab key="mobile" tab="手机号登录">
jim's avatar
jim committed
63
            {login.status === 'error' &&
ddcat1115's avatar
ddcat1115 committed
64
              login.type === 'mobile' &&
Amumu's avatar
Amumu committed
65
              !submitting &&
jim's avatar
jim committed
66
              this.renderMessage('验证码错误')}
ddcat1115's avatar
ddcat1115 committed
67 68 69 70
            <Mobile name="mobile" />
            <Captcha name="captcha" />
          </Tab>
          <div>
71
            <Checkbox checked={autoLogin} onChange={this.changeAutoLogin}>
jim's avatar
jim committed
72 73 74 75 76
              自动登录
            </Checkbox>
            <a style={{ float: 'right' }} href="">
              忘记密码
            </a>
ddcat1115's avatar
ddcat1115 committed
77
          </div>
Andreas Cederström's avatar
Andreas Cederström committed
78
          <Submit loading={submitting}>登录</Submit>
ddcat1115's avatar
ddcat1115 committed
79 80 81 82 83
          <div className={styles.other}>
            其他登录方式
            <Icon className={styles.icon} type="alipay-circle" />
            <Icon className={styles.icon} type="taobao-circle" />
            <Icon className={styles.icon} type="weibo-circle" />
jim's avatar
jim committed
84 85 86
            <Link className={styles.register} to="/user/register">
              注册账户
            </Link>
ddcat1115's avatar
ddcat1115 committed
87 88
          </div>
        </Login>
89 90 91 92
      </div>
    );
  }
}