当前位置:首页 > VUE

vue实现验证码

2026-01-11 20:21:46VUE

Vue 实现验证码的方法

使用 Canvas 生成图形验证码

安装依赖(如需要):

npm install vue-canvas-verify --save

创建验证码组件:

<template>
  <div class="captcha">
    <canvas ref="canvas" width="120" height="40" @click="refresh"></canvas>
    <input v-model="inputCode" placeholder="请输入验证码"/>
  </div>
</template>

<script>
export default {
  data() {
    return {
      code: '',
      inputCode: ''
    }
  },
  mounted() {
    this.drawCode()
  },
  methods: {
    drawCode() {
      const ctx = this.$refs.canvas.getContext('2d')
      ctx.clearRect(0, 0, 120, 40)

      // 生成随机验证码
      this.code = Math.random().toString(36).substr(2, 4).toUpperCase()

      // 绘制背景
      ctx.fillStyle = '#f5f5f5'
      ctx.fillRect(0, 0, 120, 40)

      // 绘制文字
      ctx.font = '24px Arial'
      ctx.fillStyle = '#333'
      ctx.textAlign = 'center'
      ctx.textBaseline = 'middle'
      ctx.fillText(this.code, 60, 20)

      // 绘制干扰线
      for(let i=0; i<5; i++) {
        ctx.strokeStyle = this.randomColor()
        ctx.beginPath()
        ctx.moveTo(Math.random()*120, Math.random()*40)
        ctx.lineTo(Math.random()*120, Math.random()*40)
        ctx.stroke()
      }
    },
    randomColor() {
      return `rgb(${Math.floor(Math.random()*256)}, ${Math.floor(Math.random()*256)}, ${Math.floor(Math.random()*256)})`
    },
    refresh() {
      this.drawCode()
      this.inputCode = ''
    },
    validate() {
      return this.inputCode.toUpperCase() === this.code
    }
  }
}
</script>

使用第三方库 vue-verify-code

安装:

npm install vue-verify-code

使用示例:

<template>
  <verify-code 
    :width="120"
    :height="40"
    @success="handleSuccess"
    @error="handleError"
    ref="verifyCode"
  />
</template>

<script>
import VerifyCode from 'vue-verify-code'

export default {
  components: { VerifyCode },
  methods: {
    handleSuccess(code) {
      console.log('验证成功:', code)
    },
    handleError() {
      console.log('验证失败')
    },
    refresh() {
      this.$refs.verifyCode.refresh()
    }
  }
}
</script>

短信/邮箱验证码实现

发送验证码按钮组件:

<template>
  <button 
    :disabled="countdown > 0"
    @click="sendCode"
  >
    {{ countdown > 0 ? `${countdown}s后重试` : '获取验证码' }}
  </button>
</template>

<script>
export default {
  data() {
    return {
      countdown: 0
    }
  },
  methods: {
    sendCode() {
      if (this.countdown > 0) return

      // 调用API发送验证码
      this.$api.sendVerifyCode().then(() => {
        this.countdown = 60
        const timer = setInterval(() => {
          this.countdown--
          if (this.countdown <= 0) {
            clearInterval(timer)
          }
        }, 1000)
      })
    }
  }
}
</script>

验证码校验逻辑

在表单提交时验证:

methods: {
  submitForm() {
    if (!this.validateCode()) {
      alert('验证码错误')
      return
    }
    // 提交表单逻辑
  },
  validateCode() {
    // 图形验证码验证
    // return this.$refs.captcha.validate()

    // 或短信验证码验证
    return this.inputCode === this.receivedCode
  }
}

安全注意事项

验证码有效期应设置合理时间(通常5分钟) 验证码应区分大小写或统一转换为大写/小写比较 后端必须进行二次验证,前端验证仅用于用户体验 限制单位时间内验证码发送次数,防止短信轰炸 验证码应包含数字和字母组合,避免使用简单模式

vue实现验证码

标签: 验证码vue
分享给朋友:

相关文章

vue 表格实现

vue 表格实现

Vue 表格实现方法 在 Vue 中实现表格功能可以通过多种方式完成,以下介绍几种常见的方法: 使用原生 HTML 表格 最简单的表格可以通过 Vue 模板中的原生 HTML 表格元素实现:…

vue实现自动翻译

vue实现自动翻译

Vue 实现自动翻译的方法 在 Vue 项目中实现自动翻译功能可以通过多种方式完成,以下是几种常见的方法: 使用第三方翻译 API 注册并获取翻译 API 的密钥(如 Google Transla…

vue实现屏幕共享

vue实现屏幕共享

Vue 实现屏幕共享的方法 使用 WebRTC 和 getDisplayMedia API 通过 navigator.mediaDevices.getDisplayMedia 捕获屏幕内容,结合 We…

vue实现条件判断

vue实现条件判断

Vue 条件判断实现方法 Vue 提供了多种方式实现条件判断,可以根据不同场景选择合适的方式。 v-if / v-else-if / v-else 这是 Vue 中最常用的条件渲染指令,根据表达式…

vue实现下拉层级

vue实现下拉层级

Vue实现下拉层级菜单 使用Vue实现下拉层级菜单可以通过递归组件或动态渲染的方式完成。以下是两种常见实现方法: 递归组件实现 创建一个递归组件处理多级菜单结构: <template…

vue按钮实现截屏

vue按钮实现截屏

Vue 按钮实现截屏的方法 在 Vue 项目中实现截屏功能可以通过多种方式完成,以下是几种常见的方法: 使用 html2canvas 库 安装 html2canvas 库: npm ins…