效果图:

html代码
输入错误三次显示该验证码,isSidentify 为输错次数

<div class="inputYzm vfCode" v-if="isSidentify >= 3"><input placeholder="请输入验证码" v-model="sidentifyMode" /><div @click="refreshCode()" class="vfCodeComponents" title="点击切换验证码"><v-sidentify :identifyCode="identifyCode"></v-sidentify></div>
</div>

定义和方法

data(){return {isSidentify: 0,sidentifyMode: '',identifyCode: '',identifyCodes: ['0','1','2','3'...'a','b','c'...'z']//验证码出现的数字和字母}
}
methods:{//登录submit() {if (this.isSidentify >= 3) {if (!this.sidentifyMode) {this.$message({type: 'warning',duration: '2000',message: '验证码不能为空!'})return}if (this.sidentifyMode.toLowerCase() != this.identifyCode.toLowerCase()) {this.$message({type: 'warning',duration: '2000',message: '验证码错误!'})this.refreshCode()return}}},// 生成随机数randomNum(min, max) {max = max + 1return Math.floor(Math.random() * (max - min) + min)},// 更新验证码refreshCode() {this.identifyCode = ''this.makeCode(this.identifyCodes, 4)// console.log('当前验证码:', this.identifyCode)},// 随机生成验证码字符串makeCode(data, len) {// console.log('data, len:', data, len)for (let i = 0; i < len; i++) {this.identifyCode += this.identifyCodes[this.randomNum(0, this.identifyCodes.length - 1)]}},
}

样式

.vfCode {background: #fff;width: 180px;height: 50px;border-radius: 25px;border: solid 1px #13c2c0;margin-top: 38px;&.inputYzm {background: white url('../img/login-yzm.png') no-repeat 15px center;background-size: 35px 35px;}input {background: none;border: none;outline: none;width: 130px;height: 100%;margin-left: 60px;font-size: 16px;border-radius: 0px 35px 35px 0px;box-shadow: inset 0 0 0 1000px #fff !important;}.vfCodeComponents {position: relative;bottom: 43px;left: 219px;cursor: pointer;}
}

验证码组件 identifyCode ,核心代码

<template><div class="s-canvas"><canvas id="s-canvas" :width="contentWidth" :height="contentHeight"></canvas></div>
</template>
<script>
export default {name: 'SIdentify',props: {identifyCode: {type: String,default: '1234'},fontSizeMin: {type: Number,default: 18},fontSizeMax: {type: Number,default: 40},backgroundColorMin: {type: Number,default: 180},backgroundColorMax: {type: Number,default: 240},colorMin: {type: Number,default: 50},colorMax: {type: Number,default: 160},lineColorMin: {type: Number,default: 40},lineColorMax: {type: Number,default: 180},dotColorMin: {type: Number,default: 0},dotColorMax: {type: Number,default: 255},contentWidth: {type: Number,default: 111},contentHeight: {type: Number,default: 38}},methods: {// 生成一个随机数randomNum(min, max) {return Math.floor(Math.random() * (max - min) + min)},// 生成一个随机的颜色randomColor(min, max) {let r = this.randomNum(min, max)let g = this.randomNum(min, max)let b = this.randomNum(min, max)return 'rgb(' + r + ',' + g + ',' + b + ')'},drawPic() {let canvas = document.getElementById('s-canvas')let ctx = canvas.getContext('2d')ctx.textBaseline = 'bottom'// 绘制背景ctx.fillStyle = this.randomColor(this.backgroundColorMin, this.backgroundColorMax)ctx.fillRect(0, 0, this.contentWidth, this.contentHeight)// 绘制文字for (let i = 0; i < this.identifyCode.length; i++) {this.drawText(ctx, this.identifyCode[i], i)}// this.drawLine(ctx) // 绘制干扰线// this.drawDot(ctx) // 绘制干扰点},// 绘制文本drawText(ctx, txt, i) {ctx.fillStyle = this.randomColor(this.colorMin, this.colorMax)ctx.font = this.randomNum(this.fontSizeMin, this.fontSizeMax) + 'px SimHei'let x = (i + 1) * (this.contentWidth / (this.identifyCode.length + 1))let y = this.randomNum(this.fontSizeMax, this.contentHeight - 5)var deg = this.randomNum(-30, 30) // 字符旋转角度(不超过45度比较好)// 修改坐标原点和旋转角度ctx.translate(x, y)ctx.rotate((deg * Math.PI) / 180)ctx.fillText(txt, 0, 0)// 恢复坐标原点和旋转角度ctx.rotate((-deg * Math.PI) / 180)ctx.translate(-x, -y)},drawLine(ctx) {// 绘制干扰线for (let i = 0; i < 8; i++) {ctx.strokeStyle = this.randomColor(this.lineColorMin, this.lineColorMax)ctx.beginPath()ctx.moveTo(this.randomNum(0, this.contentWidth), this.randomNum(0, this.contentHeight))ctx.lineTo(this.randomNum(0, this.contentWidth), this.randomNum(0, this.contentHeight))ctx.stroke()}},drawDot(ctx) {// 绘制干扰点for (let i = 0; i < 100; i++) {ctx.fillStyle = this.randomColor(0, 255)ctx.beginPath()ctx.arc(this.randomNum(0, this.contentWidth), this.randomNum(0, this.contentHeight), 1, 0, 2 * Math.PI)ctx.fill()}}},watch: {identifyCode() {this.drawPic()}},mounted() {this.drawPic()}
}
</script>

感谢大佬分享,点击 这里

vue实现登录验证码相关推荐

  1. vue实现登录时的图片验证码(纯前端)

    提示:这里只将我成功运行出来的案例放在这里供大家参考 一.图片验证码 经过我一天的查询,<vue实现登录时的图片验证码>,这篇博客效果比较好,代码基本没有什么问题的.效果如下,点击图片即可 ...

  2. 【vue】纯前端登录验证码实现记录

    [写在前面的话] 应项目安全需求,登录验证要新增验证码,下意识觉得是前端的工作,后来才发现前端验证了个寂寞,真正安全的验证码验证工作应该交给后台来做,此是后话,先记录一下纯前端实现的验证码模块. [实 ...

  3. 前端Vue实现手机号验证码登录(60s禁用倒计时)

    这篇文章主要介绍了Vue实现手机号验证码登录(60s禁用倒计时),帮助大家更好的理解和使用vue,具有很好的参考价值,感兴趣的朋友可以了解下 最近做了个vue项目,前端通过手机号验证码的方式登录,获取 ...

  4. 【VUE】vue实现登录滑动拼图验证的两种方法,纯前端组件验证以及前后端同时验证

    vue实现登录滑动拼图验证的两种方法: 第一种是纯前端组件验证,只能区分是人为操作还是机器操作. 第二种是前后端同时验证,这种方法加上后端校验相对会更安全一些.(注:在最底部加上了同时兼容移动端的方法 ...

  5. springboot+vue实现手机验证码功能

    springboot+vue实现手机验证码功能 榛子云短信平台用户中心注册登录(有免费的一条消息,剩下的需要买)(阿里云个人得备案) 在springboot中加入依赖,用到了redis,阿里的fast ...

  6. nodejs+vue实现登录界面功能(一)

    项目描述:一开始进入登录界面,只有登录成功才可以跳转到主页面,已注册但是忘记密码的进入忘记密码页面,找回密码后进入登录界面. 技术选型:nodejs+vue+stylus 界面效果: 切换登录方式 手 ...

  7. Vue+element登录页面实现拼图验证

    Vue+element登录页面实现拼图验证 需求分析 一.导入 二.验证的意义 三.常规验证手段 详细设计 一.使用组件 二.轮子已找好,开始拼凑 效果展示 结尾 需求分析 一.导入 项目开发过程中, ...

  8. 登录验证码实现(Captcha)

    登录验证码 登录验证是一般系统都会有的功能,验证的方式也多种多样,比如输入式验证码,拖动式验证条,拖动式验证拼图等等. 我们这里先实现常规的输入验证码的方式,右边显示验证码图片,点击可刷新,左边输入验 ...

  9. 【Vue.js】vue用户登录功能

    之前用vue实现一个网站的登录功能,这里做一个记录和总结,以下是需要实现的登录业务描述: 1.输入用户名和密码,点击登录按钮,若两者匹配,即可进入首页,首页展示登录用户的信息: 2.当用户登录后,无法 ...

  10. Python - WebDriver 识别登录验证码

    Python - WebDriver 识别登录验证码 没什么可说的直接上代码! #-*-coding:utf-8-*- # Time:2017/9/29 7:16 # Author:YangYangJ ...

最新文章

  1. 《响应式Web设计:HTML5和CSS3实践指南》——2.9节基于位置伪类的交替行样式
  2. vue打包完index.html空白,解决Vue项目打包后打开index.html页面显示空白以及图片路径错误的问题-20210315083204.pdf-原创力文档...
  3. Windows系统编程之进程间通信
  4. GraphQL的query只返回所请求的字段的实现原理
  5. ubuntu 如何转换 ppk ,连接 amazon ec2
  6. 2025.wireshark工具使用
  7. 测试丢包_如何使用ping和tracert命令检测丢包
  8. Win7-64bit下MapX的安装和使用
  9. PS中新建文件的一些常用预设信息
  10. 58条模拟、数字电路基础知识总结
  11. 第45章 DCMI—OV2640摄像头—零死角玩转STM32-F429系列
  12. 按键精灵学习如何偷菜示例基本代码
  13. mongodb分片集群数据库安全认证
  14. 【数据】社区发现数据集
  15. 2022 ICPC Gran Premio de Mexico Repechaje 题解
  16. powershell导入脚本失败,禁止运行脚本,无法远程连接服务器
  17. 计算机二级字符串,计算机二级辅导:VC字符串转换
  18. 侯震老师--沪师经纪
  19. 寻找Archie服务器中的文件,Archie是什么
  20. 处理VFS对象及标准函数---VFS对象

热门文章

  1. 软考——中级软件设计师备考建议
  2. JavaScript跨域请求
  3. cdr多页面排版_CDR文字排版实战图文教程,CorelDRAW文字排版有哪些技巧?
  4. Autojs实现图片转字符串(简易ocr预备步骤)
  5. 做电脑技术员几年的心得
  6. 陶教授,我记不住定理的证明该怎么办?(我看到陶哲轩在博客上与学生一则有意思的互动,就翻译过来了)...
  7. linux 消息队列API
  8. 使用Python下载本地的m3u8文件
  9. stm8用什么软件编程?stm8开发环境搭建手把手教程!
  10. php用哪个ui框架好,常用的前端UI框架有哪些