当前位置:首页 > 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 中。当用户提交表单时,将用户输入的验证码与服务器存储的值进行比对。

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

使用第三方库

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

安装 vue-captcha:

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实现按卡片轮播

实现卡片轮播的基本思路 在Vue中实现卡片轮播可以通过结合v-for指令和动态样式绑定完成。核心是维护一个当前显示卡片的索引,通过CSS过渡效果实现平滑切换。 基础实现步骤 模板部分 使用v-for…

vue优秀实现

vue优秀实现

Vue.js 优秀实现方法 响应式数据管理 使用 Vue 的 ref 和 reactive 处理基础类型和对象类型数据,结合 computed 计算属性优化渲染性能。对于复杂状态,推荐 Pinia 替…

vue实现树

vue实现树

Vue 实现树形结构的方法 使用递归组件实现树形结构 递归组件是Vue中实现树形结构的常见方式。通过组件调用自身,可以轻松构建多层嵌套的树形数据。 <template> <ul…

vue实现视频

vue实现视频

Vue 实现视频播放的方法 在 Vue 项目中实现视频播放可以通过多种方式完成,以下是几种常见的方法: 使用 HTML5 的 <video> 标签 在 Vue 组件中直接使用 HTML…

vue实现画廊

vue实现画廊

Vue实现画廊的基本方法 使用Vue.js实现画廊功能可以通过多种方式完成,以下是一种常见的实现方案: 安装必要依赖 npm install vue-gallery --save 引入组件并注册…

vue实现slidedown

vue实现slidedown

Vue 实现 SlideDown 动画效果 在 Vue 中实现 SlideDown 效果可以通过 CSS 过渡或动画结合 Vue 的过渡系统完成。以下是几种常见方法: 使用 Vue Transiti…