vue实现随机验证码功能的实例代码


Posted in Javascript onApril 30, 2019

先给大家展示下效果图:

vue实现随机验证码功能的实例代码

1.html代码

vue实现随机验证码功能的实例代码

<div class="form-group" style="display: flex;">
     <div>
      <span>验证码:</span>
      <input type="text" id="code" v-model="code" class="code" placeholder="请输入您的验证码" />
     </div>
     <div class="login-code" @click="refreshCode">
   <!--验证码组件-->
   <s-identify :identifyCode="identifyCode"></s-identify>
   </div>
    </div>

2.css样式

vue实现随机验证码功能的实例代码

/*验证码样式*/
.code{
 width:124px;
 height:31px;
 border:1px solid rgba(186,186,186,1);
}
.login-code{
  cursor: pointer;
}

3.js引入验证码组件,并且定义三个变量。

vue实现随机验证码功能的实例代码

import SIdentify from '../components/sidentify'

components: { SIdentify },
data () {
 return {
  identifyCodes: "1234567890",
  identifyCode: "",
  code:"",//text框输入的验证码
 }
},

4.mounted里的代码

vue实现随机验证码功能的实例代码

mounted(){
  this.identifyCode = "";
  this.makeCode(this.identifyCodes, 4);
 },

5.在created里初始化验证码

vue实现随机验证码功能的实例代码

6.methods里添加以下方法。

vue实现随机验证码功能的实例代码

需要用到的方法

//验证码
randomNum(min, max) {
 return Math.floor(Math.random() * (max - min) + min);
},
  
refreshCode() {
 this.identifyCode = "";
 this.makeCode(this.identifyCodes, 4);
},
makeCode(o, l) {
 for (let i = 0; i < l; i++) {
  this.identifyCode += this.identifyCodes[
   this.randomNum(0, this.identifyCodes.length)
  ];
 }
 console.log(this.identifyCode);
},

在提交表单的时候对验证码进行判断。

vue实现随机验证码功能的实例代码

sidentify.vue组件代码:

vue实现随机验证码功能的实例代码

<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: 25
  },
  fontSizeMax: {
   type: Number,
   default: 30
  },
  backgroundColorMin: {
   type: Number,
   default: 255
  },
  backgroundColorMax: {
   type: Number,
   default: 255
  },
  colorMin: {
   type: Number,
   default: 0
  },
  colorMax: {
   type: Number,
   default: 160
  },
  lineColorMin: {
   type: Number,
   default: 100
  },
  lineColorMax: {
   type: Number,
   default: 255
  },
  dotColorMin: {
   type: Number,
   default: 0
  },
  dotColorMax: {
   type: Number,
   default: 255
  },
  contentWidth: {
   type: Number,
   default: 112
  },
  contentHeight: {
   type: Number,
   default: 31
  }
 },
 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(-45, 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 < 5; 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 < 80; 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>
<style scoped>
.s-canvas {
 height: 38px;
}
.s-canvas canvas{
 margin-top: 1px;
 margin-left: 8px;
}
</style>

总结

以上所述是小编给大家介绍的vue实现随机验证码功能的实例代码,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

Javascript 相关文章推荐
JS图片切换的具体方法(带缩略图版)
Nov 12 Javascript
Jquery解析Json格式数据过程代码
Oct 17 Javascript
jQuery仿Flash上下翻动的中英文导航菜单实例
Mar 10 Javascript
微信小程序 wx:key详细介绍
Oct 28 Javascript
jquery心形点赞关注效果的简单实现
Nov 14 Javascript
微信小程序 两种滑动方式(横向滑动,竖向滑动)详细及实例代码
Jan 13 Javascript
关于JavaScript中的this指向问题总结篇
Jul 23 Javascript
jQuery实现可兼容IE6的淡入淡出效果告警提示功能示例
Sep 20 jQuery
JS在if中的强制类型转换方式
Jul 15 Javascript
解决使用bootstrap的dropdown部件时报错:error:Bootstrap dropdown require Popper.js问题
Aug 30 Javascript
Bootstrap的aria-label和aria-labelledby属性实例详解
Nov 02 Javascript
微信小程序个人中心的列表控件实现代码
Apr 26 Javascript
详解vue 图片上传功能
Apr 30 #Javascript
vue移动端屏幕适配详解
Apr 30 #Javascript
vue-cli3 项目优化之通过 node 自动生成组件模板 generate View、Component
Apr 30 #Javascript
微信小程序时间戳转日期的详解
Apr 30 #Javascript
使用 Vue cli 3.0 构建自定义组件库的方法
Apr 30 #Javascript
vue自动路由-单页面项目(非build时构建)
Apr 30 #Javascript
vue-router 前端路由之路由传值的方式详解
Apr 30 #Javascript
You might like
给初学PHP的5个入手程序
2006/11/23 PHP
php获取从百度搜索进入网站的关键词的详细代码
2014/01/08 PHP
yii2.0实现验证用户名与邮箱功能
2015/12/22 PHP
PHP简单实现DES加密解密的方法
2016/07/12 PHP
js 设置选中行的样式的实现代码
2010/05/24 Javascript
基于JQuery实现相同内容合并单元格的代码
2011/01/12 Javascript
jQuery学习笔记之控制页面实现代码
2012/02/27 Javascript
JS localStorage实现本地缓存的方法
2013/06/22 Javascript
javascript实现des解密加密全过程
2014/04/03 Javascript
一个JavaScript处理textarea中的字符成每一行实例
2014/09/22 Javascript
JavaScript 数组中最大最小值
2016/06/05 Javascript
Kendo Grid editing 自定义验证报错提示的解决方法
2016/11/18 Javascript
vue.js事件处理器是什么
2017/03/20 Javascript
深入理解Nodejs Global 模块
2017/06/03 NodeJs
JS实现table表格固定表头且表头随横向滚动而滚动
2017/10/26 Javascript
Angular父组件调用子组件的方法
2018/04/02 Javascript
django使用channels2.x实现实时通讯
2018/11/28 Javascript
layui表单验证select下拉框实现验证的方法
2019/09/05 Javascript
[37:23]DOTA2上海特级锦标赛主赛事日 - 3 胜者组第二轮#2Secret VS EG第二局
2016/03/04 DOTA
Python 时间处理datetime实例
2008/09/06 Python
Python编程之多态用法实例详解
2015/05/19 Python
python3.6编写的单元测试示例
2019/08/17 Python
Python hashlib模块加密过程解析
2019/11/05 Python
python文件和文件夹复制函数
2020/02/07 Python
Python识别html主要文本框过程解析
2020/02/18 Python
基于Python数据结构之递归与回溯搜索
2020/02/26 Python
Django中的session用法详解
2020/03/09 Python
python3.8动态人脸识别的实现示例
2020/09/21 Python
June Jacobs尊积帕官网:知名的spa水疗护肤品牌
2019/03/21 全球购物
中学生差生评语
2014/01/30 职场文书
甜美蛋糕店创业计划书
2014/01/30 职场文书
给老师的一封建议书
2014/03/13 职场文书
法院信息化建设方案
2014/05/21 职场文书
执法作风整顿剖析材料
2014/10/11 职场文书
python爬虫之利用selenium模块自动登录CSDN
2021/04/22 Python
详解Go语言运用广度优先搜索走迷宫
2021/06/23 Python