Vue实现随机验证码功能


Posted in Vue.js onDecember 29, 2020

本文实例为大家分享了Vue实现随机验证码功能的具体代码,供大家参考,具体内容如下

步骤1:创建一个名为identify.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: 35
},
// 字体最大值
fontSizeMax: {
  type: Number,
  default: 35
},
// 背景颜色色值最小值
backgroundColorMin: {
  type: Number,
  default: 180
},
// 背景颜色色值最大值
backgroundColorMax: {
  type: Number,
  default: 240
},
// 字体颜色色值最小值
colorMin: {
  type: Number,
  default: 50
},
// 字体颜色色值最大值
colorMax: {
  type: Number,
  default: 160
},
// 干扰线颜色色值最小值
lineColorMin: {
  type: Number,
  default: 100
},
// 干扰线颜色色值最大值
lineColorMax: {
  type: Number,
  default: 200
},
// 干扰点颜色色值最小值
dotColorMin: {
  type: Number,
  default: 0
},
// 干扰点颜色色值最大值
dotColorMax: {
  type: Number,
  default: 255
},
// 画布宽度
contentWidth: {
  type: Number,
  default: 120
},
// 画布高度
contentHeight: {
  type: Number,
  default: 40
}
 },
 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>

步骤2 在子组件中进行注册和引用

<script>
  import SIdentify from "./common/sIdentify.vue";
  export default {
    components: { SIdentify },
  }
</script>

步骤3 在主页面中使用子组件

1、template中:

<template>
 <div class="code" @click="refreshCode">
  <s-identify :identifyCode="identifyCode"></s-identify>
 </div>
</template>

2、 data中:

data() {
 return {
   identifyCode: "",
   identifyCodes: "",
 }
},

3、methods中:

methods: {
  // 生成随机数
  randomNum(min, max) {
    max = max + 1
    return Math.floor(Math.random() * (max - min) + min);
  },
  // 更新验证码
  refreshCode() {
    this.identifyCode = "";
    this.makeCode(this.identifyCodes, 4);
    console.log('当前验证码==',this.identifyCode);
  },
  // 随机生成验证码字符串
  makeCode(data, len) {
    for (let i = 0; i < len; i++) {
      this.identifyCode += this.identifyCodes[this.randomNum(0, this.identifyCodes)]
    }
  },
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Vue.js 相关文章推荐
vue自定义插件封装,实现简易的elementUi的Message和MessageBox的示例
Nov 20 Vue.js
vue $router和$route的区别详解
Dec 02 Vue.js
vue实现图书管理系统
Dec 29 Vue.js
Vue中引入svg图标的两种方式
Jan 14 Vue.js
Vue中的nextTick作用和几个简单的使用场景
Jan 25 Vue.js
vue 组件基础知识总结
Jan 26 Vue.js
vue.js实现点击图标放大离开时缩小的代码
Jan 27 Vue.js
vue 项目@change多个参数传值多个事件的操作
Jan 29 Vue.js
解决vue项目本地启动时无法携带cookie的问题
Feb 06 Vue.js
Vue中避免滥用this去读取data中数据
Mar 02 Vue.js
vue组件的路由高亮问题解决方法
May 11 Vue.js
Vue router配置与使用分析讲解
Dec 24 Vue.js
vue3+typeScript穿梭框的实现示例
Dec 29 #Vue.js
Vue.extend 登录注册模态框的实现
Dec 29 #Vue.js
vue实现简易的双向数据绑定
Dec 29 #Vue.js
vue中配置scss全局变量的步骤
Dec 28 #Vue.js
为什么推荐使用JSX开发Vue3
Dec 28 #Vue.js
Vue仿百度搜索功能
Dec 28 #Vue.js
vue中watch的用法汇总
Dec 28 #Vue.js
You might like
不错的PHP学习之php4与php5之间会穿梭一点点感悟
2007/05/03 PHP
php 地区分类排序算法
2013/07/01 PHP
php实现的RSS生成类实例
2015/04/23 PHP
win7安装php框架Yii的方法
2016/01/25 PHP
Yii2设置默认控制器的两种方法
2017/05/19 PHP
DHTML 中的绝对定位
2006/11/26 Javascript
SwfUpload在IE10上不出现上传按钮的解决方法
2013/06/25 Javascript
分享经典的JavaScript开发技巧
2015/11/21 Javascript
使用jquery给新生的th绑定hover事件的实例
2017/02/10 Javascript
JS中type=&quot;button&quot;和type=&quot;submit&quot;的区别
2017/07/04 Javascript
基于jQuery对象和DOM对象和字符串之间的转化实例
2017/08/08 jQuery
微信小程序swiper组件用法实例分析【附源码下载】
2017/12/07 Javascript
vue双向数据绑定知识点总结
2018/04/18 Javascript
JQueryDOM之样式操作
2019/03/27 jQuery
详解vue中使用微信jssdk
2019/04/19 Javascript
JavaScript中var的重要性实例分析
2019/07/09 Javascript
Vue中通过Vue.extend动态创建实例的方法
2019/08/13 Javascript
ES6使用 Array.includes 处理多重条件用法实例分析
2020/03/02 Javascript
JS定时器如何实现提交成功提示功能
2020/06/12 Javascript
[48:27]EG vs Liquid 2018国际邀请赛淘汰赛BO3 第二场 8.25
2018/08/29 DOTA
利用Python的Django框架生成PDF文件的教程
2015/07/22 Python
解读Django框架中的低层次缓存API
2015/07/24 Python
Python爬虫包BeautifulSoup实例(三)
2018/06/17 Python
numpy添加新的维度:newaxis的方法
2018/08/02 Python
python+numpy实现的基本矩阵操作示例
2019/07/19 Python
Python 继承,重写,super()调用父类方法操作示例
2019/09/29 Python
Python list与NumPy array 区分详解
2019/11/06 Python
Python3爬虫中Splash的知识总结
2020/07/10 Python
Python3利用scapy局域网实现自动多线程arp扫描功能
2021/01/21 Python
html5使用Drag事件编辑器拖拽上传图片的示例代码
2017/08/22 HTML / CSS
购买200个世界上最好的内衣品牌:Bare Necessities
2017/02/11 全球购物
大学生个人总结的自我评价
2013/10/05 职场文书
入党函调证明材料
2015/06/19 职场文书
撤回我也能看到!教你用Python制作微信防撤回脚本
2021/06/11 Python
MySQL系列之三 基础篇
2021/07/02 MySQL
Java 超详细讲解十大排序算法面试无忧
2022/04/08 Java/Android