index.js 6.83 KB
Newer Older
FE committed
1 2
import React, { Component } from 'react';
import { Formik, Form, Field } from 'formik';
FE committed
3 4
import { Toast } from 'antd-mobile';
import { isEmpty } from 'lodash';
FE committed
5
import Captcha from '@/common/Captcha';
FE committed
6
import { http, getParam } from '@/utils';
FE committed
7 8 9
import './index.scss';

class BindPhone extends Component {
FE committed
10 11
  captchaInstance = null;

FE committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
  constructor(props) {
    super(props),
    this.state = {
      validate: '',
      seconds: 60,
      isFirst: true,
      timer: null,
      isTimer: false, // 是否开始倒计时
      accountInfo: {},
      bindInfo: {},
      country: {
        num: '86'
      }
    }
  }

FE committed
28 29 30 31 32 33
  componentDidMount() {
    this.initCountryInfo();
  }

  initCountryInfo = () => {
    const { country } = this.props;
FE committed
34
    this.setState({
FE committed
35
      country
FE committed
36 37 38
    });
  }

FE committed
39 40 41
  toFetchCountryNum = () => {
    const { history, hideBindPhone } = this.props;
    hideBindPhone();
FE committed
42
    history.push('/country?from=bind');
FE committed
43 44 45 46 47 48
  }

  getCaptchaInstance = instance => {
    this.captchaInstance = instance;
  }

FE committed
49 50 51 52 53 54 55 56 57 58
  onVerify = (err, data) => {
    if (err) {
      console.log(err);
    } else {
      this.setState({
        validate: data.validate
      });
    }
  }

FE committed
59
  // 获取手机号验证码
FE committed
60
  handleToSend = ({tel, code}) => {
FE committed
61
    let { validate, seconds, isFirst, isTimer, country: {num = '86'} } = this.state;
FE committed
62 63 64
    if(validate) {
      if (!isFirst) {
        Toast.info('请重新进行滑块验证', 2, null, false);
FE committed
65
        this.captchaInstance.refresh();
FE committed
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
        this.setState({
            isFirst: true
        });
        return
      }
      if(!isTimer) {
        if (!tel) {
          Toast.info('手机号码不能为空', 2, null, false);
        }else if(!/^\d+$/.test(tel)) {
          Toast.info('请输入正确格式的手机号码', 2, null, false);
        }else {

          // 获取验证码
          http.post(
            `${API['passport-api']}/m/personal/bindPhoneSendCode`,
            {
              area_code: `00${num}`,
              phone_num: tel
            }
          ).then(res => {
            const { errno, msg } = res.data;
            if(errno === 200) {
              Toast.info('验证码发送成功', 2, null, false);

              // 倒计时
              this.timer = window.setInterval(() => {
                if (seconds <= 0) {
                  window.clearInterval(this.timer);
                  this.setState({
                    isTimer: false,
                    seconds: 60
                  });
                }else {
                  this.setState({
                    isTimer: true,
                    seconds: --seconds
                  });
                }
              }, 1000);

              // 滑块
              this.setState({
                isFirst: false
              })
            }else {
              Toast.info(msg, 2, null, false);
            }
          })
        }
      }
    }
    return false;
  }

  // 绑定手机
FE committed
121 122 123 124 125 126 127 128 129
  toBindPhone = () => {
    const { accountInfo: { tel, code }, country: {num = '86'}  } = this.state;
    const { handleToConfirmPhone, successBindPhone } = this.props;
    const params = {
      area_code: `00${num}`,
      mobile: tel,
      code: code,
      act_type: 'treasure', // 宝箱
    };
FE committed
130
    http.post(
FE committed
131 132 133 134 135
      `${API.home}/sys/v2/user/bindMobile`,
      {  
        ...params,
        type: 1, // 1:绑定,2:修改绑定
        is_valid: 1, // is_valid	是否验证 1:验证(默认),0不验证
FE committed
136 137
      }
    ).then(res => {
FE committed
138 139
      const { code, data, msg } = res.data;
      if(code === 200 ) {
FE committed
140 141
        if(data.tip_info) {
          handleToConfirmPhone(params, data.tip_info);
FE committed
142
        }else {
FE committed
143
          successBindPhone();
FE committed
144 145 146 147 148 149 150
        }
      }else {
        Toast.info(msg, 2, null, false);
      }
    });
  }

FE committed
151
  render() {
FE committed
152
    const { desc, skip = 'year' } = this.props;
FE committed
153
    const { country, validate, isTimer, seconds } = this.state;
FE committed
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
    return (
        <Formik
          initialValues={{
            tel: '',
            code: ''
          }}
          validate={({tel, code}) => {
            const errors = {};

            // if (!validateTel(tel)) {
            if(!/^\d+$/.test(tel)) {
              errors.tel = '请填写正确格式的手机号';
            }
            if (!/[0-9]{6}/.test(code)) {
              errors.code = '请输入验证码';
            }

            return errors;
          }}
          onSubmit={(values, { setStatus, setSubmitting }) => {
            this.setState({
              accountInfo: {
                ...values
              }
            });
FE committed
179
            this.toBindPhone();
FE committed
180 181
          }}
          render={({values: {tel, code}, errors}) => (
FE committed
182
            <Form className="popup-form" data-skip={skip}>
FE committed
183
              <h2 className="popup-form__title">绑定手机号</h2>
FE committed
184 185 186 187
              {
                desc &&
                <div className="poup-form__desc">{desc}</div>
              }
FE committed
188
              <div className="popup-form__item">
FE committed
189
                <a className="popup-form__button--num" onClick={this.toFetchCountryNum}>
FE committed
190 191
                  +{country.num}
                  <i className="iconfont iconiconfront-69"/>
FE committed
192
                </a>
FE committed
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
                <Field
                  name="tel"
                  render={({field}) => {
                    return (
                      <input
                        {...field}
                        className="popup-form__ipt"
                        data-type="tel"
                        type="text"
                        placeholder="请填写手机号"
                      />
                    );
                  }}
                />
              </div>
              <Captcha
                getInstance={this.getCaptchaInstance}
                onVerify={this.onVerify}
              />
              <div className="popup-form__item">
                <Field
                  name="code"
                  render={({field}) => {
                    return (
                      <input
                        {...field}
                        className="popup-form__ipt popup-form__ipt--left"
                        type="text"
                        placeholder="输入验证码"
                      />
                    );
                  }}
                />
                <button
                  className="popup-form__button--code"
                  data-status={(validate && !isTimer)? 'do': ''}
                  type="button"
                  onClick={() => this.handleToSend({tel, code})}
                >
                  {
                    isTimer? `重新发送${seconds}s` : '发送验证码'
                  }
                </button>
              </div>
              <button
FE committed
238
                className="popup-form__button--bundle"
FE committed
239 240 241 242 243 244 245 246 247 248 249 250
                data-status={(tel && code && isEmpty(errors))? 'do': 'done'}
                type="submit"
              >
                完成绑定
              </button>
            </Form>
          )}
        />
    )
  }
}

FE committed
251
export default BindPhone;