当前位置:首页 > VUE

验证码实现vue

2026-02-25 14:43:27VUE

验证码实现(Vue.js)

使用纯前端生成图形验证码

安装 vaptchavue-captcha 等专用库:

npm install vue-captcha --save

组件引入示例:

<template>
  <vue-captcha 
    :length="4" 
    @change="onCaptchaChange"
  />
</template>

<script>
import VueCaptcha from 'vue-captcha'
export default {
  components: { VueCaptcha },
  methods: {
    onCaptchaChange(code) {
      console.log('验证码:', code)
    }
  }
}
</script>

结合Canvas绘制验证码

创建独立组件Captcha.vue

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

<script>
export default {
  data() {
    return {
      code: ''
    }
  },
  mounted() {
    this.drawCode()
  },
  methods: {
    generateCode() {
      const chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'
      this.code = Array.from({length:4}, 
        () => chars[Math.floor(Math.random()*chars.length)]
      ).join('')
    },
    drawCode() {
      const ctx = this.$refs.canvas.getContext('2d')
      ctx.fillStyle = this.randomColor(180, 240)
      ctx.fillRect(0, 0, 120, 40)

      // 绘制干扰线
      for(let i=0; i<5; i++) {
        ctx.strokeStyle = this.randomColor(40, 180)
        ctx.beginPath()
        ctx.moveTo(
          Math.random()*120, 
          Math.random()*40
        )
        ctx.lineTo(
          Math.random()*120,
          Math.random()*40
        )
        ctx.stroke()
      }

      // 绘制验证码文本
      this.generateCode()
      ctx.font = '26px Arial'
      ctx.textAlign = 'center'
      for(let i=0; i<this.code.length; i++) {
        ctx.fillStyle = this.randomColor(0, 100)
        ctx.fillText(
          this.code[i], 
          30*(i+0.6), 
          28
        )
      }
    },
    randomColor(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()
      this.$emit('update:code', this.code)
    }
  }
}
</script>

服务端验证方案

前端调用示例:

async submitForm() {
  const isValid = await this.$refs.captcha.validate(inputCode)
  if(!isValid) {
    alert('验证码错误')
    return
  }
  // 继续提交表单
}

后端Node.js验证逻辑示例:

app.post('/verify', (req, res) => {
  const { inputCode, sessionCode } = req.body
  if(inputCode.toLowerCase() === sessionCode.toLowerCase()) {
    res.json({ valid: true })
  } else {
    res.json({ valid: false })
  }
})

滑动验证码集成

使用第三方服务如极验:

<template>
  <div id="geetest-captcha"></div>
</template>

<script>
export default {
  mounted() {
    initGeetest({
      gt: "YOUR_GT_KEY",
      challenge: "SERVER_GENERATED_CHALLENGE",
      offline: false
    }, (captchaObj) => {
      captchaObj.appendTo("#geetest-captcha")
      captchaObj.onSuccess(() => {
        const result = captchaObj.getValidate()
        this.$emit('verified', result)
      })
    })
  }
}
</script>

注意事项

  • 生产环境建议使用专业验证码服务(如reCAPTCHA、极验)
  • 前端生成的验证码需配合服务端会话存储验证
  • 定期更新验证码生成算法防止破解
  • 移动端需考虑触控交互体验

验证码实现vue

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

相关文章

vue实现tab换行

vue实现tab换行

Vue实现Tab切换的常见方法 使用Vue实现Tab切换功能可以通过多种方式完成,以下是几种常见的方法: 动态组件结合v-if或v-show 通过v-if或v-show控制不同Tab内容的显示…

vue怎么实现页面返回

vue怎么实现页面返回

Vue 实现页面返回的方法 在 Vue 中实现页面返回功能,可以通过以下几种方式完成,具体取决于项目使用的路由模式和技术栈。 使用 Vue Router 的编程式导航 通过 this.$router…

js实现vue

js实现vue

Vue.js 的基本实现方法 在 JavaScript 中实现 Vue.js 的功能,可以通过直接引入 Vue 库或使用现代构建工具(如 Vite 或 Webpack)。以下是几种常见的实现方式:…

vue实现iframe

vue实现iframe

Vue 中实现 iframe 的方法 在 Vue 项目中,可以通过多种方式实现 iframe 的嵌入。以下是几种常见的方法: 使用原生 HTML iframe 标签 直接在 Vue 组件的模板中使…

vue实现高亮

vue实现高亮

Vue 实现文本高亮的方法 在 Vue 中实现文本高亮可以通过多种方式完成,以下是几种常见的方法: 方法一:使用 v-html 指令 通过 v-html 指令可以动态插入 HTML 内容,将需…

vue菜单实现

vue菜单实现

Vue 菜单实现方法 使用 Element UI 的菜单组件 Element UI 提供了现成的菜单组件,适合快速实现导航菜单。安装 Element UI 后,可以直接使用 el-menu 相关组件。…