index.js 6.67 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';
zhanghaozhe committed
5 6
import CaptchaAli from "src/common/Captcha-ali"
import { http } from 'src/utils';
FE committed
7 8 9
import './index.scss';

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

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

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

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

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

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

zhanghaozhe committed
50 51 52 53 54
  onVerify = (data) => {
    this.setState({
      validate: true,
      validationData: data
    })
FE committed
55 56
  }

FE committed
57
  // 获取手机号验证码
FE committed
58
  handleToSend = ({tel, code}) => {
zhanghaozhe committed
59 60
    let {validate, seconds, validationData, isFirst, isTimer, country: {num = '86'}} = this.state;
    if (validate) {
FE committed
61 62
      if (!isFirst) {
        Toast.info('请重新进行滑块验证', 2, null, false);
zhanghaozhe committed
63
        this.captchaInstance.reset();
FE committed
64
        this.setState({
zhanghaozhe committed
65
          isFirst: true
FE committed
66 67 68
        });
        return
      }
zhanghaozhe committed
69
      if (!isTimer) {
FE committed
70 71
        if (!tel) {
          Toast.info('手机号码不能为空', 2, null, false);
zhanghaozhe committed
72
        } else if (!/^\d+$/.test(tel)) {
FE committed
73
          Toast.info('请输入正确格式的手机号码', 2, null, false);
zhanghaozhe committed
74
        } else {
FE committed
75 76 77 78 79 80

          // 获取验证码
          http.post(
            `${API['passport-api']}/m/personal/bindPhoneSendCode`,
            {
              area_code: `00${num}`,
zhanghaozhe committed
81 82
              phone_num: tel,
              ...validationData
FE committed
83 84
            }
          ).then(res => {
zhanghaozhe committed
85 86
            const {errno, msg} = res.data;
            if (errno === 200) {
FE committed
87 88 89 90 91 92 93 94 95 96
              Toast.info('验证码发送成功', 2, null, false);

              // 倒计时
              this.timer = window.setInterval(() => {
                if (seconds <= 0) {
                  window.clearInterval(this.timer);
                  this.setState({
                    isTimer: false,
                    seconds: 60
                  });
zhanghaozhe committed
97
                } else {
FE committed
98 99 100 101 102 103 104 105 106 107 108
                  this.setState({
                    isTimer: true,
                    seconds: --seconds
                  });
                }
              }, 1000);

              // 滑块
              this.setState({
                isFirst: false
              })
zhanghaozhe committed
109
            } else {
FE committed
110 111 112 113 114 115 116 117 118 119
              Toast.info(msg, 2, null, false);
            }
          })
        }
      }
    }
    return false;
  }

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

FE committed
150
  render() {
zhanghaozhe committed
151 152
    const {desc, skip = 'year'} = this.props;
    const {country, validate, isTimer, seconds} = this.state;
FE committed
153
    return (
zhanghaozhe committed
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
      <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
176
            }
zhanghaozhe committed
177 178 179 180 181 182 183 184 185
          });
          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>
FE committed
186
            }
zhanghaozhe committed
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
            <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>
207
            <CaptchaAli getInstance={this.getCaptchaInstance} onVerify={this.onVerify} mb={15}/>
zhanghaozhe committed
208 209 210 211 212 213 214 215 216 217 218 219 220
            <div className="popup-form__item">
              <Field
                name="code"
                render={({field}) => {
                  return (
                    <input
                      {...field}
                      className="popup-form__ipt popup-form__ipt--left"
                      type="text"
                      placeholder="输入验证码"
                    />
                  );
                }}
FE committed
221 222
              />
              <button
zhanghaozhe committed
223 224 225 226
                className="popup-form__button--code"
                data-status={(validate && !isTimer) ? 'do' : ''}
                type="button"
                onClick={() => this.handleToSend({tel, code})}
FE committed
227
              >
zhanghaozhe committed
228 229 230
                {
                  isTimer ? `重新发送${seconds}s` : '发送验证码'
                }
FE committed
231
              </button>
zhanghaozhe committed
232 233 234 235 236 237 238 239 240 241 242
            </div>
            <button
              className="popup-form__button--bundle"
              data-status={(tel && code && isEmpty(errors)) ? 'do' : 'done'}
              type="submit"
            >
              完成绑定
            </button>
          </Form>
        )}
      />
FE committed
243 244 245 246
    )
  }
}

FE committed
247
export default BindPhone;