微信小程序中的图形验证码

摘要:
您可以创建一个新的mcaptcha。js在utils中的代码如下:module。exports=classMcaptcha{constructor(options){this.options=options;this.fontSize=options.height*3/6;this.init();this.refresh();}init(){this.ctx=wx.createCanvasContext(

可以在utils中新建一个mcaptcha.js

代码如下:

module.exports = class Mcaptcha {
constructor(options) {
this.options = options;
this.fontSize = options.height * 3 / 6;
this.init();
this.refresh();
}
init() {
this.ctx = wx.createCanvasContext(this.options.el);
this.ctx.setTextBaseline("middle");
this.ctx.setFillStyle(this.randomColor(180, 240));
}
refresh() {
var code = '';
var txtArr = ['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',0,1,2,3,4,5,6,7,8,9]
for(var i=0;i<4;i++){
code += txtArr[this.randomNum(0, txtArr.length)];
}
this.options.createCodeImg = code;
let arr = (code + '').split('');
if (arr.length === 0) {
arr = ['e', 'r', 'r','o','r'];
};
let offsetLeft = this.options.width * 0.6 / (arr.length - 1);
let marginLeft = this.options.width * 0.2;
arr.forEach((item, index) => {
this.ctx.setFillStyle(this.randomColor(0, 180));
let size = this.randomNum(24, this.fontSize);
this.ctx.setFontSize(size);
let dis = offsetLeft * index + marginLeft - size * 0.3;
let deg = this.randomNum(-30, 30);
this.ctx.translate(dis, this.options.height*0.5);
this.ctx.rotate(deg * Math.PI / 180);
this.ctx.fillText(item, 0, 0);
this.ctx.rotate(-deg * Math.PI / 180);
this.ctx.translate(-dis, -this.options.height * 0.5);
})
for (var i = 0; i < 4; i++) {
this.ctx.strokeStyle = this.randomColor(40, 180);
this.ctx.beginPath();
this.ctx.moveTo(this.randomNum(0, this.options.width), this.randomNum(0, this.options.height));
this.ctx.lineTo(this.randomNum(0, this.options.width), this.randomNum(0, this.options.height));
this.ctx.stroke();
}
for (var i = 0; i < this.options.width / 4; i++) {
this.ctx.fillStyle = this.randomColor(0, 255);
this.ctx.beginPath();
this.ctx.arc(this.randomNum(0, this.options.width), this.randomNum(0, this.options.height), 1, 0, 2 * Math.PI);
this.ctx.fill();
}
this.ctx.draw();
}
validate(code){
var code = code.toLowerCase();
var v_code = this.options.createCodeImg.toLowerCase();
console.log(code)
console.log(v_code.substring(v_code.length - 4))
if (code == v_code.substring(v_code.length - 4)) {
return true;
} else {
return false;
}
}
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 + ")";
}
}
 
在对于页面的js中引入mcaptcha.js
var Mcaptcha = require('../../../utils/mcaptcha.js');
 
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
this.mcaptcha=new Mcaptcha({
el: 'canvas',
80,
height: 35,
createCodeImg: ""
});
},
。。。
//刷新验证码
onTap(){
this.mcaptcha.refresh();
},
 
//验证验证码
var res = this.mcaptcha.validate(this.data.imgCode);
if (this.data.imgCode == '' || this.data.imgCode==null) {
toast.showToast({ title: '请输入图形验证码' })
return;
}
if (!res) {
toast.showToast({ title: '图形验证码错误' })
return;
}
wxml页面:
<input type="text" name="codeImg" placeholder- bindinput='codeImg' placeholder="请输入图形验证码" maxlength="4" value='{{imgCode}}'/>
<view bindtap="onTap">
<canvas canvas-id="canvas"></canvas>
</view>
 
效果:
微信小程序中的图形验证码第1张
 
 
参考:https://www.jianshu.com/p/064a80a3561a

免责声明:文章转载自《微信小程序中的图形验证码》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇iOS 代码规范ASP.NET MVC+EF框架+EasyUI实现权限管理系列(18)过滤器的使用和批量删除数据(伪删除和直接删除)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

JS事件 文本框内容改变事件(onchange)通过改变文本框的内容来触发onchange事件,同时执行被调用的程序。

以下代码显示,当用户更改文本框中的文本时,会弹出一个对话框“您更改了文本内容!”。运行结果:该任务补充了右侧编辑器的第13行。当文本框的内容发生更改时,将调用message()函数,并弹出对话框“您更改了文本内容!”。DOCTYPEHTML˃文本框内容更改事件functionmessage(){alert(“您更改了文本内容!”);}个人简介:请编写您的个人...

MySQL学习笔记:字符串前后补全0

遇到一个要求:如果位数小于6,则需要使用函数LPAD()和RPAD()自动完成6位。LPAD使用字符串padstr填充并完成左侧的str,直到其长度达到len个字符,并返回str。...

java实现word转pdf文件(高效不失真)

importjava.io.File;importjava.io.FileOutputStream;importjava.io.InputStream;importorg.aspectj.weaver.ast.Test;importcom.aspose.words.Document;importcom.aspose.words.License;importc...

sql server 日志软件过大设置办法

在使用sqlserver的过程中,sql日志文件的大小将随着其增长而受到限制。1.找到相应的库--˃属性--˃恢复模式,并将其更改为简单模式。2.选择库--˃任务--˃收缩--˃文件。3.选择日志文件收缩或数据库文件收缩。删除命令后占用的空间将在此处释放。数据库ldf文件的占用空间将更改为设置的空间大小。...

NFC应用于公交卡

由于历史遗留,NFC的兼容性实在太差。一个完备的NFC产品,应该包括三个部分:NFC硬件、统一的系统接口支持、App。这种情况下,手机NFC相当于公交公司的读卡和写卡器。...

将HTML文件转换为MD文件

html格式转md格式#模块html2textpipinstallhtml2text/pip3installhtml2text测试:importhtml2textashttext_maker=ht.HTML2Text()#读取html格式文件withopen('./*.html','r',encoding='UTF-8')asf:htmlpage=f.rea...