react实现图形验证码_react的滑动图片验证码组件的示例代码

业务需求,需要在系统登陆的时候,使用“滑动图片验证码”,来验证操作的不是机器人。

效果图

使用方式

在一般的页面组件引用即可。onReload这个函数一般是用来请求后台图片的。

class App extends Component {

state = {

url: ""

}

componentDidMount() {

this.setState({ url: getImage() })

}

onReload = () => {

this.setState({ url: getImage() })

}

render() {

return (

imageUrl={this.state.url}

onReload={this.onReload}

onMatch={() => {

console.log("code is match")

}}

/>

)

}

}

上代码

// index.js

/**

* @name ImageCode

* @desc 滑动拼图验证

* @author darcrand

* @version 2019-02-26

*

* @param {String} imageUrl 图片的路径

* @param {Number} imageWidth 展示图片的宽带

* @param {Number} imageHeight 展示图片的高带

* @param {Number} fragmentSize 滑动图片的尺寸

* @param {Function} onReload 当点击'重新验证'时执行的函数

* @param {Function} onMath 匹配成功时执行的函数

* @param {Function} onError 匹配失败时执行的函数

*/

import React from "react"

import "./styles.css"

const icoSuccess = require("./icons/success.png")

const icoError = require("./icons/error.png")

const icoReload = require("./icons/reload.png")

const icoSlider = require("./icons/slider.png")

const STATUS_LOADING = 0 // 还没有图片

const STATUS_READY = 1 // 图片渲染完成,可以开始滑动

const STATUS_MATCH = 2 // 图片位置匹配成功

const STATUS_ERROR = 3 // 图片位置匹配失败

const arrTips = [{ ico: icoSuccess, text: "匹配成功" }, { ico: icoError, text: "匹配失败" }]

// 生成裁剪路径

function createClipPath(ctx, size = 100, styleIndex = 0) {

const styles = [

[0, 0, 0, 0],

[0, 0, 0, 1],

[0, 0, 1, 0],

[0, 0, 1, 1],

[0, 1, 0, 0],

[0, 1, 0, 1],

[0, 1, 1, 0],

[0, 1, 1, 1],

[1, 0, 0, 0],

[1, 0, 0, 1],

[1, 0, 1, 0],

[1, 0, 1, 1],

[1, 1, 0, 0],

[1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值