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

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

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

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

37 38 39
@connect(({ BLOCK_NAME_CAMEL_CASE, loading }) => ({
  BLOCK_NAME_CAMEL_CASE,
  submitting: loading.effects['BLOCK_NAME_CAMEL_CASE/submit'],
40 41
}))
@Form.create()
42
class PAGE_NAME_UPPER_CAMEL_CASE 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, BLOCK_NAME_CAMEL_CASE } = this.props;
陈帅's avatar
陈帅 committed
53
    const account = form.getFieldValue('mail');
54
    if (BLOCK_NAME_CAMEL_CASE.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);
ddcat1115's avatar
ddcat1115 committed
78
  };
79 80

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

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

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

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

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

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

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

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

337
export default PAGE_NAME_UPPER_CAMEL_CASE;