Vue实现随机验证码功能

 

这篇文章主要为大家详细介绍了Vue实现随机验证码功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了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)]
}
},
}

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

来源:脚本之家

链接:https://www.jb51.net/article/203106.htm

免责申明:
1. 本站所有下载资源均不包含技术支持和安装服务!需要讨论请进群!
2. 分享目的仅供大家学习和交流,请不要用于商业用途!
3. 如果你也有好源码或者教程,可以到审核区发布,分享有KR奖励和额外收入!
4. 如有链接无法下载、失效或广告,请联系管理员处理!
5. 本站无法保证资源或破解时效性,如某些授权码过期等问题,恕不在修复范围内。
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!故不接受任何形式的退款,如确认资源确有问题的,会补给相应KR以供再次购买。
7. 53Kr源码暂未发现后门代码,但无法保证100%安全,推荐检测方法:上传到 https://www.virustotal.com/在线查看是否有恶意代码以及其他有后门嫌疑的代码。
8. 在本站下载的源码我还是不建议正式使用,有特别喜欢的可以去程序官方购买。
53kr资源站仅提供学习的平台,所有资料均来自于网络,版权归原创者所有!本站不提供任何保证,并不承担任何法律责任,如果对您的版权或者利益造成损害,请提供相应的资质证明,我们将于3个工作日内予以删除。
53kr资源分享 » Vue实现随机验证码功能

发表回复