前端生成绘制一个验证码,并验证

news/2025/3/13 4:10:31/

先上效果:

这里是前端生成的验证码,如果需要请求后端把随机数改为接口就好了。

使用的是vantUI的弹窗,当然核心还是canvas。我将它封装成组件了直接上代码:

<template><div class="component"><van-dialogref="dialog"v-model="showDialog"title="请输入验证码":before-close="onBeforeClose"><canvas ref="canvas" :width="width" :height="height" /><van-fieldstyle="margin-top: 20px"v-model="decode"label="验证码:"placeholder="请输入验证码"/></van-dialog></div>
</template><script>
import { Notify } from "vant";
export default {props: {width: {type: Number,default: 200,},height: {type: Number,default: 50,},length: {// 生成长度type: Number,default: 4,},fontSize: {type: Number,default: 30,},},data() {return {showDialog: false,decode: "",encode: "",};},watch: {showDialog(newVal, oldVal) {if (newVal) {this.$nextTick(() => {this.drawCaptcha();});}},},mounted() {},methods: {async onBeforeClose(action, done) {if (action === "confirm") {// 点击确定走这里if (this.encode == this.decode.toUpperCase()) {done(); // 关闭弹窗this.decode = "";} else {Notify("验证码错误!");return done(false); // 阻止弹窗关闭}} else {done(); // 关闭弹窗}},drawCaptcha() {const canvas = this.$refs.canvas;const context = canvas.getContext("2d");// 清除画布context.clearRect(0, 0, this.width, this.height);// 计算字符之间的间距const spacing = this.width / (this.length + 1);// 随机生成指定长度的大写字母验证码const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";// 验证码this.encode = "";for (let i = 0; i < this.length; i++) {const randomLetter =alphabet[Math.floor(Math.random() * alphabet.length)];this.encode += randomLetter;// 设置字体和样式context.font = `bold ${this.fontSize}px Arial`;context.fillStyle = "#000";// 随机旋转角度const angle = Math.random() * 1 - 0.2;// 平移画布以便绘制倾斜的字符,并应用间距context.translate((i + 1) * spacing, this.height / 2);context.rotate(angle);// 在画布上绘制验证码context.fillText(randomLetter, -12, 12);// 恢复画布状态,以便绘制下一个字符context.rotate(-angle);context.translate(-((i + 1) * spacing), -(this.height / 2));}// 添加干扰元素for (let i = 0; i < (this.width * this.height) / 100; i++) {const x1 = Math.random() * this.width;const y1 = Math.random() * this.height;const lengthFactor = Math.random() * 20; // 控制线的长度范围const x2 = x1 + Math.cos(Math.random() * Math.PI * 2) * lengthFactor;const y2 = y1 + Math.sin(Math.random() * Math.PI * 2) * lengthFactor;// 生成线context.strokeStyle = this.getRandomColor();context.lineWidth = 1;context.beginPath();context.moveTo(x1, y1);context.lineTo(x2, y2);context.stroke();// 生成点const x = Math.random() * this.width;const y = Math.random() * this.height;context.fillStyle = this.getRandomColor();context.fillRect(x, y, 1, 1);}},getRandomColor() {const letters = "0123456789ABCDEF";let color = "#";for (let i = 0; i < 6; i++) {color += letters[Math.floor(Math.random() * 16)];}return color;},},
};
</script><style scoped lang="scss">
</style>

调用:

<DialogCode ref="dialogCode" />
// 调用methods: {showCode() {this.$refs.dialogCode.showDialog = true;},}


http://www.ppmy.cn/news/1389734.html

相关文章

机器学习模型—K means

文章目录 机器学习模型—K means聚类的目标k 均值原理k 均值 的实现手动实现Python 实现K 的确定 手肘法总结机器学习模型—K means K-Means 聚类是一种无监督机器学习算法,它将未标记的数据集分为不同的簇。本文旨在探讨 k 均值聚类的基本原理和工作原理以及实现。 无监督机…

一种仿物化视图技术的报表查询优化方案

报表查询&#xff0c;懂得都懂&#xff0c;具有大数据量&#xff0c;多表关连&#xff0c;计算维度多&#xff0c;单纯SQL关连查询统计慢&#xff0c;领导关注度高等特点。在本人接触的项目中&#xff0c;一般对实时性的要求可以适当滞后。针对上述特点&#xff0c;本人从已开发…

八股文三(Spring、Spring Cloud Alibaba)

Spring篇 什么是Spring Spring是个轻量级的框架&#xff0c;他有两大内核分别是IOC、AOPIOC是什么 * 依赖注入和控制反转&#xff0c;它是一种思想&#xff0c;创建对象不是直接new出来&#xff0c;而是交给IOC容器&#xff0c;由容器去创建进行管理&#xff0c;这就是控制反…

1058:求一元二次方程

【题目描述】 利用公式 求一元二次方程axbxc0的根&#xff0c;其中a不等于0。结果要求精确到小数点后5位。 【输入】 输入一行&#xff0c;包含三个浮点数a,b,c&#xff08;它们之间以一个空格分开&#xff09;&#xff0c;分别表示方程axbxc0的系数。 【输出】 输出一行&…

GB28181 —— 3、C++编写GB28181设备端,完成注册、注销、保活心跳、响应查询目录等功能(附源码)

效果 工程代码说明 源码运行环境为Vs2017+Qt5.12.4,sip部分由eXosip库完成。主要功能为模拟GB28181客户端,完成客户端向服务端注册、注销、发送保活心跳、响应服务端的"设备信息查询"及"目录查询"等功能。 源码 #include <QtWidgets/QApplication>…

【机器学习】TinyML的介绍以及在运动健康领域的应用

&#x1f9d1; 作者简介&#xff1a;阿里巴巴嵌入式技术专家&#xff0c;深耕嵌入式人工智能领域&#xff0c;具备多年的嵌入式硬件产品研发管理经验。 &#x1f4d2; 博客介绍&#xff1a;分享嵌入式开发领域的相关知识、经验、思考和感悟,欢迎关注。提供嵌入式方向的学习指导…

在centos8中部署Tomcat和Jenkins

参考链接1&#xff1a;tomcat安装和部署jenkins_jenkins和tomcat-CSDN博客 参考链接2&#xff1a;--配置开机启动tomcat文件 x​​​​​​超详细&#xff1a;Centos8安装Tomcat并配置开机自动启动_centos设置tomcat开机自启-CSDN博客文章浏览阅读4.4k次&#xff0c;点赞4次&…

OKR如何与组织的整体战略和计划相结合?

OKR&#xff08;Objectives and Key Results&#xff0c;目标与关键成果&#xff09;作为一种流行的目标管理方法&#xff0c;正逐渐成为组织实现战略目标的重要手段。本文将探讨OKR如何与组织的整体战略和计划相结合&#xff0c;从而推动组织的持续发展。 首先&#xff0c;我…