Register.js 9.24 KB
Newer Older
1 2
import React, { Component } from 'react';
import { connect } from 'dva';
陈小聪's avatar
陈小聪 committed
3
import { formatMessage, FormattedMessage } from 'umi-plugin-react/locale';
zinkey's avatar
zinkey committed
4 5
import Link from 'umi/link';
import router from 'umi/router';
陈帅's avatar
陈帅 committed
6
import { Form, Input, Button, message, Select, Row, Col, Popover, Progress } from 'antd';
7 8 9
import styles from './Register.less';

const FormItem = Form.Item;
afc163's avatar
afc163 committed
10
const { Option } = Select;
11 12 13
const InputGroup = Input.Group;

const passwordStatusMap = {
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
  ok: (
    <div className={styles.success}>
      <FormattedMessage id="validation.password.strength.strong" />
    </div>
  ),
  pass: (
    <div className={styles.warning}>
      <FormattedMessage id="validation.password.strength.medium" />
    </div>
  ),
  poor: (
    <div className={styles.error}>
      <FormattedMessage id="validation.password.strength.short" />
    </div>
  ),
29 30 31 32 33
};

const passwordProgressMap = {
  ok: 'success',
  pass: 'normal',
jim's avatar
jim committed
34
  poor: 'exception',
35 36
};

Andreas Cederström's avatar
Andreas Cederström committed
37 38 39
@connect(({ register, loading }) => ({
  register,
  submitting: loading.effects['register/submit'],
40 41
}))
@Form.create()
afc163's avatar
afc163 committed
42
class Register extends Component {
43 44 45 46 47
  state = {
    count: 0,
    confirmDirty: false,
    visible: false,
    help: '',
ddcat1115's avatar
ddcat1115 committed
48 49
    prefix: '86',
  };
50

jim's avatar
jim committed
51
  componentDidUpdate() {
52
    const { form, register } = this.props;
陈帅's avatar
陈帅 committed
53 54
    const account = form.getFieldValue('mail');
    if (register.status === 'ok') {
55 56 57 58 59 60
      router.push({
        pathname: '/user/register-result',
        state: {
          account,
        },
      });
61 62
    }
  }
陈帅's avatar
陈帅 committed
63

64 65 66 67 68 69 70 71 72 73 74 75 76 77
  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);
陈帅's avatar
陈帅 committed
78
    message.warning(formatMessage({ id: 'app.login.verification-code-warning' }));
ddcat1115's avatar
ddcat1115 committed
79
  };
80 81

  getPasswordStatus = () => {
afc163's avatar
afc163 committed
82
    const { form } = this.props;
83 84 85 86 87 88 89
    const value = form.getFieldValue('password');
    if (value && value.length > 9) {
      return 'ok';
    }
    if (value && value.length > 5) {
      return 'pass';
    }
jim's avatar
jim committed
90
    return 'poor';
ddcat1115's avatar
ddcat1115 committed
91
  };
92

jim's avatar
jim committed
93
  handleSubmit = e => {
94
    e.preventDefault();
陈帅's avatar
陈帅 committed
95 96
    const { form, dispatch } = this.props;
    form.validateFields({ force: true }, (err, values) => {
ddcat1115's avatar
ddcat1115 committed
97
      if (!err) {
陈帅's avatar
陈帅 committed
98 99
        const { prefix } = this.state;
        dispatch({
ddcat1115's avatar
ddcat1115 committed
100 101 102
          type: 'register/submit',
          payload: {
            ...values,
陈帅's avatar
陈帅 committed
103
            prefix,
ddcat1115's avatar
ddcat1115 committed
104 105
          },
        });
106
      }
ddcat1115's avatar
ddcat1115 committed
107 108
    });
  };
109

jim's avatar
jim committed
110
  handleConfirmBlur = e => {
afc163's avatar
afc163 committed
111
    const { value } = e.target;
陈帅's avatar
陈帅 committed
112 113
    const { confirmDirty } = this.state;
    this.setState({ confirmDirty: confirmDirty || !!value });
ddcat1115's avatar
ddcat1115 committed
114
  };
