index.js 1.6 KB
Newer Older
zhanghaozhe committed
1
/* global initNECaptcha */
zhanghaozhe committed
2 3 4 5
import React, { Component } from "react"
import { initCaptcha } from "src/utils"
import { BarLoader } from "react-spinners"
import "./index.scss"
zhanghaozhe committed
6

zhanghaozhe committed
7
const CAPTCHAID = "6b0f5f6c8f334f3693ee754ba5692e36"
zhanghaozhe committed
8 9

class Captcha extends Component {
zhanghaozhe committed
10 11 12
  state = {
    isReady: false,
  }
FE committed
13

zhanghaozhe committed
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
  componentDidMount() {
    const { getInstance, handleError, onVerify } = this.props
    const _this = this
    const el = document.getElementById("captcha")
    el &&
      initCaptcha(function () {
        initNECaptcha(
          {
            element: el,
            captchaId: CAPTCHAID,
            mode: "float",
            width: "auto",
            onReady: function (instance) {
              // 验证码一切准备就绪,此时可正常使用验证码的相关功能
              _this.setState({
                isReady: true,
              })
            },
            onVerify: function (err, data) {
              onVerify(err, data)
            },
          },
          (instance) => {
            getInstance && getInstance(instance)
          },
          (err) => {
            handleError && handleError(err)
          }
        )
      })
  }
zhanghaozhe committed
45

zhanghaozhe committed
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
  render() {
    return (
      <div
        className="captcha-container"
        style={{
          marginBottom: this.props.mrBtm,
        }}
      >
        {!this.state.isReady && (
          <div className="captcha-animation">
            <BarLoader />
          </div>
        )}
        <div
          id={"captcha"}
          style={{
            marginBottom: this.props.mrBtm,
          }}
        />
      </div>
    )
  }
zhanghaozhe committed
68 69
}

zhanghaozhe committed
70
export default Captcha