Login.js 3.4 KB
Newer Older
1 2
import React, { Component } from 'react';
import { connect } from 'dva';
Z
zinkey 已提交
3
import Link from 'umi/link';
D
ddcat1115 已提交
4
import { Checkbox, Alert, Icon } from 'antd';
5
import Login from '@/components/Login';
6 7
import styles from './Login.less';

D
ddcat1115 已提交
8
const { Tab, UserName, Password, Mobile, Captcha, Submit } = Login;
9

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

J
jim 已提交
20
  onTabChange = type => {
A
afc163 已提交
21
    this.setState({ type });
J
jim 已提交
22
  };
23

24 25 26 27 28 29
  onGetCaptcha = () => {
    return new Promise((resolve, reject) => {
      this.loginForm.validateFields(['mobile'], {}, (err, values) => {
        if (err) {
          reject(err);
        } else {
陈帅 已提交
30 31 32 33 34
          const { dispatch } = this.props;
          dispatch({
            type: 'login/getCaptcha',
            payload: values.mobile,
          })
35 36 37 38 39 40
            .then(resolve)
            .catch(reject);
        }
      });
    });
  };
陈帅 已提交
41

D
ddcat1115 已提交
42 43 44
  handleSubmit = (err, values) => {
    const { type } = this.state;
    if (!err) {
陈帅 已提交
45 46
      const { dispatch } = this.props;
      dispatch({
D
ddcat1115 已提交
47 48 49 50 51 52 53
        type: 'login/login',
        payload: {
          ...values,
          type,
        },
      });
    }
J
jim 已提交
54
  };
55

J
jim 已提交
56
  changeAutoLogin = e => {
D
ddcat1115 已提交
57 58 59
    this.setState({
      autoLogin: e.target.checked,
    });
J
jim 已提交
60
  };
61

J
jim 已提交
62 63 64
  renderMessage = content => {
    return <Alert style={{ marginBottom: 24 }} message={content} type="error" showIcon />;
  };
65 66

  render() {
A
Andreas Cederström 已提交
67
    const { login, submitting } = this.props;
陈帅 已提交
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;
          }}
        >
D
ddcat1115 已提交
79
          <Tab key="account" tab="账户密码登录">
J
jim 已提交
80
            {login.status === 'error' &&
D
ddcat1115 已提交
81
              login.type === 'account' &&
82
              !submitting &&
J
jim 已提交
83
              this.renderMessage('账户或密码错误(admin/888888)')}
D
ddcat1115 已提交
84
            <UserName name="userName" placeholder="admin/user" />
陈帅 已提交
85 86 87 88 89
            <Password
              name="password"
              placeholder="888888/123456"
              onPressEnter={() => this.loginForm.validateFields(this.handleSubmit)}
            />
D
ddcat1115 已提交
90 91
          </Tab>
          <Tab key="mobile" tab="手机号登录">
J
jim 已提交
92
            {login.status === 'error' &&
D
ddcat1115 已提交
93
              login.type === 'mobile' &&
94
              !submitting &&
J
jim 已提交
95
              this.renderMessage('验证码错误')}
D
ddcat1115 已提交
96
            <Mobile name="mobile" />
97
            <Captcha name="captcha" countDown={120} onGetCaptcha={this.onGetCaptcha} />
D
ddcat1115 已提交
98 99
          </Tab>
          <div>
陈帅 已提交
100
            <Checkbox checked={autoLogin} onChange={this.changeAutoLogin}>
J
jim 已提交
101 102 103 104 105
              自动登录
            </Checkbox>
            <a style={{ float: 'right' }} href="">
              忘记密码
            </a>
D
ddcat1115 已提交
106
          </div>
A
Andreas Cederström 已提交
107
          <Submit loading={submitting}>登录</Submit>
D
ddcat1115 已提交
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" />
陈小聪-小虎Oni's avatar
陈小聪-小虎Oni 已提交
113
            <Link className={styles.register} to="/User/Register">
J
jim 已提交
114 115
              注册账户
            </Link>
D
ddcat1115 已提交
116 117
          </div>
        </Login>
118 119 120 121
      </div>
    );
  }
}