Register.js 7.3 KB
Newer Older
1 2 3 4 5 6 7
import React, { Component } from 'react';
import { connect } from 'dva';
import { routerRedux, Link } from 'dva/router';
import { Form, Input, Button, Select, Row, Col, Popover, Progress } from 'antd';
import styles from './Register.less';

const FormItem = Form.Item;
afc163's avatar
afc163 committed
8
const { Option } = Select;
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
const InputGroup = Input.Group;

const passwordStatusMap = {
  ok: <p className={styles.success}>强度</p>,
  pass: <p className={styles.warning}>强度</p>,
  pool: <p className={styles.error}>强度太短</p>,
};

const passwordProgressMap = {
  ok: 'success',
  pass: 'normal',
  pool: 'exception',
};

@connect(state => ({
  register: state.register,
}))
@Form.create()
export default class Register extends Component {
  state = {
    count: 0,
    confirmDirty: false,
    visible: false,
    help: '',
  }

  componentWillReceiveProps(nextProps) {
    if (nextProps.register.status === 'ok') {
ddcat1115's avatar
ddcat1115 committed
37
      this.props.dispatch(routerRedux.push('/user/register-result'));
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
    }
  }

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

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

  getPasswordStatus = () => {
afc163's avatar
afc163 committed
58
    const { form } = this.props;
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
    const value = form.getFieldValue('password');
    if (value && value.length > 9) {
      return 'ok';
    }
    if (value && value.length > 5) {
      return 'pass';
    }
    return 'pool';
  }

  handleSubmit = (e) => {
    e.preventDefault();
    this.props.form.validateFields({ force: true },
      (err, values) => {
        if (!err) {
          this.props.dispatch({
            type: 'register/submit',
            payload: values,
          });
        }
      }
    );
  }

  handleConfirmBlur = (e) => {
afc163's avatar
afc163 committed
84
    const { value } = e.target;
85 86 87 88
    this.setState({ confirmDirty: this.state.confirmDirty || !!value });
  }

  checkConfirm = (rule, value, callback) => {
afc163's avatar
afc163 committed
89
    const { form } = this.props;
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
    if (value && value !== form.getFieldValue('password')) {
      callback('两次输入的密码不匹配!');
    } else {
      callback();
    }
  }

  checkPassword = (rule, value, callback) => {
    if (!value) {
      this.setState({
        help: '请输入密码!',
        visible: !!value,
      });
      callback('error');
    } else {
      this.setState({
        help: '',
      });
      if (!this.state.visible) {
        this.setState({
          visible: !!value,
        });
      }
      if (value.length < 6) {
        callback('error');
      } else {
afc163's avatar
afc163 committed
116
        const { form } = this.props;
117 118 119 120 121 122 123 124 125
        if (value && this.state.confirmDirty) {
          form.validateFields(['confirm'], { force: true });
        }
        callback();
      }
    }
  }

  renderPasswordProgress = () => {
afc163's avatar
afc163 committed
126
    const { form } = this.props;
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
    const value = form.getFieldValue('password');
    const passwordStatus = this.getPasswordStatus();
    return value && value.length ?
      <div className={styles[`progress-${passwordStatus}`]}>
        <Progress
          status={passwordProgressMap[passwordStatus]}
          className={styles.progress}
          strokeWidth={6}
          percent={value.length * 10 > 100 ? 100 : value.length * 10}
          showInfo={false}
        />
      </div> : null;
  }

  render() {
    const { form, register } = this.props;
    const { getFieldDecorator } = form;
    const { count } = this.state;
    return (
      <div className={styles.main}>
        <h3>注册</h3>
        <Form onSubmit={this.handleSubmit}>
          <FormItem>
            {getFieldDecorator('mail', {
              rules: [{
                required: true, message: '请输入邮箱地址!',
              }, {
                type: 'email', message: '邮箱地址格式错误!',
              }],
            })(
ddcat1115's avatar
ddcat1115 committed
157
              <Input size="large" placeholder="邮箱" />
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
            )}
          </FormItem>
          <FormItem help={this.state.help}>
            <Popover
              content={
                <div>
                  {passwordStatusMap[this.getPasswordStatus()]}
                  {this.renderPasswordProgress()}
                  <p style={{ marginTop: 10 }}>请至少输入 6 个字符请不要使用容易被猜到的密码</p>
                </div>
              }
              overlayStyle={{ width: 240 }}
              placement="right"
              visible={this.state.visible}
            >
              {getFieldDecorator('password', {
                rules: [{
                  validator: this.checkPassword,
                }],
              })(
                <Input
ddcat1115's avatar
ddcat1115 committed
179
                  size="large"
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
                  type="password"
                  placeholder="至少6位密码,区分大小写"
                />
              )}
            </Popover>
          </FormItem>
          <FormItem>
            {getFieldDecorator('confirm', {
              rules: [{
                required: true, message: '请确认密码!',
              }, {
                validator: this.checkConfirm,
              }],
            })(
              <Input
ddcat1115's avatar
ddcat1115 committed
195
                size="large"
196 197 198 199 200 201
                type="password"
                placeholder="确认密码"
              />
            )}
          </FormItem>
          <FormItem>
ddcat1115's avatar
ddcat1115 committed
202 203
            <InputGroup size="large" className={styles.mobileGroup} compact>
              <FormItem style={{ width: '20%' }}>
204 205 206
                {getFieldDecorator('prefix', {
                  initialValue: '86',
                })(
ddcat1115's avatar
ddcat1115 committed
207
                  <Select size="large">
208 209 210 211 212
                    <Option value="86">+86</Option>
                    <Option value="87">+87</Option>
                  </Select>
                )}
              </FormItem>
ddcat1115's avatar
ddcat1115 committed
213
              <FormItem style={{ width: '80%' }}>
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
                {getFieldDecorator('mobile', {
                  rules: [{
                    required: true, message: '请输入手机号!',
                  }, {
                    pattern: /^1\d{10}$/, message: '手机号格式错误!',
                  }],
                })(
                  <Input placeholder="11位手机号" />
                )}
              </FormItem>
            </InputGroup>
          </FormItem>
          <FormItem>
            <Row gutter={8}>
              <Col span={16}>
                {getFieldDecorator('captcha', {
                  rules: [{
                    required: true, message: '请输入验证码!',
                  }],
                })(
                  <Input
ddcat1115's avatar
ddcat1115 committed
235
                    size="large"
236 237 238 239 240 241
                    placeholder="验证码"
                  />
                )}
              </Col>
              <Col span={8}>
                <Button
ddcat1115's avatar
ddcat1115 committed
242
                  size="large"
243 244 245 246 247 248 249 250 251 252
                  disabled={count}
                  className={styles.getCaptcha}
                  onClick={this.onGetCaptcha}
                >
                  {count ? `${count} s` : '获取验证码'}
                </Button>
              </Col>
            </Row>
          </FormItem>
          <FormItem>
ddcat1115's avatar
ddcat1115 committed
253
            <Button size="large" loading={register.submitting} className={styles.submit} type="primary" htmlType="submit">
254 255 256 257 258 259 260 261 262
              注册
            </Button>
            <Link className={styles.login} to="/user/login">使用已有账户登录</Link>
          </FormItem>
        </Form>
      </div>
    );
  }
}