前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >React人机验证控件

React人机验证控件

作者头像
fanzhh
发布2019-08-20 11:35:04
1.7K0
发布2019-08-20 11:35:04
举报
文章被收录于专栏:fanzhh的技术笔记

在使用React做前端,用户注册页面因为要短信验证,短信服务商要求加人机验证,于是我找到了 react-captcha-generator

感谢作者,非常好用,但是……短信服务商认为这个生成的图片太简单,“很简单,再加上几条横线就行……”。但怎么加横线呢?先找别的人机验证轮子,实在没有更合适的。自己从头做一个吧,又实在值不当的。

能不能在react-captcha-generator基础上修改呢?打开源码研究,谷歌搜如何在svg上加直线,还真弄成了。以下是代码:

代码语言:javascript
复制
import React, { Component } from 'react';

class ReactCaptchaGenerator extends Component {

    constructor(props) {
        super(props)
        this.state = {
            height: 100,
            width: 100,
            textColor: false,
            fontFamily: 'Verdana',
            fontSize: '30',
            paddingLeft: '20',
            paddingTop: '60',
            lenght: '5',
            background: 'none'
        }
        this.setData = this.setData.bind(this)
    }

    componentWillMount() {
        this.setData()
    }

    setData() {
        this.setState({
            height: this.props.height ? this.props.height : 100,
            width: this.props.width ? this.props.width : 100,
            textColor: this.props.textColor ? this.props.textColor : false,
            fontFamily: this.props.fontFamily ? this.props.fontFamily : 'Verdana',
            fontSize: this.props.fontSize ? this.props.fontSize : '30',
            paddingLeft: this.props.paddingLeft ? this.props.paddingLeft : '20',
            paddingTop: this.props.paddingTop ? this.props.paddingTop : '60',
            lenght: this.props.lenght ? this.props.lenght : '5',
            background: this.props.background ? this.props.background : 'none',
        })

        this.text = []
        this.originText = []
        this.possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        for (var i = 0; i < this.state.lenght; i++) {
            let char = this.possible.charAt(Math.floor(Math.random() * this.possible.length))
            this.text.push(
                `<text 
                    font-family="${this.state.fontFamily}" 
                    font-size="${this.state.fontSize}" 
                    x="${this.state.paddingLeft * i}" 
                    y="${this.state.paddingTop}"
                    fill="${ this.props.textColor ? this.props.textColor : "#" + ((1 << 24) * Math.random() | 0).toString(16)}"
                    transform="rotate(${Math.random() * (5 - 0) + 0})"
                >${char}</text>`
            )
            this.originText.push(
                char
            )
            /* 以下为增加部分*/
            let x1 = Math.floor(Math.random() * this.state.width)
            let x2 = Math.floor(Math.random() * this.state.width)
            let y1 = Math.floor(Math.random() * this.state.height)
            let y2= Math.floor(Math.random() * this.state.height)
            this.text.push(
                `<line x1="${x1}" y1="${y1}" x2="${x2}" y2="${y2}"  style="stroke:rgb(255,0,0);stroke-width:2" />`
            )
           /* 增加部分结束 */
        }
        this.props.result(this.originText.join(''))

    }

    render() {

        return (
            <img
                style={{ background: this.state.background }}
                src={
                    'data:image/svg+xml;base64,' +
                    btoa('<svg ' +
                        'xmlns="http://www.w3.org/2000/svg" ' +
                        'height="' + this.state.height + '" ' +
                        'width="' + this.state.width + '">' +
                        this.text.join() +
                        '</svg>')
                }
                alt="" />
        );
    }
}

export default ReactCaptchaGenerator;

原来的验证图片:

修改前

修改之后的验证图片:

修改后

再次感谢原控件作者Vetal StekolschikovV

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019.08.05 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
短信
腾讯云短信(Short Message Service,SMS)可为广大企业级用户提供稳定可靠,安全合规的短信触达服务。用户可快速接入,调用 API / SDK 或者通过控制台即可发送,支持发送验证码、通知类短信和营销短信。国内验证短信秒级触达,99%到达率;国际/港澳台短信覆盖全球200+国家/地区,全球多服务站点,稳定可靠。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档