115 116

  checkConfirm = (rule, value, callback) => {
afc163's avatar
afc163 committed
117
    const { form } = this.props;
118
    if (value && value !== form.getFieldValue('password')) {
119
      callback(formatMessage({ id: 'validation.password.twice' }));
120 121 122
    } else {
      callback();
    }
ddcat1115's avatar
ddcat1115 committed
123
  };
124 125

  checkPassword = (rule, value, callback) => {
陈帅's avatar
陈帅 committed
126
    const { visible, confirmDirty } = this.state;
127 128
    if (!value) {
      this.setState({
129
        help: formatMessage({ id: 'validation.password.required' }),
130 131 132 133 134 135 136
        visible: !!value,
      });
      callback('error');
    } else {
      this.setState({
        help: '',
      });
陈帅's avatar
陈帅 committed
137
      if (!visible) {
138 139 140 141 142 143 144
        this.setState({
          visible: !!value,
        });
      }
      if (value.length < 6) {
        callback('error');
      } else {
afc163's avatar
afc163 committed
145
        const { form } = this.props;
陈帅's avatar
陈帅 committed
146
        if (value && confirmDirty) {
147 148 149 150 151
          form.validateFields(['confirm'], { force: true });
        }
        callback();
      }
    }
ddcat1115's avatar
ddcat1115 committed
152 153
  };

jim's avatar
jim committed
154
  changePrefix = value => {
ddcat1115's avatar
ddcat1115 committed
155 156 157 158
    this.setState({
      prefix: value,
    });
  };
159 160

  renderPasswordProgress = () => {
afc163's avatar
afc163 committed
161
    const { form } = this.props;
162 163
    const value = form.getFieldValue('password');
    const passwordStatus = this.getPasswordStatus();
ddcat1115's avatar
ddcat1115 committed
164
    return value && value.length ? (
165 166 167 168 169 170 171 172
      <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
173 174 175
      </div>
    ) : null;
  };
176 177

  render() {
Andreas Cederström's avatar
Andreas Cederström committed
178
    const { form, submitting } = this.props;
179
    const { getFieldDecorator } = form;
陈帅's avatar
陈帅 committed
180
    const { count, prefix, help, visible } = this.state;
181 182
    return (
      <div className={styles.main}>
183 184 185
        <h3>
          <FormattedMessage id="app.register.register" />
        </h3>
186 187 188
        <Form onSubmit={this.handleSubmit}>
          <FormItem>
            {getFieldDecorator('mail', {
ddcat1115's avatar
ddcat1115 committed
189 190 191
              rules: [
                {
                  required: true,
192
                  message: formatMessage({ id: 'validation.email.required' }),
ddcat1115's avatar
ddcat1115 committed
193 194 195
                },
                {
                  type: 'email',
196
                  message: formatMessage({ id: 'validation.email.wrong-format' }),
ddcat1115's avatar
ddcat1115 committed
197 198
                },
              ],
199 200 201
            })(
              <Input size="large" placeholder={formatMessage({ id: 'form.email.placeholder' })} />
            )}
202
          </FormItem>
陈帅's avatar
陈帅 committed
203
          <FormItem help={help}>
204
            <Popover
Ilan's avatar
Ilan committed
205
              getPopupContainer={node => node.parentNode}
206
              content={
ddcat1115's avatar
ddcat1115 committed
207
                <div style={{ padding: '4px 0' }}>
208 209
                  {passwordStatusMap[this.getPasswordStatus()]}
                  {this.renderPasswordProgress()}
ddcat1115's avatar
ddcat1115 committed
210
                  <div style={{ marginTop: 10 }}>
211
                    <FormattedMessage id="validation.password.strength.msg" />
ddcat1115's avatar
ddcat1115 committed
212
                  </div>
213 214 215 216
                </div>
              }
              overlayStyle={{ width: 240 }}
              placement="right"
陈帅's avatar
陈帅 committed
217
              visible={visible}
218 219
            >
              {getFieldDecorator('password', {
ddcat1115's avatar
ddcat1115 committed
220 221 222 223 224
                rules: [
                  {
                    validator: this.checkPassword,
                  },
                ],
225 226 227 228 229 230 231
              })(
                <Input
                  size="large"
                  type="password"
                  placeholder={formatMessage({ id: 'form.password.placeholder' })}
                />
              )}
232 233 234 235
            </Popover>
          </FormItem>
          <FormItem>
            {getFieldDecorator('confirm', {
ddcat1115's avatar
ddcat1115 committed
236 237 238
              rules: [
                {
                  required: true,
239
                  message: formatMessage({ id: 'validation.confirm-password.required' }),
ddcat1115's avatar
ddcat1115 committed
240 241 242 243 244
                },
                {
                  validator: this.checkConfirm,
                },
              ],
245 246 247 248 249 250 251
            })(
              <Input
                size="large"
                type="password"
                placeholder={formatMessage({ id: 'form.confirm-password.placeholder' })}
              />
            )}
252 253
          </FormItem>
          <FormItem>
ddcat1115's avatar
ddcat1115 committed
254 255 256 257 258 259 260 261 262 263 264 265 266 267
            <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,
268
                    message: formatMessage({ id: 'validation.phone-number.required' }),
ddcat1115's avatar
ddcat1115 committed
269 270
                  },
                  {
271
                    pattern: /^\d{11}$/,
272
                    message: formatMessage({ id: 'validation.phone-number.wrong-format' }),
ddcat1115's avatar
ddcat1115 committed
273 274
                  },
                ],
