Login.js 2.64 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 5
import { Checkbox, Alert, Icon } from 'antd';
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 10 11 12

@connect(state => ({
  login: state.login,
}))
ddcat1115's avatar
ddcat1115 committed
13
export default class LoginPage extends Component {
14 15
  state = {
    type: 'account',
ddcat1115's avatar
ddcat1115 committed
16
    autoLogin: true,
17 18
  }

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

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

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

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

  render() {
ddcat1115's avatar
ddcat1115 committed
49 50
    const { login } = this.props;
    const { type } = this.state;
51 52
    return (
      <div className={styles.main}>
ddcat1115's avatar
ddcat1115 committed
53 54 55 56 57 58 59 60 61 62 63 64
        <Login
          defaultActiveKey={type}
          onTabChange={this.onTabChange}
          onSubmit={this.handleSubmit}
        >
          <Tab key="account" tab="账户密码登录">
            {
              login.status === 'error' &&
              login.type === 'account' &&
              login.submitting === false &&
              this.renderMessage('账户或密码错误')
            }
ddcat1115's avatar
ddcat1115 committed
65 66
            <UserName name="userName" placeholder="admin/user" />
            <Password name="password" placeholder="888888/123456" />
ddcat1115's avatar
ddcat1115 committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
          </Tab>
          <Tab key="mobile" tab="手机号登录">
            {
              login.status === 'error' &&
              login.type === 'mobile' &&
              login.submitting === false &&
              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>
          <Submit>登录</Submit>
          <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>
91 92 93 94
      </div>
    );
  }
}