vue 验证码

本文介绍如何创建一个Vue验证码组件,并提供代码示例,指导如何在其他页面中引入并使用该组件,主要涉及变量identifyCode作为验证码的传递。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

验证码组件

创建一个验证码的vue组件
将下列代码复制到主机中在其他页面引入

identifyCode为验证码,传入字符串即可

<template>
  <div class="s-canvas">
    <canvas id="s-canvas" :width="contentWidth" :height="contentHeight"></canvas>
  </div>
</template>
<script> 
export default {
  name: 'identify',
  props: {
    identifyCode: {
      //默认注册码
      type: String,
      default: '1234'
    },
    fontSizeMin: {
      // 字体最小值
      type: Number,
      default: 25
    },
    fontSizeMax: {
      // 字体最大值
      type: Number,
      default: 35
    },
    backgroundColorMin: {
      // 验证码图片背景色最小值
      type: Number,
      default: 200
    },
    backgroundColorMax: {
      // 验证码图片背景色最大值
      type: Number,
      default: 220
    },
    dotColorMin: {
      // 背景干扰点最小值
      type: Number,
      default: 60
    },
    dotColorMax: {
      // 背景干扰点最大值
      type: Number,
      default: 120
    },
    contentWidth: {
      //容器宽度
      type: Number,
      default: 116
    },
    contentHeight: {
      //容器高度
      type: Number,
      default: 38
    }
  },
  methods: {
    // 生成一个随机数
    randomNum(min, max) {
      return Math.floor(Math.random() * (max - min) + min)
    },
    // 生成一个随机的颜色
    randomColor(min, max) {
      let r = this.randomNum(min, max)
      let g = this.randomNum(min, max)
      let b = this.randomNum(min, max)
      return 'rgb(' + r + ',' + g + ',' + b + ')'
    },
    drawPic() {
      let canvas = document.getElementById('s-canvas')
      let ctx = canvas.getContext('2d')
      ctx.textBaseline = 'bottom'
      // 绘制背景
      ctx.fillStyle = this.randomColor(
        this.backgroundColorMin,
        this.backgroundColorMax
      )
      ctx.fillRect(0, 0, this.contentWidth, this.contentHeight)
      // 绘制文字
      for (let i = 0; i < this.identifyCode.length; i++) {
        this.drawText(ctx, this.identifyCode[i], i)
      }
      this.drawLine(ctx)
      this.drawDot(ctx)
    },
    drawText(ctx, txt, i) {
      ctx.fillStyle = this.randomColor(50, 160) //随机生成字体颜色
      ctx.font =
        this.randomNum(this.fontSizeMin, this.fontSizeMax) + 'px SimHei' //随机生成字体大小
      let x = (i + 1) * (this.contentWidth / (this.identifyCode.length + 1))
      let y = this.randomNum(this.fontSizeMax, this.contentHeight - 5)
      var deg = this.randomNum(-30, 30)
      // 修改坐标原点和旋转角度
      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 < 4; i++) {
        ctx.strokeStyle = this.randomColor(100, 200)
        ctx.beginPath()
        ctx.moveTo(
          this.randomNum(0, this.contentWidth),
          this.randomNum(0, this.contentHeight)
        )
        ctx.lineTo(
          this.randomNum(0, this.contentWidth),
          this.randomNum(0, this.contentHeight)
        )
        ctx.stroke()
      }
    },
    drawDot(ctx) {
      // 绘制干扰点
      for (let i = 0; i < 30; i++) {
        ctx.fillStyle = this.randomColor(0, 255)
        ctx.beginPath()
        ctx.arc(
          this.randomNum(0, this.contentWidth),
          this.randomNum(0, this.contentHeight),
          1,
          0,
          2 * Math.PI
        )
        ctx.fill()
      }
    }
  },
  watch: {
    identifyCode() {
      this.drawPic()
    }
  },
  mounted() {
    this.drawPic()
  }
}
</script>
### Vue.js 中实现验证码功能的组件 在 Vue.js 应用程序中,可以通过 `vue-verifycode` 插件来方便地实现验证码的功能。安装插件后,在应用程序的入口文件中引入该模块: ```javascript import Vue from 'vue'; import VerifyCode from 'vue-verifycode'; Vue.use(VerifyCode); ``` 随后可以在任何需要使用的组件内通过如下方式调用此组件[^1]。 #### 使用示例 下面是一个简单的例子展示如何在一个表单页面里加入验证码输入框以及对应的验证图片显示区域: ```html <template> <div class="form-group"> <!-- 显示验证码 --> <verify-code ref="vc"></verify-code> <!-- 用户输入验证码的地方 --> <input type="text" v-model="captchaInput"> <!-- 提交按钮触发校验逻辑 --> <button @click.prevent="checkCaptcha">提交</button> {{ message }} </div> </template> <script> export default { data() { return { captchaInput: '', message: '' } }, methods: { checkCaptcha() { const isValid = this.$refs.vc.validate(this.captchaInput); if (isValid) { this.message = "验证码正确"; } else { this.message = "验证码错误,请重新尝试"; } // 清除当前输入以便再次尝试 this.captchaInput = ''; // 更新验证码图像以防缓存影响 this.$refs.vc.refresh(); } } } </script> ``` 上述代码片段展示了如何创建一个包含验证码控件的表单位置,并定义了一个方法用于检验用户所填写的内容是否匹配生成的图形验证码。如果匹配成功,则给出相应的提示;反之则告知用户输入有误并允许其重试。每次点击刷新或失败后都会自动生成新的验证码供下一轮验证使用[^3]。 此外,对于更复杂的场景比如跨域请求时遇到的问题也可以借助 vue.config.js 文件中的代理设置解决,确保前后端之间的正常通信[^5]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值