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 限制数字 js限制输入实现代码
Dec 04 Javascript
JS实现随机数生成算法示例代码
Aug 08 Javascript
多种方法判断Javascript对象是否存在
Sep 22 Javascript
事件委托与阻止冒泡阻止其父元素事件触发
Sep 02 Javascript
Jquery中$.post和$.ajax的用法小结
Apr 28 Javascript
javascript事件模型介绍
May 31 Javascript
jquery ajax异步提交表单数据的方法
Oct 27 jQuery
微信小程序url传参写变量的方法
Aug 09 Javascript
使用vscode快速建立vue模板过程详解
Oct 10 Javascript
JS window对象简单操作完整示例
Jan 14 Javascript
2020淘宝618理想生活列车自动领喵币js脚本的代码
Jun 02 Javascript
VUE-ElementUI 自定义Loading图操作
Nov 11 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
地球防卫队:陪着奥特曼打小怪兽的人类力量 那些经典队服
2020/03/08 日漫
PHP CURL模拟GET及POST函数代码
2010/04/25 PHP
php 常用类汇总 推荐收藏
2010/05/13 PHP
PHP自定session保存路径及删除、注销与写入的方法
2014/11/18 PHP
PHP使用PDO 连接与连接管理操作实例分析
2020/04/21 PHP
解析arp病毒背后利用的Javascript技术附解密方法
2007/08/06 Javascript
jquery星级插件、支持页面中多次使用
2012/03/25 Javascript
获取当前点击按钮的id用this.id实现
2014/03/17 Javascript
JavaScript删除指定子元素代码实例
2015/01/13 Javascript
一个超简单的jQuery回调函数例子(分享)
2016/08/08 Javascript
jQuery实现的导航下拉菜单效果示例
2016/09/05 Javascript
初识简单却不失优雅的Vue.js
2016/09/12 Javascript
Bootstrap树形菜单插件TreeView.js使用方法详解
2016/11/01 Javascript
使用requirejs模块化开发多页面一个入口js的使用方式
2017/06/14 Javascript
微信小程序实现下拉刷新和轮播图效果
2017/11/21 Javascript
vue加载完成后的回调函数方法
2018/09/07 Javascript
微信小程序五子棋游戏的棋盘,重置,对弈实现方法【附demo源码下载】
2019/02/20 Javascript
浅谈vue websocket nodeJS 进行实时通信踩到的坑
2020/09/22 NodeJs
基于openlayers实现角度测量功能
2020/09/28 Javascript
利用Python自带PIL库扩展图片大小给图片加文字描述的方法示例
2017/08/08 Python
Python通过Pygame绘制移动的矩形实例代码
2018/01/03 Python
简单了解django处理跨域请求最佳解决方案
2020/03/25 Python
python 负数取模运算实例
2020/06/03 Python
印度在线内衣和时尚目的地:Zivame
2017/09/28 全球购物
美国网上书店:Barnes & Noble
2018/08/15 全球购物
美国优质马术服装购买网站:Breeches.com
2019/12/16 全球购物
工程地质勘察专业大学生求职信
2013/10/13 职场文书
餐厅总厨求职信
2014/03/04 职场文书
数控专业毕业生自荐信范文
2014/03/04 职场文书
优秀少先队辅导员先进事迹材料
2014/05/18 职场文书
设计专业自荐信
2014/06/19 职场文书
机关党员三严三实心得体会
2014/10/13 职场文书
铣工实训报告
2014/11/05 职场文书
人事局接收函
2015/01/30 职场文书
python-for x in range的用法(注意要点、细节)
2021/05/10 Python
SQL SERVER触发器详解
2022/02/24 SQL Server