当前位置:首页 > VUE

vue实现图片验证

2026-01-18 13:50:34VUE

Vue 实现图片验证码

使用 Canvas 生成验证码图片

在 Vue 中可以通过 Canvas 动态生成验证码图片。创建一个组件,利用 Canvas 绘制随机字符和干扰线。

<template>
  <div>
    <canvas ref="canvas" width="120" height="40" @click="refreshCode"></canvas>
  </div>
</template>

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

      // 生成随机验证码
      this.code = this.generateRandomCode(4)

      // 绘制背景
      ctx.fillStyle = this.getRandomColor(180, 240)
      ctx.fillRect(0, 0, canvas.width, canvas.height)

      // 绘制验证码
      for (let i = 0; i < this.code.length; i++) {
        ctx.font = '20px Arial'
        ctx.fillStyle = this.getRandomColor(50, 160)
        ctx.fillText(this.code[i], 20 + i * 20, 25)
      }

      // 绘制干扰线
      for (let i = 0; i < 5; i++) {
        ctx.strokeStyle = this.getRandomColor(40, 180)
        ctx.beginPath()
        ctx.moveTo(Math.random() * canvas.width, Math.random() * canvas.height)
        ctx.lineTo(Math.random() * canvas.width, Math.random() * canvas.height)
        ctx.stroke()
      }
    },
    generateRandomCode(length) {
      const chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'
      let result = ''
      for (let i = 0; i < length; i++) {
        result += chars.charAt(Math.floor(Math.random() * chars.length))
      }
      return result
    },
    getRandomColor(min, max) {
      const r = min + Math.floor(Math.random() * (max - min))
      const g = min + Math.floor(Math.random() * (max - min))
      const b = min + Math.floor(Math.random() * (max - min))
      return `rgb(${r}, ${g}, ${b})`
    },
    refreshCode() {
      this.drawCode()
    }
  }
}
</script>

服务器端验证

前端生成验证码后,需要将验证码值发送到服务器进行存储,通常存储在 session 中。当用户提交表单时,将用户输入的验证码与服务器存储的值进行比对。

vue实现图片验证

// 在提交表单时验证
methods: {
  submitForm() {
    if (this.userInputCode !== this.code) {
      alert('验证码错误')
      this.refreshCode()
      return
    }
    // 继续表单提交逻辑
  }
}

使用第三方库

可以使用现成的 Vue 验证码组件,如 vue-captcha 或 vue-img-verify,简化开发流程。

安装 vue-captcha:

vue实现图片验证

npm install vue-captcha --save

使用示例:

<template>
  <vue-captcha ref="captcha" @verify="onVerify"></vue-captcha>
</template>

<script>
import VueCaptcha from 'vue-captcha'

export default {
  components: { VueCaptcha },
  methods: {
    onVerify(response) {
      if (response) {
        // 验证通过
      }
    }
  }
}
</script>

安全注意事项

验证码应该有一定时效性,通常在服务器端设置过期时间。避免使用纯数字验证码,增加破解难度。可以考虑加入简单的算术验证码提高安全性。

前端生成的验证码需要配合服务器端验证,不能仅依赖前端验证。验证码图片应该定期刷新,防止暴力破解。

标签: 图片vue
分享给朋友:

相关文章

vue手动实现弹窗

vue手动实现弹窗

实现弹窗组件的基本结构 在Vue中手动实现弹窗组件,需要创建一个独立的弹窗组件,并通过props控制其显示与隐藏。弹窗组件通常包含遮罩层、内容区域和关闭按钮。 <template>…

vue页面分离的实现

vue页面分离的实现

Vue页面分离的实现方法 将Vue页面分离为多个组件或模块,有助于提升代码可维护性和复用性。以下是几种常见的实现方式: 组件化拆分 通过将页面拆分为多个子组件,每个组件负责特定功能或UI部分。使用…

vue怎么实现选中删除

vue怎么实现选中删除

实现选中删除功能的方法 在Vue中实现选中删除功能通常涉及以下几个关键步骤: 数据绑定与选中状态管理 使用v-model或v-for指令绑定列表数据,结合复选框或单选按钮实现选中状态管理。例如:…

vue实现绑卡的原理

vue实现绑卡的原理

Vue 实现绑卡的原理 Vue 实现绑卡功能通常涉及前端表单处理、数据绑定、验证以及和后端 API 的交互。以下是实现绑卡功能的核心原理和步骤: 数据绑定与表单处理 Vue 通过 v-model 指…

vue前端实现下载进度

vue前端实现下载进度

实现下载进度条的基本思路 在Vue中实现下载进度条,通常需要结合XMLHttpRequest或Fetch API来监听下载进度事件。通过计算已下载数据与总数据的比例,动态更新进度条的显示。 使用XM…

vue实现视窗

vue实现视窗

Vue 实现视窗功能 在 Vue 中实现视窗功能通常涉及监听浏览器窗口大小变化、响应式布局或创建自定义弹窗组件。以下是几种常见实现方式: 监听浏览器窗口大小变化 使用 Vue 的 mounted 和…