index.js 5.21 KB
Newer Older
FE committed
1 2 3 4 5
import React, { Component } from 'react';
import classnames from 'classnames';
import { Link } from 'react-router-dom';
import { Formik, Form, Field } from 'formik';
import { Toast } from 'antd-mobile';
zhanghaozhe committed
6
import { http, getParam } from 'src/utils';
FE committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
import './index.scss';

class BargainBindPhone extends Component {

  constructor(props) {
    super(props);
    this.state = {
      mobile: '',
      code: '',
      num: '86',
      seconds: 60,
      isTimer: false, // 是否开始倒计时
    };
  }

  // 获取短信验证码
zhanghaozhe committed
23 24 25 26 27
  handleToSendCode = ({mobile}) => {
    const {country: {num = 86}} = this.props;
    let {isTimer, seconds} = this.state;
    if (!isTimer) {
      if (!/^\d+$/.test(mobile)) {
FE committed
28 29 30 31 32 33 34 35 36
        Toast.info('请输入正确的手机号');
        return;
      }

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

FE committed
44 45 46 47 48 49
          // 倒计时
          this.timer = window.setInterval(() => {
            if (seconds <= 0) {
              window.clearInterval(this.timer);
              this.setState({
                isTimer: false,
zhanghaozhe committed
50
                seconds: 60,
FE committed
51
              });
zhanghaozhe committed
52
            } else {
FE committed
53 54
              this.setState({
                isTimer: true,
zhanghaozhe committed
55
                seconds: --seconds,
FE committed
56 57 58
              });
            }
          }, 1000);
zhanghaozhe committed
59
        } else {
FE committed
60 61 62 63 64 65 66 67
          Toast.info(msg);
        }
      });
    }
  }

  // 绑定手机号
  handleToBindPhone = ({code, mobile}) => {
zhanghaozhe committed
68
    const {country: {num = 86}, handleToBargain, confirmBindPhone} = this.props;
FE committed
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
    if (!mobile) {
      Toast.info('请填手机号码');
      return;
    }
    if (!code) {
      Toast.info('请填写验证码');
      return;
    }

    const params = {
      area_code: `00${num}`,
      phone_num: mobile,
      code: code,
    };

    // is_valid	是否验证 1:验证(默认),0不验证
    http.post(
      `${API['passport-api']}/m/personal/bindPhone`,
      {
        ...params,
        type: 1,
zhanghaozhe committed
90 91
        is_valid: 1,
      },
FE committed
92
    ).then(res => {
zhanghaozhe committed
93 94 95
      const {errno, data, msg} = res.data;
      if (errno === 200) {
        if (data.tip_info) {
FE committed
96
          confirmBindPhone(params, data.tip_info);
zhanghaozhe committed
97
        } else {
FE committed
98 99
          handleToBargain();
        }
zhanghaozhe committed
100
      } else {
FE committed
101 102 103 104 105 106
        Toast.info(msg);
      }
    });
  }

  render() {
zhanghaozhe committed
107 108
    const {country: {num = '86'}} = this.props;
    const {isTimer, seconds} = this.state;
FE committed
109 110 111 112
    return (
      <Formik
        initialValues={{
          mobile: '',
zhanghaozhe committed
113
          code: '',
FE committed
114 115 116
        }}
        validate={({mobile, code}) => {
          const errors = {};
zhanghaozhe committed
117
          if (!/^\d+$/.test(mobile)) {
FE committed
118 119 120 121 122 123 124
            errors.mobile = '请填写正确格式的手机号';
          }
          if (!/[0-9]{6}/.test(code)) {
            errors.code = '请输入验证码';
          }
          return errors;
        }}
zhanghaozhe committed
125
        onSubmit={(values, {setStatus, setSubmitting}) => {
FE committed
126 127
          this.handleToBindPhone(values);
        }}
zhanghaozhe committed
128 129
      >
        {({values: {mobile, code}, errors}) => (
FE committed
130 131 132 133 134 135 136 137 138 139 140 141
          <Form className="bargain-bind-phone">
            <h2 className="bargain-bind-phone__title">绑定手机,先砍一刀</h2>
            <div className="bargain-bind-phone__item">
              <Link
                className="bargain-bind-phone__button--num"
                to={`/country?id=${getParam('id')}&from=bargain`}
              >
                +{num}
                <i className="iconfont iconiconfront-69"></i>
              </Link>
              <Field
                name="mobile"
zhanghaozhe committed
142 143 144
              >
                {({field}) => (
                  <input
FE committed
145 146 147
                    {...field}
                    className="bargain-bind-phone__ipt"
                    type="tel"
zhanghaozhe committed
148
                    placeholder='手机号'
FE committed
149 150 151
                    maxLength={11}
                  />
                )}
zhanghaozhe committed
152
              </Field>
FE committed
153 154
            </div>
            <div className="bargain-bind-phone__item">
zhanghaozhe committed
155 156 157
              <Field name="code">
                {({field}) => (
                  <input
FE committed
158 159 160 161 162 163
                    {...field}
                    type="tel"
                    placeholder='验证码'
                    maxLength={6}
                  />
                )}
zhanghaozhe committed
164
              </Field>
FE committed
165
              {errors.mobile}
zhanghaozhe committed
166
              <button
FE committed
167 168 169 170
                type="button"
                className={classnames(
                  'bargain-bind-phone__button--send',
                  {
zhanghaozhe committed
171 172
                    'active': mobile && errors.mobile === undefined,
                  },
FE committed
173 174 175 176
                )}
                disabled={!(mobile && errors.mobile === undefined)}
                onClick={() => this.handleToSendCode({mobile})}
              >
zhanghaozhe committed
177
                {isTimer ? `重新发送${seconds}s` : '发送验证码'}
FE committed
178 179
              </button>
            </div>
zhanghaozhe committed
180
            <button
FE committed
181 182 183
              type="submit"
              className="bargain-bind-phone__button--bargain"
              disabled={!(mobile && code && JSON.stringify(errors) === '{}')}
zhanghaozhe committed
184 185
            >先砍一刀
            </button>
FE committed
186 187
          </Form>
        )}
zhanghaozhe committed
188
      </Formik>
FE committed
189 190 191 192 193
    )
  }
}

export default BargainBindPhone;