前端自定义验证码,校验验证码,验证码时效

2023-12-16 11:36

本文主要是介绍前端自定义验证码,校验验证码,验证码时效,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

最近做的项目,不需要后端接口,只需要前端验证,如图

初始页面

获取验证码

验证码的文件,直接复制就行

<template><div class="s-canvas"><canvasid="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: 35,},backgroundColorMin: {// 验证码图片背景色最小值type: Number,default: 200,},backgroundColorMax: {// 验证码图片背景色最大值type: Number,default: 220,},dotColorMin: {// 背景干扰点最小值type: Number,default: 60,},dotColorMax: {// 背景干扰点最大值type: Number,default: 120,},contentWidth: {// 容器宽度type: Number,default: 90,},contentHeight: {// 容器高度type: Number,default: 38,},},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");//创建一个2D对象作为上下文。let ctx = canvas.getContext("2d");ctx.textBaseline = "bottom";// 绘制背景ctx.fillStyle = "#e6ecfd";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(50, 160); // 随机生成字体颜色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(-30, 30);// 修改坐标原点和旋转角度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 < 4; i++) {ctx.strokeStyle = this.randomColor(100, 200);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 < 30; 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>

登录页面,运用验证码

<div style="position: relative;margin: 20px 0;"><el-input style="width: 100px;margin-right: 10px;" v-model="code" placeholder="验证码"> </el-input>//组件       <dentify v-if="identifyCode" :identifyCode="identifyCode" style="width: 100px;height: 4 0px;display: inline-block;position: absolute;top: 0px;"></dentify><el-button v-if="identifyCode" @click="refreshCode" style="margin-left: 100px;color: #c4c4c4;">换一换</el-button><el-button v-else  @click="refreshCode" style="color: #c4c4c4;">获取验证码</el-button>               
</div>
<el-button @click="Submit" class="buton">登录 </el-button>
import dentify from "@/components/dentify.vue";
export default {components: {dentify,},data() {return {code:'',identifyCodes: "1234567890abcdefjhijklinopqrsduvwxyz", //随机串内容,从这里随机抽几个显示验证码identifyCode: "", //验证码图片内容timer: null, //设置计时器count:'',};},methods: {
//判断验证码并提交async Submit() {console.log(this.count,'count')if(this.identifyCode ==''){this.$message({message: '获取验证码',type: 'warning'});}else if(this.count == 0){this.$message.error("验证码失效");}else if(this.code == ''){this.$message.error("验证码不能为空");}else if(this.identifyCode == this.code){await GET_LOGIN(this.form).then((res) => {console.log(res, "ress");if (res.code == 200) {this.$message({message: "登陆成功",type: "success",});} else {this.$message.error(res.msg);}});}else{this.$message.error("验证码错误");}},// 重置验证码并倒计时refreshCode() {this.identifyCode = "";this.count = ""this.makeCode(this.identifyCodes, 4);let TIME_COUNT = 60;if (!this.timer) {this.count = TIME_COUNT;this.timer = setInterval(() => {if (this.count > 0 && this.count <= TIME_COUNT) { this.count--;} else {clearInterval(this.timer);this.timer = null;}}, 1000);}},//获取验证码的值makeCode(o, l) {for (let i = 0; i < l; i++) {//通过循环获取字符串内随机几位this.identifyCode +=this.identifyCodes[this.randomNum(0, this.identifyCodes.length)];}},//随机数字:用于当角标拿字符串的值randomNum(min, max) {return Math.floor(Math.random() * (max - min) + min);},}}

这篇关于前端自定义验证码,校验验证码,验证码时效的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/500327

相关文章

SpringBoot3.4配置校验新特性的用法详解

《SpringBoot3.4配置校验新特性的用法详解》SpringBoot3.4对配置校验支持进行了全面升级,这篇文章为大家详细介绍了一下它们的具体使用,文中的示例代码讲解详细,感兴趣的小伙伴可以参考... 目录基本用法示例定义配置类配置 application.yml注入使用嵌套对象与集合元素深度校验开发

Spring Security自定义身份认证的实现方法

《SpringSecurity自定义身份认证的实现方法》:本文主要介绍SpringSecurity自定义身份认证的实现方法,下面对SpringSecurity的这三种自定义身份认证进行详细讲解,... 目录1.内存身份认证(1)创建配置类(2)验证内存身份认证2.JDBC身份认证(1)数据准备 (2)配置依

HTML5中的Microdata与历史记录管理详解

《HTML5中的Microdata与历史记录管理详解》Microdata作为HTML5新增的一个特性,它允许开发者在HTML文档中添加更多的语义信息,以便于搜索引擎和浏览器更好地理解页面内容,本文将探... 目录html5中的Mijscrodata与历史记录管理背景简介html5中的Microdata使用M

html5的响应式布局的方法示例详解

《html5的响应式布局的方法示例详解》:本文主要介绍了HTML5中使用媒体查询和Flexbox进行响应式布局的方法,简要介绍了CSSGrid布局的基础知识和如何实现自动换行的网格布局,详细内容请阅读本文,希望能对你有所帮助... 一 使用媒体查询响应式布局        使用的参数@media这是常用的

HTML5表格语法格式详解

《HTML5表格语法格式详解》在HTML语法中,表格主要通过table、tr和td3个标签构成,本文通过实例代码讲解HTML5表格语法格式,感兴趣的朋友一起看看吧... 目录一、表格1.表格语法格式2.表格属性 3.例子二、不规则表格1.跨行2.跨列3.例子一、表格在html语法中,表格主要通过< tab

Java使用ANTLR4对Lua脚本语法校验详解

《Java使用ANTLR4对Lua脚本语法校验详解》ANTLR是一个强大的解析器生成器,用于读取、处理、执行或翻译结构化文本或二进制文件,下面就跟随小编一起看看Java如何使用ANTLR4对Lua脚本... 目录什么是ANTLR?第一个例子ANTLR4 的工作流程Lua脚本语法校验准备一个Lua Gramm

Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案

《Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案》:本文主要介绍Vue3组件中getCurrentInstance()获取App实例,但是返回nu... 目录vue3组件中getCurrentInstajavascriptnce()获取App实例,但是返回n

JS+HTML实现在线图片水印添加工具

《JS+HTML实现在线图片水印添加工具》在社交媒体和内容创作日益频繁的今天,如何保护原创内容、展示品牌身份成了一个不得不面对的问题,本文将实现一个完全基于HTML+CSS构建的现代化图片水印在线工具... 目录概述功能亮点使用方法技术解析延伸思考运行效果项目源码下载总结概述在社交媒体和内容创作日益频繁的

前端CSS Grid 布局示例详解

《前端CSSGrid布局示例详解》CSSGrid是一种二维布局系统,可以同时控制行和列,相比Flex(一维布局),更适合用在整体页面布局或复杂模块结构中,:本文主要介绍前端CSSGri... 目录css Grid 布局详解(通俗易懂版)一、概述二、基础概念三、创建 Grid 容器四、定义网格行和列五、设置行

SpringValidation数据校验之约束注解与分组校验方式

《SpringValidation数据校验之约束注解与分组校验方式》本文将深入探讨SpringValidation的核心功能,帮助开发者掌握约束注解的使用技巧和分组校验的高级应用,从而构建更加健壮和可... 目录引言一、Spring Validation基础架构1.1 jsR-380标准与Spring整合1