Register.js 7.8 KB
Newer Older
1 2
import React, { Component } from 'react';
import { connect } from 'dva';
Z
zinkey 已提交
3 4
import Link from 'umi/link';
import router from 'umi/router';
5 6 7 8
import { Form, Input, Button, Select, Row, Col, Popover, Progress } from 'antd';
import styles from './Register.less';

const FormItem = Form.Item;
A
afc163 已提交
9
const { Option } = Select;
10 11 12
const InputGroup = Input.Group;

const passwordStatusMap = {
A
afc163 已提交
13 14
  ok: <div className={styles.success}>强度</div>,
  pass: <div className={styles.warning}>强度</div>,
J
jim 已提交
15
  poor: <div className={styles.error}>强度太短</div>,
16 17 18 19 20
};

const passwordProgressMap = {
  ok: 'success',
  pass: 'normal',
J
jim 已提交
21
  poor: 'exception',
22 23
};

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

J
jim 已提交
38
  componentDidUpdate() {
陈帅 已提交
39 40 41 42
    const { form, register, dispatch } = this.props;
    const account = form.getFieldValue('mail');
    if (register.status === 'ok') {
      dispatch(
Z
zinkey 已提交
43
        router.push({
愚道 已提交
44
          pathname: '/user/register-result',
J
jim 已提交
45 46 47 48 49
          state: {
            account,
          },
        })
      );
50 51
    }
  }
陈帅 已提交
52

53 54 55 56 57 58 59 60 61 62 63 64 65 66
  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);
D
ddcat1115 已提交
67
  };
68 69

  getPasswordStatus = () => {
A
afc163 已提交
70
    const { form } = this.props;
71 72 73 74 75 76 77
    const value = form.getFieldValue('password');
    if (value && value.length > 9) {
      return 'ok';
    }
    if (value && value.length > 5) {
      return 'pass';
    }
J
jim 已提交
78
    return 'poor';
D
ddcat1115 已提交
79
  };
80

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

J
jim 已提交
98
  handleConfirmBlur = e => {
A
afc163 已提交
99
    const { value } = e.target;
陈帅 已提交
100 101
    const { confirmDirty } = this.state;
    this.setState({ confirmDirty: confirmDirty || !!value });
D
ddcat1115 已提交
102
  };
103 104

  checkConfirm = (rule, value, callback) => {
A
afc163 已提交
105
    const { form } = this.props;
106 107 108 109 110
    if (value && value !== form.getFieldValue('password')) {
      callback('两次输入的密码不匹配!');
    } else {
      callback();
    }
D
ddcat1115 已提交
111
  };
112 113

  checkPassword = (rule, value, callback) => {
陈帅 已提交
114
    const { visible, confirmDirty } = this.state;
115 116 117 118 119 120 121 122 123 124
    if (!value) {
      this.setState({
        help: '请输入密码!',
        visible: !!value,
      });
      callback('error');
    } else {
      this.setState({
        help: '',
      });
陈帅 已提交
125
      if (!visible) {
126 127 128 129 130 131 132
        this.setState({
          visible: !!value,
        });
      }
      if (value.length < 6) {
        callback('error');
      } else {
A
afc163 已提交
133
        const { form } = this.props;
陈帅 已提交
134
        if (value && confirmDirty) {
135 136 137 138 139
          form.validateFields(['confirm'], { force: true });
        }
        callback();
      }
    }
D
ddcat1115 已提交
140 141
  };

J
jim 已提交
142
  changePrefix = value => {
D
ddcat1115 已提交
143 144 145 146
    this.setState({
      prefix: value,
    });
  };
147 148

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

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