当前位置:首页 > VUE

vue 验证码实现

2026-02-23 10:15:31VUE

验证码组件实现

使用Vue实现验证码功能可以通过自定义组件完成。以下是一个基于Canvas绘制的图形验证码实现方案:

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

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

      // 生成随机验证码文本
      this.captchaText = 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'

      // 添加干扰线
      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()
      }

      // 添加干扰点
      for(let i = 0; i < 30; i++) {
        ctx.fillStyle = this.getRandomColor()
        ctx.beginPath()
        ctx.arc(
          Math.random() * canvas.width,
          Math.random() * canvas.height,
          1,
          0,
          2 * Math.PI
        )
        ctx.fill()
      }

      // 绘制文字(最后绘制确保不被覆盖)
      ctx.fillText(this.captchaText, canvas.width/2, canvas.height/2)
    },
    getRandomColor() {
      const letters = '0123456789ABCDEF'
      let color = '#'
      for (let i = 0; i < 6; i++) {
        color += letters[Math.floor(Math.random() * 16)]
      }
      return color
    },
    refreshCaptcha() {
      this.generateCaptcha()
    },
    validateCaptcha() {
      return this.inputCode.toUpperCase() === this.captchaText
    }
  }
}
</script>

<style>
.captcha-container {
  display: flex;
  align-items: center;
  gap: 10px;
}
canvas {
  border: 1px solid #ddd;
  cursor: pointer;
}
</style>

短信验证码实现

对于短信验证码功能,通常需要结合后端服务。以下是前端实现逻辑:

vue 验证码实现

<template>
  <div class="sms-container">
    <input v-model="phone" placeholder="手机号码"/>
    <input v-model="smsCode" placeholder="验证码"/>
    <button 
      :disabled="countdown > 0"
      @click="sendSmsCode"
    >
      {{ countdown > 0 ? `${countdown}s后重试` : '获取验证码' }}
    </button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      phone: '',
      smsCode: '',
      countdown: 0,
      timer: null
    }
  },
  methods: {
    async sendSmsCode() {
      if (!this.phone) {
        alert('请输入手机号码')
        return
      }

      try {
        // 调用API发送验证码
        const response = await axios.post('/api/send-sms', {
          phone: this.phone
        })

        if (response.data.success) {
          this.startCountdown()
        } else {
          alert(response.data.message)
        }
      } catch (error) {
        alert('发送失败,请稍后重试')
      }
    },
    startCountdown() {
      this.countdown = 60
      this.timer = setInterval(() => {
        this.countdown--
        if (this.countdown <= 0) {
          clearInterval(this.timer)
        }
      }, 1000)
    },
    validateSmsCode() {
      return axios.post('/api/verify-sms', {
        phone: this.phone,
        code: this.smsCode
      })
    }
  },
  beforeDestroy() {
    clearInterval(this.timer)
  }
}
</script>

验证码安全增强

为提高验证码安全性,可考虑以下措施:

vue 验证码实现

  • 后端生成验证码并存储于session或redis,前端仅显示
  • 增加IP限制,防止频繁请求
  • 验证码有效期设置(通常5分钟)
  • 图形验证码加入扭曲、旋转等效果防止OCR识别
  • 短信验证码加入频率限制(如1条/分钟,5条/天)

验证码自动填充处理

现代浏览器支持验证码自动填充,可通过以下方式优化用户体验:

<input 
  v-model="captcha"
  name="one-time-code"
  autocomplete="one-time-code"
  inputmode="numeric"
>

无障碍访问

确保验证码组件可被屏幕阅读器识别:

<canvas 
  ref="canvas" 
  aria-label="验证码" 
  role="img"
  @click="refreshCaptcha"
></canvas>

以上实现方案可根据实际需求进行调整,图形验证码适合防机器人,短信验证码适合用户身份验证场景。

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

相关文章

vue评分实现

vue评分实现

Vue 评分组件实现方法 使用第三方库(如 Element UI) 安装 Element UI: npm install element-ui 引入并注册组件: import Vue from '…

vue实现jqueryui

vue实现jqueryui

Vue 实现 jQuery UI 功能 在 Vue 项目中实现类似 jQuery UI 的功能,可以通过原生 Vue 组件或第三方库来实现。以下是几种常见 jQuery UI 功能的 Vue 替代方案…

vue实现picker

vue实现picker

Vue 实现 Picker 组件的方法 在 Vue 中实现 Picker 组件可以通过多种方式完成,以下是几种常见的实现方法: 使用原生 HTML 和 CSS 实现 通过 Vue 的模板和样式绑定,…

vue 实现搜索

vue 实现搜索

实现 Vue 搜索功能 在 Vue 中实现搜索功能通常涉及以下几个关键步骤: 数据绑定与输入监听 使用 v-model 双向绑定搜索输入框的值,监听用户输入: <template>…

vue实现gps

vue实现gps

Vue 中实现 GPS 定位功能 在 Vue 中实现 GPS 定位功能通常依赖于浏览器的 Geolocation API 或第三方地图服务(如高德、百度地图等)。以下是两种常见的实现方式: 使用浏览…

vue实现pwa

vue实现pwa

Vue 实现 PWA 的步骤 Vue 结合 PWA(Progressive Web App)可以创建离线可用的 Web 应用。以下是实现方法: 安装 Vue CLI 并创建项目 确保已安装 Vue…