当前位置:首页 > VUE

vue实现图片验证

2026-02-19 06:14:18VUE

Vue 实现图片验证码功能

使用 Canvas 生成验证码图片

在 Vue 项目中可以通过 Canvas 动态生成验证码图片。创建一个验证码组件,在 mounted 钩子中绘制验证码。

vue实现图片验证

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

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

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

      // 绘制背景
      ctx.fillStyle = '#f5f5f5'
      ctx.fillRect(0, 0, canvas.width, canvas.height)

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

      // 绘制干扰线
      for(let i = 0; i < 5; i++) {
        ctx.strokeStyle = this.getRandomColor()
        ctx.beginPath()
        ctx.moveTo(
          Math.random() * canvas.width,
          Math.random() * canvas.height
        )
        ctx.lineTo(
          Math.random() * canvas.width,
          Math.random() * canvas.height
        )
        ctx.stroke()
      }
    },
    getRandomColor() {
      const r = Math.floor(Math.random() * 256)
      const g = Math.floor(Math.random() * 256)
      const b = Math.floor(Math.random() * 256)
      return `rgb(${r},${g},${b})`
    },
    refreshCaptcha() {
      this.drawCaptcha()
    },
    validateCaptcha() {
      return this.inputCode.toUpperCase() === this.code
    }
  }
}
</script>

使用第三方验证码服务

可以集成第三方验证码服务如极验、腾讯云验证码等。这些服务通常提供 SDK 和 API 接口。

vue实现图片验证

// 以腾讯云验证码为例
import TCaptcha from 'tcaptcha-js'

export default {
  methods: {
    initCaptcha() {
      new TCaptcha({
        appId: '你的AppID',
        callback: (res) => {
          if(res.ret === 0) {
            // 验证通过,res.ticket可用于服务端验证
            this.$emit('verified', res.ticket)
          }
        }
      }).show()
    }
  }
}

服务端验证实现

前端生成的验证码需要与后端配合验证。将用户输入的验证码与 session 中存储的验证码比对。

// 前端发送验证请求
axios.post('/api/verifyCaptcha', {
  captcha: this.inputCode
}).then(response => {
  if(response.data.success) {
    // 验证通过
  } else {
    // 验证失败
    this.refreshCaptcha()
  }
})

验证码组件优化

可以添加以下优化功能:

  • 验证码过期时间设置
  • 点击验证码图片刷新
  • 输入错误时清空输入框
  • 响应式设计适应不同设备

验证码实现需要考虑安全性和用户体验的平衡,防止自动化工具破解的同时也要保证用户友好性。

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

相关文章

vue实现双折线图

vue实现双折线图

实现双折线图的步骤 安装必要的依赖库(如 ECharts 或 Chart.js),这里以 ECharts 为例: npm install echarts --save 在 Vue 组件中引入 EC…

vue交互实现

vue交互实现

Vue 交互实现方法 Vue.js 提供了多种方式实现用户交互,包括事件处理、表单绑定、动态渲染等。以下是常见的交互实现方法: 事件处理 通过 v-on 或 @ 指令绑定事件,触发方法或直接执行表…

利用vue 实现

利用vue 实现

以下是利用 Vue 实现常见功能的几种方法,分为不同场景和技术要点: 数据绑定与响应式更新 Vue 的核心特性是数据绑定,通过 v-model 实现双向绑定: <template>…

vue实现尺子

vue实现尺子

Vue 实现尺子组件 实现一个尺子组件可以通过 Canvas 或纯 CSS 完成。以下是两种常见实现方式: 使用 Canvas 绘制尺子 通过 Canvas 动态绘制刻度线和数字,适合需要高精度…

vue实现导航

vue实现导航

使用 Vue Router 实现导航 Vue Router 是 Vue.js 官方提供的路由管理器,用于构建单页应用(SPA)的导航系统。以下是实现导航的基本步骤。 安装 Vue Router n…

vue实现伸缩

vue实现伸缩

Vue实现伸缩功能的方法 使用CSS过渡和Vue的v-if或v-show 通过CSS的transition属性结合Vue的条件渲染指令(v-if或v-show)可以实现元素的伸缩效果。定义一个CSS类…