Register.js 7.84 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
const InputGroup = Input.Group;

const passwordStatusMap = {
afc163's avatar
afc163 committed
12 13
  ok: <div className={styles.success}>强度</div>,
  pass: <div className={styles.warning}>强度</div>,
jim's avatar
jim committed
14
  poor: <div className={styles.error}>强度太短</div>,
15 16 17 18 19
};

const passwordProgressMap = {
  ok: 'success',
  pass: 'normal',
jim's avatar
jim committed
20
  poor: 'exception',
21 22
};

Andreas Cederström's avatar
Andreas Cederström committed
23 24 25
@connect(({ register, loading }) => ({
  register,
  submitting: loading.effects['register/submit'],
26 27 28 29 30 31 32 33
}))
@Form.create()
export default class Register extends Component {
  state = {
    count: 0,
    confirmDirty: false,
    visible: false,
    help: '',
ddcat1115's avatar
ddcat1115 committed
34 35
    prefix: '86',
  };
36 37

  componentWillReceiveProps(nextProps) {
38 39
    const { form, dispatch } = this.props;
    const account = form.getFieldValue('mail');
40
    if (nextProps.register.status === 'ok') {
41
      dispatch(
jim's avatar
jim committed
42 43 44 45 46 47 48
        routerRedux.push({
          pathname: '/user/register-result',
          state: {
            account,
          },
        })
      );
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
    }
  }

  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);
ddcat1115's avatar
ddcat1115 committed
66
  };
67 68

  getPasswordStatus = () => {
afc163's avatar
afc163 committed
69
    const { form } = this.props;
70 71 72 73 74 75 76
    const value = form.getFieldValue('password');
    if (value && value.length > 9) {
      return 'ok';
    }
    if (value && value.length > 5) {
      return 'pass';
    }
jim's avatar
jim committed
77
    return 'poor';
ddcat1115's avatar
ddcat1115 committed
78
  };
79

jim's avatar
jim committed
80
  handleSubmit = e => {
81
    e.preventDefault();
82 83 84
    const { form, dispatch } = this.props;
    form.validateFields({ force: true }, (err, values) => {
      const { prefix } = this.state;
ddcat1115's avatar
ddcat1115 committed
85
      if (!err) {
86
        dispatch({
ddcat1115's avatar
ddcat1115 committed
87 88 89
          type: 'register/submit',
          payload: {
            ...values,
90
            prefix,
ddcat1115's avatar
ddcat1115 committed
91 92
          },
        });
93
      }
ddcat1115's avatar
ddcat1115 committed
94 95
    });
  };
96

jim's avatar
jim committed
97
  handleConfirmBlur = e => {
afc163's avatar
afc163 committed
98
    const { value } = e.target;
99 100
    const { confirmDirty } = this.state;
    this.setState({ confirmDirty: confirmDirty || !!value });
ddcat1115's avatar
ddcat1115 committed
101
  };
102 103

  checkConfirm = (rule, value, callback) => {
afc163's avatar
afc163 committed
104
    const { form } = this.props;
105 106 107 108 109
    if (value && value !== form.getFieldValue('password')) {
      callback('两次输入的密码不匹配!');
    } else {
      callback();
    }
ddcat1115's avatar
ddcat1115 committed
110
  };
111 112 113 114 115 116 117 118 119 120 121 122

  checkPassword = (rule, value, callback) => {
    if (!value) {
      this.setState({
        help: '请输入密码!',
        visible: !!value,
      });
      callback('error');
    } else {
      this.setState({
        help: '',
      });
123 124
      const { visible, confirmDirty } = this.state;
      if (!visible) {
125 126 127 128 129 130 131
        this.setState({
          visible: !!value,
        });
      }
      if (value.length < 6) {
        callback('error');
      } else {
afc163's avatar
afc163 committed
132
        const { form } = this.props;
133
        if (value && confirmDirty) {
134 135 136 137 138
          form.validateFields(['confirm'], { force: true });
        }
        callback();
      }
    }
ddcat1115's avatar
ddcat1115 committed
139 140
  };

jim's avatar
jim committed
141
  changePrefix = value => {
ddcat1115's avatar
ddcat1115 committed
142 143 144 145
    this.setState({
      prefix: value,
    });
  };
146 147

  renderPasswordProgress = () => {
afc163's avatar
afc163 committed
148
    const { form } = this.props;
149 150
    const value = form.getFieldValue('password');
    const passwordStatus = this.getPasswordStatus();
ddcat1115's avatar
ddcat1115 committed
151
    return value && value.length ? (
152 153 154 155 156 157 158 159
      <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}
        />
ddcat1115's avatar
ddcat1115 committed
160 161 162
      </div>
    ) : null;
  };
163 164

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