Login.js 5.67 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';
4 5 6 7
import { Form, Input, Tabs, Button, Icon, Checkbox, Row, Col, Alert } from 'antd';
import styles from './Login.less';

const FormItem = Form.Item;
afc163's avatar
afc163 committed
8
const { TabPane } = Tabs;
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

@connect(state => ({
  login: state.login,
}))
@Form.create()
export default class Login extends Component {
  state = {
    count: 0,
    type: 'account',
  }

  componentWillUnmount() {
    clearInterval(this.interval);
  }

afc163's avatar
afc163 committed
24 25
  onSwitch = (type) => {
    this.setState({ type });
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
  }

  onGetCaptcha = () => {
    let count = 59;
    this.setState({ count });
    this.interval = setInterval(() => {
      count -= 1;
      this.setState({ count });
      if (count === 0) {
        clearInterval(this.interval);
      }
    }, 1000);
  }

  handleSubmit = (e) => {
    e.preventDefault();
    this.props.form.validateFields({ force: true },
      (err, values) => {
        if (!err) {
          this.props.dispatch({
afc163's avatar
afc163 committed
46 47 48 49 50
            type: 'login/login',
            payload: {
              ...values,
              type: this.state.type,
            },
51 52 53 54 55 56
          });
        }
      }
    );
  }

afc163's avatar
afc163 committed
57 58 59
  renderMessage = (message) => {
    return (
      <Alert
ddcat1115's avatar
ddcat1115 committed
60
        style={{ marginBottom: 24 }}
afc163's avatar
afc163 committed
61 62 63 64 65
        message={message}
        type="error"
        showIcon
      />
    );
66 67 68 69 70 71 72 73 74
  }

  render() {
    const { form, login } = this.props;
    const { getFieldDecorator } = form;
    const { count, type } = this.state;
    return (
      <div className={styles.main}>
        <Form onSubmit={this.handleSubmit}>
nikogu's avatar
nikogu committed
75
          <Tabs animated={false} className={styles.tabs} activeKey={type} onChange={this.onSwitch}>
76
            <TabPane tab="账户密码登录" key="account">
ddcat1115's avatar
ddcat1115 committed
77 78 79 80
              {
                login.status === 'error' &&
                login.type === 'account' &&
                login.submitting === false &&
afc163's avatar
afc163 committed
81
                this.renderMessage('账户或密码错误')
ddcat1115's avatar
ddcat1115 committed
82
              }
83 84 85 86 87 88 89
              <FormItem>
                {getFieldDecorator('userName', {
                  rules: [{
                    required: type === 'account', message: '请输入账户名!',
                  }],
                })(
                  <Input
ddcat1115's avatar
ddcat1115 committed
90
                    size="large"
ddcat1115's avatar
ddcat1115 committed
91
                    prefix={<Icon type="user" className={styles.prefixIcon} />}
ddcat1115's avatar
ddcat1115 committed
92
                    placeholder="admin"
93 94 95 96 97 98 99 100 101 102
                  />
                )}
              </FormItem>
              <FormItem>
                {getFieldDecorator('password', {
                  rules: [{
                    required: type === 'account', message: '请输入密码!',
                  }],
                })(
                  <Input
ddcat1115's avatar
ddcat1115 committed
103
                    size="large"
ddcat1115's avatar
ddcat1115 committed
104
                    prefix={<Icon type="lock" className={styles.prefixIcon} />}
105
                    type="password"
ddcat1115's avatar
ddcat1115 committed
106
                    placeholder="888888"
107 108 109 110 111
                  />
                )}
              </FormItem>
            </TabPane>
            <TabPane tab="手机号登录" key="mobile">
ddcat1115's avatar
ddcat1115 committed
112 113 114 115
              {
                login.status === 'error' &&
                login.type === 'mobile' &&
                login.submitting === false &&
afc163's avatar
afc163 committed
116
                this.renderMessage('验证码错误')
ddcat1115's avatar
ddcat1115 committed
117
              }
118 119 120 121 122 123 124 125 126
              <FormItem>
                {getFieldDecorator('mobile', {
                  rules: [{
                    required: type === 'mobile', message: '请输入手机号!',
                  }, {
                    pattern: /^1\d{10}$/, message: '手机号格式错误!',
                  }],
                })(
                  <Input
ddcat1115's avatar
ddcat1115 committed
127
                    size="large"
ddcat1115's avatar
ddcat1115 committed
128
                    prefix={<Icon type="mobile" className={styles.prefixIcon} />}
129 130 131 132 133 134 135 136 137 138 139 140 141 142
                    placeholder="手机号"
                  />
                )}
              </FormItem>
              <FormItem>
                <Row gutter={8}>
                  <Col span={16}>
                    {getFieldDecorator('captcha', {
                      rules: [{
                        required: type === 'mobile', message: '请输入验证码!',
                      }],
                    })(
                      <Input
                        size="large"
ddcat1115's avatar
ddcat1115 committed
143
                        prefix={<Icon type="mail" className={styles.prefixIcon} />}
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
                        placeholder="验证码"
                      />
                    )}
                  </Col>
                  <Col span={8}>
                    <Button
                      disabled={count}
                      className={styles.getCaptcha}
                      size="large"
                      onClick={this.onGetCaptcha}
                    >
                      {count ? `${count} s` : '获取验证码'}
                    </Button>
                  </Col>
                </Row>
              </FormItem>
            </TabPane>
          </Tabs>
          <FormItem className={styles.additional}>
            {getFieldDecorator('remember', {
              valuePropName: 'checked',
              initialValue: true,
            })(
ddcat1115's avatar
ddcat1115 committed
167
              <Checkbox className={styles.autoLogin}>自动登录</Checkbox>
168 169
            )}
            <a className={styles.forgot} href="">忘记密码</a>
ddcat1115's avatar
ddcat1115 committed
170
            <Button size="large" loading={login.submitting} className={styles.submit} type="primary" htmlType="submit">
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
              登录
            </Button>
          </FormItem>
        </Form>
        <div className={styles.other}>
          其他登录方式
          {/* 需要加到 Icon 中 */}
          <span className={styles.iconAlipay} />
          <span className={styles.iconTaobao} />
          <span className={styles.iconWeibo} />
          <Link className={styles.register} to="/user/register">注册账户</Link>
        </div>
      </div>
    );
  }
}