react怎么实现图片验证

react实现图片验证的方法:1、打开相应的react文件;2、通过“randomNum = (min, max) => {…}”方法生成一个随机数;3、通过“drawLine(ctx) {…}”方法绘制干扰线;4、使用“randomCode() {…}”方法随机生成验证码即可。

react怎么实现图片验证

本教程操作环境:Windows10系统、react18版、Dell G3电脑。

react怎么实现图片验证?

react实现图片验证码

效果如图所示:

2a64cfcd3b57f425ae2e1b710cdc298.jpg

import React, { Component } from 'react'import { Icon, Input, Form } from 'antd';class OperationalDataManagement extends Component {  state = {      code: '',      codeLength: 4,      fontSizeMin: 20,      fontSizeMax: 22,      backgroundColorMin: 240,      backgroundColorMax: 250,      colorMin: 10,      colorMax: 20,      lineColorMin: 40,      lineColorMax: 180,      contentWidth: 96,      contentHeight: 38,      showError: false, // 默认不显示验证码的错误信息  }  componentWillMount() {      this.canvas = React.createRef()  }  componentDidMount() {      this.drawPic()  }  // 生成一个随机数  // eslint-disable-next-line arrow-body-style  randomNum = (min, max) => {      return Math.floor(Math.random() * (max - min) + min)  }  drawPic = () => {      this.randomCode()  }  // 生成一个随机的颜色  // eslint-disable-next-line react/sort-comp  randomColor(min, max) {      const r = this.randomNum(min, max)      const g = this.randomNum(min, max)      const b = this.randomNum(min, max)      return `rgb(${r}, ${g}, ${b})`  }  drawText(ctx, txt, i) {      ctx.fillStyle = this.randomColor(this.state.colorMin, this.state.colorMax)      const fontSize = this.randomNum(this.state.fontSizeMin, this.state.fontSizeMax)      ctx.font = fontSize + 'px SimHei'      const padding = 10;      const offset = (this.state.contentWidth - 40) / (this.state.code.length - 1)      let x = padding;      if (i > 0) {          x = padding + (i * offset)      }      let y = this.randomNum(this.state.fontSizeMax, this.state.contentHeight - 5)      if (fontSize > 40) {          y = 40      }      const deg = this.randomNum(-10, 10)      // 修改坐标原点和旋转角度      ctx.translate(x, y)      ctx.rotate(deg * Math.PI / 180)      ctx.fillText(txt, 0, 0)      // 恢复坐标原点和旋转角度      ctx.rotate(-deg * Math.PI / 180)      ctx.translate(-x, -y)  }  drawLine(ctx) {      // 绘制干扰线      for (let i = 0; i  {      this.drawPic()      this.props.form.setFieldsValue({          sendcode: '',      });  }  // 输入验证码  changeCode = e => {      if (e.target.value.toLowerCase() !== '' && e.target.value.toLowerCase() !== this.state.code.toLowerCase()) {          this.setState({              showError: true          })      } else if (e.target.value.toLowerCase() === '') {          this.setState({              showError: false          })      } else if (e.target.value.toLowerCase() === this.state.code.toLowerCase()) {          this.setState({              showError: false          })      }  }  // 随机生成验证码  randomCode() {    let random = ''    // 去掉了I l i o O,可自行添加    const str = 'QWERTYUPLKJHGFDSAZXCVBNMqwertyupkjhgfdsazxcvbnm1234567890'    for (let i = 0; i  {        const canvas = this.canvas.current;        const ctx = canvas.getContext('2d')        ctx.textBaseline = 'bottom'        // 绘制背景        ctx.fillStyle = this.randomColor(this.state.backgroundColorMin, this.state.backgroundColorMax)        ctx.fillRect(0, 0, this.state.contentWidth, this.state.contentHeight)        // 绘制文字        for (let i = 0; i         
                      {getFieldDecorator('sendcode', {                rules: [                  { required: true, message: '请输入校验码!' },                  {                    validator: (rule, value, callback) => {                      if (value) {                        if(value.toLowerCase()===this.state.code.toLowerCase()){                              callback()                                                                                                  this.setState({                                sendcode: value,                                showError: false                              })                        } else {                            callback('请输入正确的验证码')                            this.setState({                                showError: true                            })                        }                      } else {                        callback()                      }                    }                  }                ],                  })(                  }                      onChange={this.changeCode}                      placeholder="请输入校验码"                  />                )}                  
        
                                
          )  }}const WrappedRegistrationForm = Form.create()(OperationalDataManagement);export default WrappedRegistrationForm;

