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中使用CSS3实现内容无缝滚动的示例代码
Nov 27 Vue.js
vue 使用rules对表单字段进行校验的步骤
Dec 25 Vue.js
vue使用require.context实现动态注册路由
Dec 25 Vue.js
手写Vue源码之数据劫持示例详解
Jan 04 Vue.js
Vue ​v-model相关知识总结
Jan 28 Vue.js
vue 实现click同时传入事件对象和自定义参数
Jan 29 Vue.js
vue项目实现分页效果
Mar 24 Vue.js
使用Vue3+Vant组件实现App搜索历史记录功能(示例代码)
Jun 09 Vue.js
详解gantt甘特图可拖拽、编辑(vue、react都可用 highcharts)
Nov 27 Vue.js
vue使用wavesurfer.js解决音频可视化播放问题
Apr 04 Vue.js
VUE之图片Base64编码使用ElementUI组件上传
Apr 09 Vue.js
vue组件vue-esign实现电子签名
Apr 21 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遍历数组的4种方法总结
2014/07/05 PHP
Thinkphp搭建包括JS多语言的多语言项目实现方法
2014/11/24 PHP
PHP自带方法验证邮箱是否存在
2016/02/01 PHP
php 策略模式原理与应用深入理解
2019/09/25 PHP
入门基础学习 ExtJS笔记(一)
2010/11/11 Javascript
javascript温习的一些笔记 基础常用知识小结
2011/06/22 Javascript
用jquery.sortElements实现table排序
2014/05/04 Javascript
Java遍历集合方法分析(实现原理、算法性能、适用场合)
2016/04/25 Javascript
深入理解逻辑表达式的用法 与或非的用法
2016/06/06 Javascript
js实现自动图片轮播代码
2017/03/22 Javascript
ES6模块化的import和export用法方法总结
2017/08/08 Javascript
微信小程序页面生命周期详解
2018/01/31 Javascript
浅谈FastClick 填坑及源码解析
2018/03/02 Javascript
javascript使用正则实现去掉字符串前面的所有0
2018/07/23 Javascript
vue侧边栏动态生成下级菜单的方法
2018/09/07 Javascript
vue-mugen-scroll组件实现pc端滚动刷新
2019/08/16 Javascript
layui操作列按钮个数和文字颜色的判断实例
2019/09/11 Javascript
详解React 条件渲染
2020/07/08 Javascript
react ant Design手动设置表单的值操作
2020/10/31 Javascript
Vue-router中hash模式与history模式的区别详解
2020/12/15 Vue.js
python中os操作文件及文件路径实例汇总
2015/01/15 Python
用实例分析Python中method的参数传递过程
2015/04/02 Python
Python实现图像几何变换
2015/07/06 Python
python 文件操作api(文件操作函数)
2016/08/28 Python
python3 requests中使用ip代理池随机生成ip的实例
2018/05/07 Python
Python程序慢的重要原因
2020/09/04 Python
浅析与CSS3的loading动画加载相关的transition优化
2015/05/18 HTML / CSS
台湾时尚彩瞳专门店:imeime
2019/08/16 全球购物
数据库基础的一些面试题
2012/02/25 面试题
高三毕业生自我鉴定
2013/12/20 职场文书
优秀的计算机专业求职信范文
2013/12/27 职场文书
2014年高数考试作弊检讨书
2014/12/14 职场文书
南极大冒险观后感
2015/06/05 职场文书
涨工资申请书应该怎么写?
2019/07/08 职场文书
CSS实现章节添加自增序号的方法
2021/06/23 HTML / CSS
Python实现将多张图片合成MP4视频并加入背景音乐
2022/04/28 Python