275 276 277 278 279 280 281
              })(
                <Input
                  size="large"
                  style={{ width: '80%' }}
                  placeholder={formatMessage({ id: 'form.phone-number.placeholder' })}
                />
              )}
282 283 284 285 286 287
            </InputGroup>
          </FormItem>
          <FormItem>
            <Row gutter={8}>
              <Col span={16}>
                {getFieldDecorator('captcha', {
ddcat1115's avatar
ddcat1115 committed
288 289 290
                  rules: [
                    {
                      required: true,
291
                      message: formatMessage({ id: 'validation.verification-code.required' }),
ddcat1115's avatar
ddcat1115 committed
292 293
                    },
                  ],
294 295 296 297 298 299
                })(
                  <Input
                    size="large"
                    placeholder={formatMessage({ id: 'form.verification-code.placeholder' })}
                  />
                )}
300 301 302
              </Col>
              <Col span={8}>
                <Button
ddcat1115's avatar
ddcat1115 committed
303
                  size="large"
304 305 306 307
                  disabled={count}
                  className={styles.getCaptcha}
                  onClick={this.onGetCaptcha}
                >
308 309 310
                  {count
                    ? `${count} s`
                    : formatMessage({ id: 'app.register.get-verification-code' })}
311 312 313 314 315
                </Button>
              </Col>
            </Row>
          </FormItem>
          <FormItem>
ddcat1115's avatar
ddcat1115 committed
316 317
            <Button
              size="large"
Andreas Cederström's avatar
Andreas Cederström committed
318
              loading={submitting}
ddcat1115's avatar
ddcat1115 committed
319 320 321 322
              className={styles.submit}
              type="primary"
              htmlType="submit"
            >
323
              <FormattedMessage id="app.register.register" />
324
            </Button>
xiaohu's avatar
xiaohu committed
325
            <Link className={styles.login} to="/User/Login">
David Z. Han's avatar
David Z. Han committed
326
              <FormattedMessage id="app.register.sign-in" />
ddcat1115's avatar
ddcat1115 committed
327
            </Link>
328 329 330 331 332 333
          </FormItem>
        </Form>
      </div>
    );
  }
}
lijiehua's avatar
lijiehua committed
334 335

export default Register;