index.js 6.82 KB
Newer Older
zhanghaozhe committed
1 2 3 4
import React, { Component } from "react"
import { Formik, Form, Field } from "formik"
import { Toast } from "antd-mobile"
import { isEmpty } from "lodash"
zhanghaozhe committed
5
import CaptchaAli from "src/common/Captcha-ali"
zhanghaozhe committed
6 7
import { http } from "src/utils"
import "./index.scss"
FE committed
8 9

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

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

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

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

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

zhanghaozhe committed
46 47
  getCaptchaInstance = (instance) => {
    this.captchaInstance = instance
FE committed
48 49
  }

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

FE committed
57
  // 获取手机号验证码
zhanghaozhe committed
58 59 60 61 62 63 64 65 66
  handleToSend = ({ tel, code }) => {
    let {
      validate,
      seconds,
      validationData,
      isFirst,
      isTimer,
      country: { num = "86" },
    } = this.state
zhanghaozhe committed
67
    if (validate) {
FE committed
68
      if (!isFirst) {
zhanghaozhe committed
69 70
        Toast.info("请重新进行滑块验证", 2, null, false)
        this.captchaInstance.reset()
FE committed
71
        this.setState({
zhanghaozhe committed
72 73
          isFirst: true,
        })
FE committed
74 75
        return
      }
zhanghaozhe committed
76
      if (!isTimer) {
FE committed
77
        if (!tel) {
zhanghaozhe committed
78
          Toast.info("手机号码不能为空", 2, null, false)
zhanghaozhe committed
79
        } else if (!/^\d+$/.test(tel)) {
zhanghaozhe committed
80
          Toast.info("请输入正确格式的手机号码", 2, null, false)
zhanghaozhe committed
81
        } else {
FE committed
82
          // 获取验证码
zhanghaozhe committed
83 84
          http
            .post(`${API["passport-api"]}/m/personal/bindPhoneSendCode`, {
FE committed
85
              area_code: `00${num}`,
zhanghaozhe committed
86
              phone_num: tel,
zhanghaozhe committed
87 88 89 90 91 92
              ...validationData,
            })
            .then((res) => {
              const { errno, msg } = res.data
              if (errno === 200) {
                Toast.info("验证码发送成功", 2, null, false)
FE committed
93

zhanghaozhe committed
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
                // 倒计时
                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)
FE committed
109

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

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

FE committed
157
  render() {
zhanghaozhe committed
158 159
    const { desc, skip = "year" } = this.props
    const { country, validate, isTimer, seconds } = this.state
FE committed
160
    return (
zhanghaozhe committed
161 162
      <Formik
        initialValues={{
zhanghaozhe committed
163 164
          tel: "",
          code: "",
zhanghaozhe committed
165
        }}
zhanghaozhe committed
166 167
        validate={({ tel, code }) => {
          const errors = {}
zhanghaozhe committed
168 169 170

          // if (!validateTel(tel)) {
          if (!/^\d+$/.test(tel)) {
zhanghaozhe committed
171
            errors.tel = "请填写正确格式的手机号"
zhanghaozhe committed
172 173
          }
          if (!/[0-9]{6}/.test(code)) {
zhanghaozhe committed
174
            errors.code = "请输入验证码"
zhanghaozhe committed
175 176
          }

zhanghaozhe committed
177
          return errors
zhanghaozhe committed
178
        }}
zhanghaozhe committed
179
        onSubmit={(values, { setStatus, setSubmitting }) => {
zhanghaozhe committed
180 181
          this.setState({
            accountInfo: {
zhanghaozhe committed
182 183 184 185
              ...values,
            },
          })
          this.toBindPhone()
zhanghaozhe committed
186
        }}
zhanghaozhe committed
187
      >
zhanghaozhe committed
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
        {({ 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>
            <CaptchaAli
              getInstance={this.getCaptchaInstance}
              onVerify={this.onVerify}
              mb={15}
            />
            <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
234
              <button
zhanghaozhe committed
235 236 237 238
                className="popup-form__button--code"
                data-status={validate && !isTimer ? "do" : ""}
                type="button"
                onClick={() => this.handleToSend({ tel, code })}
FE committed
239
              >
zhanghaozhe committed
240
                {isTimer ? `重新发送${seconds}s` : "发送验证码"}
FE committed
241
              </button>
zhanghaozhe committed
242 243 244 245 246 247 248 249 250 251
            </div>
            <button
              className="popup-form__button--bundle"
              data-status={tel && code && isEmpty(errors) ? "do" : "done"}
              type="submit"
            >
              完成绑定
            </button>
          </Form>
        )}
zhanghaozhe committed
252
      </Formik>
FE committed
253 254 255 256
    )
  }
}

zhanghaozhe committed
257
export default BindPhone