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

相关文章

CSS 样式表的四种应用方式及css注释的应用小结

《CSS样式表的四种应用方式及css注释的应用小结》:本文主要介绍了CSS样式表的四种应用方式及css注释的应用小结,本文通过实例代码给大家介绍的非常详细,详细内容请阅读本文,希望能对你有所帮助... 一、外部 css(推荐方式)定义:将 CSS 代码保存为独立的 .css 文件,通过 <link> 标签

SpringBoot后端实现小程序微信登录功能实现

《SpringBoot后端实现小程序微信登录功能实现》微信小程序登录是开发者通过微信提供的身份验证机制,获取用户唯一标识(openid)和会话密钥(session_key)的过程,这篇文章给大家介绍S... 目录SpringBoot实现微信小程序登录简介SpringBoot后端实现微信登录SpringBoo

使用Vue-ECharts实现数据可视化图表功能

《使用Vue-ECharts实现数据可视化图表功能》在前端开发中,经常会遇到需要展示数据可视化的需求,比如柱状图、折线图、饼图等,这类需求不仅要求我们准确地将数据呈现出来,还需要兼顾美观与交互体验,所... 目录前言为什么选择 vue-ECharts?1. 基于 ECharts,功能强大2. 更符合 Vue

Vue中插槽slot的使用示例详解

《Vue中插槽slot的使用示例详解》:本文主要介绍Vue中插槽slot的使用示例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录一、插槽是什么二、插槽分类2.1 匿名插槽2.2 具名插槽2.3 作用域插槽三、插槽的基本使用3.1 匿名插槽

springboot+vue项目怎么解决跨域问题详解

《springboot+vue项目怎么解决跨域问题详解》:本文主要介绍springboot+vue项目怎么解决跨域问题的相关资料,包括前端代理、后端全局配置CORS、注解配置和Nginx反向代理,... 目录1. 前端代理(开发环境推荐)2. 后端全局配置 CORS(生产环境推荐)3. 后端注解配置(按接口

Vue 2 项目中配置 Tailwind CSS 和 Font Awesome 的最佳实践举例

《Vue2项目中配置TailwindCSS和FontAwesome的最佳实践举例》:本文主要介绍Vue2项目中配置TailwindCSS和FontAwesome的最... 目录vue 2 项目中配置 Tailwind css 和 Font Awesome 的最佳实践一、Tailwind CSS 配置1. 安

Java实现图片淡入淡出效果

《Java实现图片淡入淡出效果》在现代图形用户界面和游戏开发中,**图片淡入淡出(FadeIn/Out)**是一种常见且实用的视觉过渡效果,它可以用于启动画面、场景切换、轮播图、提示框弹出等场景,通过... 目录1. 项目背景详细介绍2. 项目需求详细介绍2.1 功能需求2.2 非功能需求3. 相关技术详细

CSS3 布局样式及其应用举例

《CSS3布局样式及其应用举例》CSS3的布局特性为前端开发者提供了无限可能,无论是Flexbox的一维布局还是Grid的二维布局,它们都能够帮助开发者以更清晰、简洁的方式实现复杂的网页布局,本文给... 目录深入探讨 css3 布局样式及其应用引言一、CSS布局的历史与发展1.1 早期布局的局限性1.2

使用animation.css库快速实现CSS3旋转动画效果

《使用animation.css库快速实现CSS3旋转动画效果》随着Web技术的不断发展,动画效果已经成为了网页设计中不可或缺的一部分,本文将深入探讨animation.css的工作原理,如何使用以及... 目录1. css3动画技术简介2. animation.css库介绍2.1 animation.cs

CSS引入方式和选择符的讲解和运用小结

《CSS引入方式和选择符的讲解和运用小结》CSS即层叠样式表,是一种用于描述网页文档(如HTML或XML)外观和格式的样式表语言,它主要用于将网页内容的呈现(外观)和结构(内容)分离,从而实现... 目录一、前言二、css 是什么三、CSS 引入方式1、行内样式2、内部样式表3、链入外部样式表四、CSS 选