index.js 6.83 KB
Newer Older
FE committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 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 238 239 240 241 242 243 244 245 246 247 248 249 250 251
import React, { Component } from 'react';
import { Formik, Form, Field } from 'formik';
import { Toast } from 'antd-mobile';
import { isEmpty } from 'lodash';
import Captcha from '@/common/Captcha';
import { http, getParam } from '@/utils';
import './index.scss';

class BindPhone extends Component {
  captchaInstance = null;

  constructor(props) {
    super(props),
    this.state = {
      validate: '',
      seconds: 60,
      isFirst: true,
      timer: null,
      isTimer: false, // 是否开始倒计时
      accountInfo: {},
      bindInfo: {},
      country: {
        num: '86'
      }
    }
  }

  componentDidMount() {
    this.initCountryInfo();
  }

  initCountryInfo = () => {
    const { country } = this.props;
    this.setState({
      country
    });
  }

  toFetchCountryNum = () => {
    const { history, hideBindPhone } = this.props;
    hideBindPhone();
    history.push('/country?from=bind');
  }

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

  onVerify = (err, data) => {
    if (err) {
      console.log(err);
    } else {
      this.setState({
        validate: data.validate
      });
    }
  }

  // 获取手机号验证码
  handleToSend = ({tel, code}) => {
    let { validate, seconds, isFirst, isTimer, country: {num = '86'} } = this.state;
    if(validate) {
      if (!isFirst) {
        Toast.info('请重新进行滑块验证', 2, null, false);
        this.captchaInstance.refresh();
        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;
  }

  // 绑定手机
  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', // 宝箱
    };
    http.post(
      `${API.home}/sys/v2/user/bindMobile`,
      {  
        ...params,
        type: 1, // 1:绑定,2:修改绑定
        is_valid: 1, // is_valid	是否验证 1:验证(默认),0不验证
      }
    ).then(res => {
      const { code, data, msg } = res.data;
      if(code === 200 ) {
        if(data.tip_info) {
          handleToConfirmPhone(params, data.tip_info);
        }else {
          successBindPhone();
        }
      }else {
        Toast.info(msg, 2, null, false);
      }
    });
  }

  render() {
    const { desc, skip = 'year' } = this.props;
    const { country, validate, isTimer, seconds } = this.state;
    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
              }
            });
            this.toBindPhone();
          }}
          render={({values: {tel, code}, errors}) => (
            <Form className="popup-form" data-skip={skip}>
              <h2 className="popup-form__title">绑定手机号</h2>
              {
                desc &&
                <div className="poup-form__desc">{desc}</div>
              }
              <div className="popup-form__item">
                <a className="popup-form__button--num" onClick={this.toFetchCountryNum}>
                  +{country.num}
                  <i className="iconfont iconiconfront-69"/>
                </a>
                <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
                className="popup-form__button--bundle"
                data-status={(tel && code && isEmpty(errors))? 'do': 'done'}
                type="submit"
              >
                完成绑定
              </button>
            </Form>
          )}
        />
    )
  }
}

export default BindPhone;