index.js 3.63 KB
Newer Older
zhanghaozhe committed
1 2 3 4 5
import React, { Component } from "react"
import "./veri-code-input.scss"
import { http, validateEmail } from "src/utils"
import { Toast } from "antd-mobile"
import { has, isEmpty } from "lodash"
zhanghaozhe committed
6

zhanghaozhe committed
7
import classnames from "classnames"
zhanghaozhe committed
8

zhanghaozhe committed
9
import Input from "../Input"
zhanghaozhe committed
10 11

class VeriCodeInput extends Component {
zhanghaozhe committed
12 13 14 15
  count = 60
  state = {
    counting: false,
    count: this.count,
zhanghaozhe committed
16
    isFirst: true,
zhanghaozhe committed
17 18 19 20
  }
  timer = null

  countDown = () => {
zhanghaozhe committed
21
    let { count } = this.state
zhanghaozhe committed
22
    if (!this.state.isFirst) {
zhanghaozhe committed
23
      Toast.info("请重新进行滑块验证", 2, null, false)
zhanghaozhe committed
24 25
      this.props.instance.reset()
      this.setState({
zhanghaozhe committed
26
        isFirst: true,
zhanghaozhe committed
27 28
      })
      return
zhanghaozhe committed
29 30
    }

zhanghaozhe committed
31 32 33 34
    if (!this.state.counting) {
      if (!this.sendCode()) {
        return
      }
zhanghaozhe committed
35
      this.setState({ count: count--, counting: true })
zhanghaozhe committed
36 37 38
      this.timer = setInterval(() => {
        if (count <= 0) {
          clearInterval(this.timer)
zhanghaozhe committed
39
          this.setState({ counting: false, count: this.count })
zhanghaozhe committed
40
          return
zhanghaozhe committed
41
        }
zhanghaozhe committed
42
        this.setState({ count: count-- })
zhanghaozhe committed
43
      }, 1000)
zhanghaozhe committed
44
    }
zhanghaozhe committed
45
  }
zhanghaozhe committed
46

zhanghaozhe committed
47
  getType = () => {
zhanghaozhe committed
48
    const { email } = this.props
zhanghaozhe committed
49
    if (validateEmail(email)) {
zhanghaozhe committed
50
      return "email"
zhanghaozhe committed
51
    }
zhanghaozhe committed
52 53 54 55
  }

  sendCode = () => {
    if (!this.validate()) return
zhanghaozhe committed
56 57
    this.getType() === "email" ? this.sendEmail() : this.sendSMS()
    return true
zhanghaozhe committed
58 59 60
  }

  sendEmail = () => {
zhanghaozhe committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
    const { email, validationData } = this.props
    http
      .post(`${API["passport-api"]}/send_email_code`, {
        email,
        ...validationData,
      })
      .then((res) => {
        if (res.data.errno === 0) {
          Toast.info("验证码发送成功", 2, null, false)
        } else {
          Toast.info(res.data.msg, 2, null, false)
        }
        this.setState({
          isFirst: false,
        })
zhanghaozhe committed
76 77 78 79
      })
  }

  sendSMS = () => {
zhanghaozhe committed
80 81 82 83 84 85 86 87
    const {
      action,
      tel,
      account,
      validationData,
      checking,
      country,
    } = this.props
zhanghaozhe committed
88
    if (!tel) {
zhanghaozhe committed
89
      Toast.info("请输入手机号")
zhanghaozhe committed
90
      return
zhanghaozhe committed
91
    }
zhanghaozhe committed
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
    http
      .post(`${API["passport-api"]}/quick_sms`, {
        phone_num: tel || account,
        action: action || "login",
        area_code: "00" + country.num,
        checking,
        ...validationData,
      })
      .then((res) => {
        if (res.data.errno === 0) {
          Toast.info("验证码发送成功", 2, null, false)
        } else {
          Toast.info(res.data.msg, 2, null, false)
        }
        this.setState({
          isFirst: false,
        })
zhanghaozhe committed
109 110 111 112
      })
  }

  validate = () => {
zhanghaozhe committed
113 114
    const { tel, validationData, email } = this.props
    let hasTel = has(this.props, "tel")
zhanghaozhe committed
115 116 117 118
    let content

    if (hasTel) {
      if (!tel) {
zhanghaozhe committed
119
        content = "手机号码不能为空"
zhanghaozhe committed
120 121
      }
      if (!/\d/.test(tel)) {
zhanghaozhe committed
122
        content = "请输入正确格式的手机号码"
zhanghaozhe committed
123 124 125
      }
    } else {
      if (!email) {
zhanghaozhe committed
126
        content = "电子邮件不能为空"
zhanghaozhe committed
127 128
      }
      if (!validateEmail(email)) {
zhanghaozhe committed
129
        content = "请输入正确格式的电子邮件"
zhanghaozhe committed
130
      }
zhanghaozhe committed
131
    }
zhanghaozhe committed
132

zhanghaozhe committed
133 134 135
    if (content) {
      Toast.info(content, 2, null, false)
      return false
zhanghaozhe committed
136 137
    }

zhanghaozhe committed
138
    if (isEmpty(validationData)) {
zhanghaozhe committed
139
      Toast.info("请进行滑块验证", 2, null, false)
zhanghaozhe committed
140
      return false
zhanghaozhe committed
141
    }
zhanghaozhe committed
142 143 144 145
    return true
  }

  render() {
zhanghaozhe committed
146
    let { className, validationData, ...rest } = this.props
zhanghaozhe committed
147
    return (
zhanghaozhe committed
148 149 150 151 152 153 154
      <Input type={"number"} wrapperClass={className} {...rest}>
        <button
          type="button"
          className={classnames("verify", { active: !this.state.counting })}
          onClick={this.countDown}
        >
          {this.state.counting ? `重新发送${this.state.count}s` : "发送验证码"}
zhanghaozhe committed
155 156
        </button>
      </Input>
zhanghaozhe committed
157
    )
zhanghaozhe committed
158
  }
zhanghaozhe committed
159 160
}

zhanghaozhe committed
161
export default VeriCodeInput