Register.js 7.79 KB
Newer Older
1 2
import React, { Component } from 'react';
import { connect } from 'dva';
zinkey's avatar
zinkey committed
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;
afc163's avatar
afc163 committed
9
const { Option } = Select;
10 11 12
const InputGroup = Input.Group;

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

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

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

jim's avatar
jim committed
38
  componentDidUpdate() {
39
    const { form, register } = this.props;
陈帅's avatar
陈帅 committed
40 41
    const account = form.getFieldValue('mail');
    if (register.status === 'ok') {
42 43 44 45 46 47
      router.push({
        pathname: '/user/register-result',
        state: {
          account,
        },
      });
48 49
    }
  }
陈帅's avatar
陈帅 committed
50

51 52 53 54 55 56 57 58 59 60 61 62 63 64
  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
65
  };
66 67

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

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

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

  checkConfirm = (rule, value, callback) => {
afc163's avatar
afc163 committed
103
    const { form } = this.props;
104 105 106 107 108
    if (value && value !== form.getFieldValue('password')) {
      callback('两次输入的密码不匹配!');
    } else {
      callback();
    }
ddcat1115's avatar
ddcat1115 committed
109
  };
110 111

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

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

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

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

export default Register;