登录后复制

推荐学习:《react视频教程》

以上就是react怎么实现图片验证的详细内容,更多请关注【创想鸟】其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2923680.html

(0)
上一篇 2025年3月11日 18:49:56
下一篇 2025年3月11日 18:50:15

AD推荐 黄金广告位招租... 更多推荐

相关推荐

  • react怎么隐藏滚动条滚动

    react隐藏滚动条滚动的方法:1、打开相应的“react-native”文件;2、通过horizontal设置水平滚动;3、通过设置“showsHorizontalScrollIndicator”的值为“false”来隐藏水平滚动条即可。…

    2025年3月11日
    200
  • react怎么改变组件大小

    react改变组件大小的方法:1、使用“React.cloneElement”加强包裹组件;2、在包裹的组件设置绝对定位,并在组件内加上四个可调整大小的拖动条;3、点击拖动条并进行拖动即会改变DragBox的大小。 本教程操作环境:Wind…

    2025年3月11日
    200
  • react怎么修改值

    react修改值的方法:1、打开相应的前端代码文件,获取标签上的参数;2、在方法调用的地方加一个bind指向;3、用bind改变this方向 ;4、使用state状态来修改值即可。 本教程操作环境:Windows10系统、react18版、…

    2025年3月11日
    200
  • react 怎么实现数组求和

    react实现数组求和的方法:1、创建一个代码示例文件;2、输入“import React,{useState,useEffect} from ‘react’;”;3、使用受控组件,并绑定onChange事件;4、通…

    2025年3月11日
    200
  • react 怎么删除dom元素

    react删除dom元素的方法:1、利用render生命周期来定义一个DOM节点变量;2、通过“onClickS(){this.setState({deled:false})}”实现删除dom元素即可。 本教程操作环境:Windows10系…

    2025年3月11日
    200
  • react native 删除线怎么设置

    react native设置删除线的方法:1、创建一个react示例文件;2、通过“”语句实现添加删除线即可。 本教程操作环境:Windows10系统、react18.0.0版、Dell G3电脑。 react native 删除线怎么设置…

    2025年3月11日
    200
  • react 卡住动不了怎么办

    react卡住动不了的解决办法:1、查看3000端口是否被占用,若是被占用则杀掉进程;2、直接改下“package.json”文件的start配置为“”scripts”: {“start”: …

    2025年3月11日
    200
  • react怎么实现浮动菜单

    react实现浮动菜单的方法:1、利用onMouseOver和onMouseLeave来监听鼠标的变化;2、在样式中设置父类及子类的position值;3、设置父类值为relative,子类值为absolute,并在菜单的css中加入“z-…

    2025年3月11日
    200
  • react中怎么设置focus

    react中设置focus的方法:1、在componentDidMount中使用DOM元素;2、调用“this.input.focus()”的 DOM API即可使整体达到页面加载完成就自动focus到输入框的功能。 本教程操作环境:Win…

    2025年3月11日
    200
  • npm react 安装报错怎么办

    npm react安装报错的解决办法:1、打开项目中的“package.json”文件,找到dependencies对象;2、将其中的“react.json”移动到“devDependencies”;3、在终端中运行“npm audit &…

    2025年3月11日
    200

发表回复

登录后才能评论