基于canvas的数字/数字+字符验证码   SIdentify.vue 组件
<!-- 基于canvas的数字/数字+字符验证码 -->
<!-- 调用格式
<s-identify
@func="getMsgFormSon"
:isRefreshCode="isRefreshCode"
:identifyCodes="identifyCodes" //可选传,选传写法有要求
></s-identify> -->
<template>
<div class="s-canvas">
<canvas id="s-canvas" :width="contentWidth" :height="contentHeight"></canvas>
</div>
</template>
<script>
export default {
name: "SIdentify",
props: {
isRefreshCode: {
type: Boolean,
default: false
},
identifyCodes: {
type: Array
},
identifyLen: {
type: Number,
default: 4
},
fontSizeMin: {
type: Number,
default: 16
},
fontSizeMax: {
type: Number,
default: 40
},
backgroundColorMin: {
type: Number,
default: 180
},
backgroundColorMax: {
type: Number,
default: 240
},
colorMin: {
type: Number,
default: 50
},
colorMax: {
type: Number,
default: 160
},
lineColorMin: {
type: Number,
default: 40
},
lineColorMax: {
type: Number,
default: 180
},
dotColorMin: {
type: Number,
default: 0
},
dotColorMax: {
type: Number,
default: 255
},
contentWidth: {
type: Number,
default: 112
},
contentHeight: {
type: Number,
default: 32
}
},
data() {
return {
nums: [
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"0",
"A",
"B",
"C",
"D",
"E",
"F",
"G",
"H",
"I",
"J",
"K",
"L",
"M",
"N",
"O",
"P",
"Q",
"R",
"S",
"T",
"U",
"V",
"W",
"X",
"Y",
"Z",
"a",
"b",
"c",
"d",
"e",
"f",
"g",
"h",
"i",
"j",
"k",
"l",
"m",
"n",
"o",
"p",
"q",
"r",
"s",
"t",
"u",
"v",
"w",
"x",
"y",
"z"
],
identifyText: ""
};
},
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 + ")";
},
// 绘制干扰线
drawLine(ctx) {
for (let i = 0; i < 6; 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,
* Math.PI
);
ctx.fill();
}
},
// 生成l长度的验证码
makeCode(o, l) {
let identifyCodes = o;
this.identifyText = "";
for (let i = 0; i < l; i++) {
this.identifyText +=
identifyCodes[this.randomNum(0, identifyCodes.length)];
}
},
// 验证码文本
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.identifyText.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);
},
// 验证码图形
drawPic() {
let identifyCodesArray = this.identifyCodes
? this.identifyCodes
: this.nums;
this.makeCode(identifyCodesArray, this.identifyLen);
let identifyCode = "";
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);
console.log(this.identifyText, "huizhi");
// 绘制文字
for (let i = 0; i < this.identifyLen; i++) {
this.drawText(ctx, this.identifyText[i], i);
identifyCode += this.identifyText[i];
}
// for (let i = 0; i < this.identifyArray.length; i++) {
// this.drawText(ctx, this.identifyArray[i], i);
// }
this.drawLine(ctx);
this.drawDot(ctx);
console.log(identifyCode, "要传的值");
// 向父组件传值
this.$emit("func", identifyCode);
}
},
watch: {
if (this.isRefreshCode) {
this.drawPic();
}
},
mounted() {
this.drawPic();
}
};
</script>
<style lang="scss" scoped>
.s-canvas {
height: 32px;
canvas {
margin-top: 1px;
margin-left: 8px;
}
}
</style>

父组件调用

①默认 数字+字母验证码 形式

<template>
<div>
<el-input v-model="verificationCode" placeholder="验证码" />
<div class="identify-code" @click="refreshCode">
<!--验证码组件-->
<s-identify @func="getSidentifyCode" :isRefreshCode="isRefreshCode"></s-identify>
</div> </div>
</template>
<script>
import Vue from "vue";
import SIdentify from "./common/SIdentify"; export default {
components: { SIdentify },
data() {
return {
verificationCode: "",
identifyCode: "",
isRefreshCode: false,
};
},
created() {
// this.refreshCode();
},
mounted() {},
watch: {},
methods: {
refreshCode() {
this.identifyCode = "";
this.isRefreshCode = true; //更改状态--SIdentify组件中监听该状态
},
getSidentifyCode(data) {
this.identifyCode = data;
this.isRefreshCode = false;
console.log(this.identifyCode, "接收到的code");
}
}
};
</script>
<style lang="scss" scoped>
.identify-code {
display: inline-block;
}
}
</style>

②通过传  纯数字  为数字验证码

<template>
<div>
<el-input v-model="verificationCode" placeholder="验证码" />
<div class="identify-code" @click="refreshCode">
<!--验证码组件-->
<s-identify @func="getSidentifyCode" :isRefreshCode="isRefreshCode"></s-identify>
</div> </div>
</template>
<script>
import Vue from "vue";
import SIdentify from "./common/SIdentify"; export default {
components: { SIdentify },
data() {
return {
verificationCode: "",
identifyCodes: ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"],
identifyCode: "",
isRefreshCode: false,
};
},
created() {
this.refreshCode();
},
mounted() {},
watch: {},
methods: {
refreshCode() {
this.identifyCode = "";
this.isRefreshCode = true; //更改状态--SIdentify组件中监听该状态
// 随机排序
if (this.identifyCodes.length) {
let tempCodesArray = this.identifyCodes.sort(function() {
return Math.random() - 0.5;
});
this.identifyCodes = tempCodesArray;
}
},
getSidentifyCode(data) {
this.identifyCode = data;
this.isRefreshCode = false;
console.log(this.identifyCode, "接收到的code");
}
}
};
</script>
<style lang="scss" scoped>
.identify-code {
display: inline-block;
}
}
</style>

