可以在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',
width: 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" id="code" name="codeImg" placeholder-class='C3' bindinput='codeImg' placeholder="请输入图形验证码" maxlength="4" value='{{imgCode}}'/>
<view style='position:relative;' bindtap="onTap">
<canvas style="width:160rpx;height:70rpx;position:absolute;right:0rpx;bottom:10rpx;text-align: center;z-index:9999;" canvas-id="canvas"></canvas>
</view>
 
效果:
 
 
参考:https://www.jianshu.com/p/064a80a3561a

最新文章

  1. VS自定义项目模板:[2]创建VSIX项目模板扩展
  2. Linux下搭建Windows KMS服务器
  3. C++ const
  4. python之路-Day5
  5. MySQL 基础语句的练习
  6. Servlet的几种跳转(转)
  7. CSS/JS图片鼠标悬浮一道光闪过
  8. Intersection of Two Arrays | &amp; ||
  9. 【MYSQL】创建虚表来辅助数据库查询
  10. NeHe OpenGL教程 第九课:移动图像
  11. zedboard上移植OPENCV库
  12. BZOJ 1854: [Scoi2010]游戏 无向图判环
  13. [AngularJS] $http cache
  14. asp.net application
  15. floyd学习中
  16. js模拟jq获取id
  17. PHP访问连接MYSQL数据库
  18. temp--达州银行
  19. (转)java之Spring(IOC)注解装配Bean详解
  20. python2到python3代码转化:2to3

热门文章

  1. 相关性系数缺点与证明 k阶矩
  2. PHP 7.2 RC3 on CentOS/RHEL 7.3 via Yum
  3. MVC Web Api 发布到Azure报错
  4. UVA10600 ACM Contest and Blackout —— 次小生成树
  5. ACTION 中 单表查询语句 SQL写法
  6. C++在循环内和循环外定义变量的差异
  7. Masonry 比例(multipliedBy)
  8. 并不对劲的p2664树上游戏
  9. Ural2040:Palindromes and Super Abilities(离线&amp;manecher算法)
  10. bzoj1296 [SCOI2009]粉刷匠——背包