Vue - 登录图片验证码(identify)

2023-12-24 02:59

本文主要是介绍Vue - 登录图片验证码(identify),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Vue使用 identify 插件 生成图片验证码

一. 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: 26,},// 字体大小(最大值)fontSizeMax: {type: Number,default: 40,},// 背景颜色色值最小值,最小为0backgroundColorMin: {type: Number,default: 180,},// 背景颜色色值最大值,最小为255backgroundColorMax: {type: Number,default: 240,},// 字体颜色色值最小值,最小值为0colorMin: {type: Number,default: 50,},// 字体颜色色值最大值,最小为255colorMax: {type: Number,default: 160,},// 干扰线颜色色值最小值,最小为0lineColorMin: {type: Number,default: 40,},// 干扰线颜色色值最大值,最小为255lineColorMax: {type: Number,default: 180,},// 干扰点颜色色值最小值,最小为0dotColorMin: {type: Number,default: 0,},// 干扰点颜色色值最大值,最小为255dotColorMax: {type: Number,default: 255,},// 画布宽度contentWidth: {type: Number,default: 111,},// 画布高度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");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 < 0; 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 < 50; 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>
<style scoped>
.s-canvas {height: 38px;
}
canvas {margin-top: 5px;
}
</style>

identify参数说明如下:

参数说明类型默认值
identifyCode需要展示的验证码String1234
fontSizeMin字体大小,最小值Number16
fontSizeMax字体大小,最大值Number40
backgroundColorMin背景颜色色值最小值,最小为0Number180
backgroundColorMax背景颜色色值最大值,最大为255Number240
colorMin字体颜色色值最小值,最小为0Number50
colorMax字体颜色色值最大值,最大为255Number160
lineColorMin干扰线颜色色值最小值,最小为0Number40
lineColorMax干扰线颜色色值最大值,最大为255Number180
dotColorMin干扰点颜色色值最小值,最小为0Number0
dotColorMax干扰点颜色色值最大值,最大为255Number255
contentWidth画布宽度Number160
contentHeight画布高度Number40

二. 使用 identify.vue 组件生成验证码,并验证规则

<template><div class="wrap"><el-form :model="loginForm" :rules="loginFormRules" ref="FormRef" label-position="top"><el-form-item label="图片验证码" prop="pictureCode"><el-input v-model="loginForm.pictureCode" autocomplete="off" class="pictureCode"><template slot="append"><div style="border: none" @click="refreshCode"><!-- 3. 使用 identify 组件 --><picture-identify :identifyCode="loginForm.identifyCode"></picture-identify></div></template></el-input></el-form-item></el-form></div>
</template>
<script>
// 1. 引入 identify (注意文件路径,请以实际路径为准)
import PictureIdentify from '@/utils/identify.vue'
export default {// 2. 注册 identify 组件components:{PictureIdentify},data() {// 验证输入的图片验证码是否和存储的验证码相同var checkPictureCode = (rules, value, callback) => {if (value == this.loginForm.identifyCode) {return callback();} else {callback(new Error("验证码错误"));}};return {loginForm: {pictureCode: "",//输入input框的验证码identifyCode: "", //存储的图片验证码},// 验证规则loginFormRules: {pictureCode: [{ required: true, message: "请输入图片验证码", trigger: "blur" },{ validator: checkPictureCode,trigger:'blur' }],},};},mounted() {this.refreshCode()},methods: {// 获取随机图片验证码refreshCode() {var numCode = "";for (var i = 0; i < 4; i++) {numCode += Math.floor(Math.random() * 10);}this.loginForm.identifyCode = numCode;},},
};
</script><style scoped>
.el-form {width: 600px;margin: 0 auto;
}
.identifyDiv{width:100%;height: 100%;
}
.pictureCode >>> .el-input-group__append{padding: 0;
}
.pictureCode >>> .s-canvas canvas{margin-top: 0;
}
</style>

三. 效果图

在这里插入图片描述

这篇关于Vue - 登录图片验证码(identify)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