当用方式②时, SIdentify.vue 组件 可以略去 isRefreshCode的监听判断,改为监听 identifyCodes

代码段为

SIdentify.vue

<script>
export default {
...
watch: {
identifyCodes() {
this.drawPic();
}
//,
// isRefreshCode() {
// this.drawPic();
// }
} };
</script>
<style lang="scss" scoped>
.
.
.
</style>

【vue】canvas验证码组件--数字/数字加字母的更多相关文章

  1. Vue.js 子组件的异步加载及其生命周期控制

    前端开发社区的繁荣,造就了很多优秀的基于 MVVM 设计模式的框架,而组件化开发思想也越来越深入人心.这其中不得不提到 Vue.js 这个专注于 VM 层的框架. 本文主要对 Vue.js 组件化开发 ...

  2. vue 单文件组件中样式加载

    在写单文件组件时,一般都是把标签.脚本.样式写到一起,这样写个人感觉有点不够简洁,所以就想着把样式分离出去. 采用import加载样式 在局部作用域(scoped)采用@import加载进来的样式文件 ...

  3. vue的异步组件按需加载

    当build打包后,app.js过大的时候,可以考虑用异步组件的方式. import HomeHeader from "./components/Header"; import H ...

  4. vue之vant组件下拉加载更多

    vant地址:https://youzan.github.io/vant/#/zh-CN/intro 基础用法 List 组件通过loading和finished两个变量控制加载状态,当组件滚动到底部 ...

  5. Vue路由(组件)懒加载(异步)

    传统的引入方式 import test from '@/components/test' { path: '/test', name: '测试页面', component:test }, 懒加载的方式 ...

  6. vue+element树组件 实现树懒加载

    本文连接https://www.cnblogs.com/aknife/p/11709255.html 一.页面样式 二.数据库 三.前端页面代码 <template> <el-tre ...

  7. canvas验证码 - 随机字母数字

    基于canvas制作随机生成数字英文组合验证码效果,点击或刷新会自动重组.输入验证码提交验证效果代码. <div class="verification"> <i ...

  8. Vue图片验证码-自定义组件高级版

    最近项目中要用到图片验证码,网上一查有很多,基本都是千篇一律的4位纯数字验证码.首先得感谢那位一代目兄台提供的模板,由于不能满足需求,所以对其进行了改造升级. 经改造的图片验证码能满足一下情形使用:① ...

  9. java实现登录的验证码和猜数字游戏_图形化界面

    实验任务四 1,出现设计思想 (1)先定义文本框.密码框和验证码框的组件 (2)定义面板和按钮的个数 (3)定义公有的虚构方法,通过对象实例化来调用 (4)利用Random类来实现生成0-9的随机数 ...

随机推荐

  1. 11、kubernetes之dashboard

    一.准备dashboard使用的证书 # cd /etc/kubernetes/pki/ pki]# (umask 077; openssl genrsa -out dashboard.key 204 ...

  2. 如何解决Struts2和Servlet共存问题

    我之前用Servlet写过二维码扫描登录,结果把它整合到ssh框架中,发现Servlet和Struts存在共存问题,这是因为当我们在页面在请求应用时,struts2将会截获所有请求,对于servlet ...

  3. gromacs2018使用踩坑记--grompp 为啥要用-r

    1. GMX grompp 概要 gmx grompp [ -f [<.mdp>] ] [ -c [<.gro / .g96 / ...>] ] [ -r [<.gro ...

  4. 浏览器端-W3School-JavaScript-HTML DOM:HTML DOM Attribute 对象

      ylbtech-浏览器端-W3School-JavaScript-HTML DOM:HTML DOM Attribute 对象 1.返回顶部 1. HTML DOM Attribute 对象 HT ...

  5. python - readline 模块

    转载:https://www.cnblogs.com/fireflow/p/4841413.html readline模块定义了一系列函数用来读写Python解释器中历史命令,并提供自动补全命令功能. ...

  6. Linux_Ubuntu之用户目录

    位于/home/user,称之为用户工作目录或家目录,表示方式: /home/user ~

  7. 运行上次失败用例(--lf 和 --ff)

    前言 “80%的bug集中在20%的模块,越是容易出现bug的模块,bug是越改越多“平常我们做手工测试的时候,比如用100个用例需要执行,其中10个用例失败了,当开发修复完bug后,我们一般是重点测 ...

  8. java游戏服务器--简单工厂模式

    先来学习下简单工厂模式! 我们知道在游戏里有很多的场景,例如:帮派场景,副本场景,野外场景... 现在我们有这样的需求: 1.我们需要进入帮派场景时---开始执行帮派任务. 2.我们需要进入副本场景时 ...

  9. JDBC操作数据库的基本操作

    JDBC操作数据库的基本步骤: 1)加载(注册)数据库驱动(到JVM). 2)建立(获取)数据库连接. 3)创建(获取)数据库操作对象. 4)定义操作的SQL语句. 5)执行数据库操作. 6)获取并操 ...

  10. CentOS7上安装配置破解Elasticsearch+Kibana 6.4.2-6.5.1全过程

    最近正在学习服务器应用平台的搭建的相关知识.有幸从朋友与书上了解到Elastic套件的使用,我花了两天的时间把最新的套件部署在我的服务器上,中间踩了数不清的坑.我把整个过程都记录了下来与各位有需要的朋 ...