封装的一个随机验证码的VUE组件

本文介绍了一款自创的验证码组件,适用于Vue项目。组件具备一定的灵活性,可通过参数配置验证码的字体大小、宽度、高度等属性。文章详细展示了组件的代码实现,包括生成随机验证码、绘制验证码图像等功能。

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

由于工作需要自己做了一个验证码组件,灵活性不高,但是可以用,代码也不太复杂

<template>
  <div style="display: flex;justify-content: start;align-items: center;border-radius: 4px">
    <canvas style="" :width="contentWidth" :height="contentHeight" id="cav" @click="next()"></canvas>
  </div>
</template>

<script>
// import num from './components/cc'
export default {
  name: 'verificationCode',
  props: {
    fontSize: {
      type: Number,
      default: 20
    },
    contentWidth: {
      type: Number,
      default: 100
    },
    contentHeight: {
      type: Number,
      default: 40
    },
    verification: {
      type: String
    },
    refreshCode: {
      type: Boolean
    }
  },
  watch: {
    verification: function (newVal) {
      if (newVal.toLowerCase() === this.identify.toLowerCase()) {
        this.$emit('handleIdentify', true)
      } else {
        this.$emit('handleIdentify', false)
      }
    },
    refreshCode: function (newVal) {
      this.draw()
    }
  },
  mounted () {
    this.draw()
  },
  data () {
    return {
      identify: '',
      letter: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
    }
  },
  methods: {
    draw () {
      let self = this
      let text = this.randomNum()
      this.identify = text.join('')
      let ctx = document.getElementById('cav')
      let cav = ctx.getContext('2d')
      cav.clearRect(0, 0, self.contentWidth, self.contentHeight)
      let w = ctx.width
      let h = ctx.height
      cav.textBaseline = 'bottom'
      self.drawText(cav, text[0], 10, 10, self.randomDeg(0, 0.1), 20)
      self.drawText(cav, text[1], 20, 10, self.randomDeg(0, 0.1))
      self.drawText(cav, text[2], 25, 10, self.randomDeg(0, 0.1))
      self.drawText(cav, text[3], 30, 10, self.randomDeg(0, 0.1))
      let i
      for (i = 0; i < 3; i++) {
        self.drawLine(cav, self.randomDeg(0, 0.3 * w, 1), self.randomDeg(0, h, 1), self.randomDeg(0, w, 1), self.randomDeg(0, h, 1), self.randomDeg(0.8 * w, w, 1), self.randomDeg(0, h, 1))
      }
    },
    drawText (cav, text, x1, y1, route) {
      cav.beginPath()
      cav.fillStyle = '#ffe9db'
      cav.font = this.fontSize + 'px' + ' ' + '黑体'
      cav.translate(x1, y1)
      cav.rotate(Math.PI * route)
      cav.fillText(text, x1, y1)
      cav.fill()
      cav.rotate(-Math.PI * route)
      cav.translate(-(x1), -(y1))
    },
    drawLine (cav, x1, y1, x2, y2, x3, y3) {
      cav.beginPath()
      cav.strokeStyle = 'rgb(155, 204, 237)'
      cav.bezierCurveTo(x1, y1, x2, y2, x3, y3)
      cav.stroke()
    },
    randomNum () { // 生成随机字
      let i
      let letter = this.letter
      let text = []
      for (i = 0; i < 4; i++) {
        text.push(letter[Math.floor(Math.random() * 36)])
      }
      return text
    },
    randomDeg (min, max, f) { // 设置文字倾斜角度
      f = undefined ? Math.random() > 0.5 ? 1 : -1 : 1
      return f * (Math.random() * (max - min) + min)
    },
    next () {
      this.draw()
    }
  }
}
</script>

<style scoped>

</style>

样式什么的自己可以调一调,效果如下:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值