Login.js 2.7 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,
18 19
  }

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

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

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

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

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