Login.js 3.37 KB
Newer Older
1 2
import React, { Component } from 'react';
import { connect } from 'dva';
zinkey's avatar
zinkey committed
3
import Link from 'umi/link';
ddcat1115's avatar
ddcat1115 committed
4
import { Checkbox, Alert, Icon } from 'antd';
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

afc163's avatar
afc163 committed
10
export default
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;
          }}
        >
ddcat1115's avatar
ddcat1115 committed
79
          <Tab key="account" tab="账户密码登录">
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 91
          </Tab>
          <Tab key="mobile" tab="手机号登录">
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}>
jim's avatar
jim committed
101 102 103 104 105
              自动登录
            </Checkbox>
            <a style={{ float: 'right' }} href="">
              忘记密码
            </a>
ddcat1115's avatar
ddcat1115 committed
106
          </div>
Andreas Cederström's avatar
Andreas Cederström committed
107
          <Submit loading={submitting}>登录</Submit>
ddcat1115's avatar
ddcat1115 committed
108 109 110 111 112
          <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" />
xiaohu's avatar
xiaohu committed
113
            <Link className={styles.register} to="/User/Register">
jim's avatar
jim committed
114 115
              注册账户
            </Link>
ddcat1115's avatar
ddcat1115 committed
116 117
          </div>
        </Login>
118 119 120 121
      </div>
    );
  }
}