利用Python脚本实现批量将图片转换为WebP格式

《利用Python脚本实现批量将图片转换为WebP格式》Python语言的简洁语法和库支持使其成为图像处理的理想选择,本文将介绍如何利用Python实现批量将图片转换为WebP格式的脚本,WebP作为... 目录简介1. python在图像处理中的应用2. WebP格式的原理和优势2.1 WebP格式与传统

前端如何通过nginx访问本地端口

《前端如何通过nginx访问本地端口》:本文主要介绍前端如何通过nginx访问本地端口的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、nginx安装1、下载(1)下载地址(2)系统选择(3)版本选择2、安装部署(1)解压(2)配置文件修改(3)启动(4)

HTML中meta标签的常见使用案例(示例详解)

《HTML中meta标签的常见使用案例(示例详解)》HTMLmeta标签用于提供文档元数据,涵盖字符编码、SEO优化、社交媒体集成、移动设备适配、浏览器控制及安全隐私设置,优化页面显示与搜索引擎索引... 目录html中meta标签的常见使用案例一、基础功能二、搜索引擎优化(seo)三、社交媒体集成四、移动

HTML input 标签示例详解

《HTMLinput标签示例详解》input标签主要用于接收用户的输入,随type属性值的不同,变换其具体功能,本文通过实例图文并茂的形式给大家介绍HTMLinput标签,感兴趣的朋友一... 目录通用属性输入框单行文本输入框 text密码输入框 password数字输入框 number电子邮件输入编程框

HTML img标签和超链接标签详细介绍

《HTMLimg标签和超链接标签详细介绍》:本文主要介绍了HTML中img标签的使用,包括src属性(指定图片路径)、相对/绝对路径区别、alt替代文本、title提示、宽高控制及边框设置等,详细内容请阅读本文,希望能对你有所帮助... 目录img 标签src 属性alt 属性title 属性width/h

CSS3打造的现代交互式登录界面详细实现过程

《CSS3打造的现代交互式登录界面详细实现过程》本文介绍CSS3和jQuery在登录界面设计中的应用,涵盖动画、选择器、自定义字体及盒模型技术,提升界面美观与交互性,同时优化性能和可访问性,感兴趣的朋... 目录1. css3用户登录界面设计概述1.1 用户界面设计的重要性1.2 CSS3的新特性与优势1.

HTML5 中的<button>标签用法和特征

《HTML5中的<button>标签用法和特征》在HTML5中,button标签用于定义一个可点击的按钮,它是创建交互式网页的重要元素之一,本文将深入解析HTML5中的button标签,详细介绍其属... 目录引言<button> 标签的基本用法<button> 标签的属性typevaluedisabled

HTML5实现的移动端购物车自动结算功能示例代码

《HTML5实现的移动端购物车自动结算功能示例代码》本文介绍HTML5实现移动端购物车自动结算,通过WebStorage、事件监听、DOM操作等技术,确保实时更新与数据同步,优化性能及无障碍性,提升用... 目录1. 移动端购物车自动结算概述2. 数据存储与状态保存机制2.1 浏览器端的数据存储方式2.1.

基于 HTML5 Canvas 实现图片旋转与下载功能(完整代码展示)

《基于HTML5Canvas实现图片旋转与下载功能(完整代码展示)》本文将深入剖析一段基于HTML5Canvas的代码,该代码实现了图片的旋转(90度和180度)以及旋转后图片的下载... 目录一、引言二、html 结构分析三、css 样式分析四、JavaScript 功能实现一、引言在 Web 开发中,

Python如何去除图片干扰代码示例

《Python如何去除图片干扰代码示例》图片降噪是一个广泛应用于图像处理的技术,可以提高图像质量和相关应用的效果,:本文主要介绍Python如何去除图片干扰的相关资料,文中通过代码介绍的非常详细,... 目录一、噪声去除1. 高斯噪声(像素值正态分布扰动)2. 椒盐噪声(随机黑白像素点)3. 复杂噪声(如伪