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

1 篇文章 0 订阅
1 篇文章 0 订阅

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

<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>

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一些思路和代码示例。下面是一个简单的获取验证码组件的示例: ```html <template> <div class="verification-code"> <input type="text" v-model="phoneNumber" placeholder="请输入手机号" /> <button :disabled="countdown > 0" @click="getCode">{{ buttonText }}</button> </div> </template> <script> export default { data() { return { phoneNumber: "", countdown: 0, buttonText: "获取验证码", timer: null, }; }, methods: { getCode() { if (!this.phoneNumber) { alert("请输入手机号!"); return; } // 发送验证码接口 // ... // 模拟倒计时 this.countdown = 60; this.buttonText = `${this.countdown}秒后重新获取`; this.timer = setInterval(() => { this.countdown--; if (this.countdown <= 0) { clearInterval(this.timer); this.timer = null; this.buttonText = "获取验证码"; } else { this.buttonText = `${this.countdown}秒后重新获取`; } }, 1000); }, }, beforeDestroy() { if (this.timer) { clearInterval(this.timer); this.timer = null; } }, }; </script> <style scoped> .verification-code { display: flex; flex-direction: row; justify-content: center; align-items: center; } input { width: 80%; height: 40px; font-size: 16px; padding: 5px; margin-right: 10px; border-radius: 4px; } button { width: 20%; height: 40px; background-color: #409eff; border: none; color: #fff; font-size: 16px; border-radius: 4px; cursor: pointer; } button:disabled { background-color: #b5b5b5; cursor: not-allowed; } </style> ``` 这个组件包含一个输入框和一个按钮,当用户点击按钮时,会触发 `getCode` 方法,该方法会先检查用户输入的手机号,然后向后端发送验证码请求,并开启一个模拟的倒计时,防止用户重复获取验证码。在倒计时过程中,按钮会显示剩余时间,倒计时结束后,按钮会重新变为可用状态。同时,为了防止组件销毁时倒计时仍然在进行,我们需要在组件销毁前清除计时器。 以上示例代码基于 Vue.js 和 uni-app 框架,您可以根据您的实际需求进行修改和优化。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值