index.js 1.94 KB
Newer Older
zhanghaozhe committed
1 2
import React, { Component } from 'react';
import { initCaptcha } from '@/utils';
FE committed
3 4
import { BarLoader } from 'react-spinners';
import './index.scss';
zhanghaozhe committed
5 6 7 8 9

const CAPTCHAID = '6b0f5f6c8f334f3693ee754ba5692e36'


class Captcha extends Component {
FE committed
10 11 12 13 14

    state = {
        isReady: false
    }

zhanghaozhe committed
15
    componentDidMount() {
FE committed
16 17
        const {getInstance, handleError, onVerify} = this.props;
        const _this = this;
FE committed
18 19 20 21 22 23 24
        const el = document.getElementById('captcha');
        el && initCaptcha(function () {
            initNECaptcha({
                    element: el,
                    captchaId: CAPTCHAID,
                    mode: 'float',
                    width: 'auto',
FE committed
25 26 27 28 29 30
                    onReady: function (instance) {
                        // 验证码一切准备就绪,此时可正常使用验证码的相关功能
                        _this.setState({
                            isReady: true
                        });
                    },
FE committed
31 32 33 34 35 36 37 38 39 40 41 42
                    onVerify: function (err, data) {
                        onVerify(err,data)
                    }
                },
                instance => {
                    getInstance && getInstance(instance)
                },
                err => {
                    handleError && handleError(err)
                }
            )
        })
zhanghaozhe committed
43 44 45 46
    }

    render() {
        return (
FE committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
            <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
66 67 68 69 70
        );
    }
}

export default Captcha;