Login.js 3.44 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

Andreas Cederström's avatar
Andreas Cederström committed
10 11 12
@connect(({ login, loading }) => ({
  login,
  submitting: loading.effects['login/login'],
13
}))
afc163's avatar
afc163 committed
14
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

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

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

jim's avatar
jim committed
55
  changeAutoLogin = e => {
ddcat1115's avatar
ddcat1115 committed
56 57 58
    this.setState({
      autoLogin: e.target.checked,
    });
jim's avatar
jim committed
59
  };
60

61 62 63
  renderMessage = content => (
    <Alert style={{ marginBottom: 24 }} message={content} type="error" showIcon />
  );
64 65

  render() {
Andreas Cederström's avatar
Andreas Cederström committed
66
    const { login, submitting } = this.props;
陈帅's avatar
陈帅 committed
67
    const { type, autoLogin } = this.state;
68 69
    return (
      <div className={styles.main}>
70 71 72 73 74 75 76 77
        <Login
          defaultActiveKey={type}
          onTabChange={this.onTabChange}
          onSubmit={this.handleSubmit}
          ref={form => {
            this.loginForm = form;
          }}
        >
ddcat1115's avatar
ddcat1115 committed
78
          <Tab key="account" tab="账户密码登录">
jim's avatar
jim committed
79
            {login.status === 'error' &&
ddcat1115's avatar
ddcat1115 committed
80
              login.type === 'account' &&
Amumu's avatar
Amumu committed
81
              !submitting &&
jim's avatar
jim committed
82
              this.renderMessage('账户或密码错误(admin/888888)')}
ddcat1115's avatar
ddcat1115 committed
83
            <UserName name="userName" placeholder="admin/user" />
陈帅's avatar
陈帅 committed
84 85 86 87 88
            <Password
              name="password"
              placeholder="888888/123456"
              onPressEnter={() => this.loginForm.validateFields(this.handleSubmit)}
            />
ddcat1115's avatar
ddcat1115 committed
89 90
          </Tab>
          <Tab key="mobile" tab="手机号登录">
jim's avatar
jim committed
91
            {login.status === 'error' &&
ddcat1115's avatar
ddcat1115 committed
92
              login.type === 'mobile' &&
Amumu's avatar
Amumu committed
93
              !submitting &&
jim's avatar
jim committed
94
              this.renderMessage('验证码错误')}
ddcat1115's avatar
ddcat1115 committed
95
            <Mobile name="mobile" />
96
            <Captcha name="captcha" countDown={120} onGetCaptcha={this.onGetCaptcha} />
ddcat1115's avatar
ddcat1115 committed
97 98
          </Tab>
          <div>
陈帅's avatar
陈帅 committed
99
            <Checkbox checked={autoLogin} onChange={this.changeAutoLogin}>
jim's avatar
jim committed
100 101 102 103 104
              自动登录
            </Checkbox>
            <a style={{ float: 'right' }} href="">
              忘记密码
            </a>
ddcat1115's avatar
ddcat1115 committed
105
          </div>
Andreas Cederström's avatar
Andreas Cederström committed
106
          <Submit loading={submitting}>登录</Submit>
ddcat1115's avatar
ddcat1115 committed
107 108
          <div className={styles.other}>
            其他登录方式
陈帅's avatar
陈帅 committed
109 110 111
            <Icon type="alipay-circle" className={styles.icon} theme="outlined" />
            <Icon type="taobao-circle" className={styles.icon} theme="outlined" />
            <Icon type="weibo-circle" className={styles.icon} theme="outlined" />
xiaohu's avatar
xiaohu committed
112
            <Link className={styles.register} to="/User/Register">
jim's avatar
jim committed
113 114
              注册账户
            </Link>
ddcat1115's avatar
ddcat1115 committed
115 116
          </div>
        </Login>
117 118 119 120
      </div>
    );
  }
}
陈帅's avatar
陈帅 committed
121 122

export default LoginPage;