可以在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. 网页或微信小程序中使元素占满整个屏幕高度

    在项目中经常要用到一个容器元素占满屏幕高度和宽度,然后再在这个容器元素里放置其他元素. 宽度很简单就是width:100% 但是高度呢,我们知道的是height:100%必须是在父元素的高度给定了的情 ...

  2. 在微信小程序中使用富文本转化插件wxParse

    在微信小程序中我们往往需要展示一些丰富的页面内容,包括图片.文本等,基本上要求能够解析常规的HTML最好,由于微信的视图标签和HTML标签不一样,但是也有相对应的关系,因此有人把HTML转换做成了一个 ...

  3. 微信小程序中发送模版消息注意事项

    在微信小程序中发送模版消息 参考微信公众平台Api文档地址:https://mp.weixin.qq.com/debug/wxadoc/dev/api/notice.html#模版消息管理 此参考地址 ...

  4. 微信小程序中placeholder的样式

    通常,现代浏览器大多支持::placeholder选择器,用于设置placeholder的样式,但是在微信小程序中并不支持这种方式,而是提供了一个专门的属性(placeholder-class)来处理 ...

  5. 微信小程序中转义字符的处理

    在微信小程序开发过程中,有时候会用到常用的一些特殊字符如:‘<’.‘>’.‘&’.‘空格’等,微信小程序同样支持对转义字符的处理,下面提供两种方法用来处理微信小程序中转义字符的处理 ...

  6. 微信小程序中用户登录和登录态维护

    提供用户登录以及维护用户的登录状态,是一个拥有用户系统的软件应用普遍需要做的事情.像微信这样的一个社交平台,如果做一个小程序应用,我们可能很少会去做一个完全脱离和舍弃连接用户信息的纯工具软件. 让用户 ...

  7. 微信小程序中的组件使用1

    不管是vue还是react中,都在强调组件思想,同样,在微信小程序中也是使用组件思想来实现页面复用的,下面就简单介绍一下微信小程序中的组件思想. 组件定义与使用 要使用组件,首先需要有组件页面和使用组 ...

  8. 微信小程序中如何使用WebSocket实现长连接(含完整源码)

    本文由腾讯云技术团队原创,感谢作者的分享. 1.前言   微信小程序提供了一套在微信上运行小程序的解决方案,有比较完整的框架.组件以及 API,在这个平台上面的想象空间很大.腾讯云研究了一番之后,发现 ...

  9. 全栈开发工程师微信小程序-中(下)

    全栈开发工程师微信小程序-中(下) 微信小程序视图层 wxml用于描述页面的结构,wxss用于描述页面的样式,组件用于视图的基本组成单元. // 绑定数据 index.wxml <view> ...

随机推荐

  1. 总结文件操作函数(二)-C语言

    格式化读写: #include <stdio.h> int printf(const char *format, ...);                   //相当于fprintf( ...

  2. atom及其插件activate-power-mode下载安装

    Atom是Github推出的一个文本编辑器,其中包含很多插件可以自行下载安装,其中一个最近比较火的就是插件activate-power-mode,可以实现打字屏振效果, 打字带特效哦,所以最近就尝试安 ...

  3. bzoj5333: [Sdoi2018]荣誉称号

    请不要去改题目给的输入,不然你会wa穿... 这么故弄玄虚的题目,肯定要先转换问题 看到这个不断的除2想起别人家的线段树的写法...x的两个孩子是x<<1和x<<1|1 然后问 ...

  4. jQuery - AJAX 级联变动

    此篇文章主要是用来记忆使用JQUERY+AJAX技术实现 二级级联变动 : 当第一个下拉框变动时,第二个下拉列表框中也将会随之变动. JSP: ---------------------------- ...

  5. Xenocode Postbuild 2010 for .NET 使用

    代码混淆工具 参考地址1:http://blog.csdn.net/yanpingsoft/article/details/7997212 参考地址2:http://www.cnblogs.com/w ...

  6. Spring Boot之配置文件值注入(@ConfigurationProperties)

    前言:Spring Boot配置文件值的注入有两种方式,分别是 @ConfigurationProperties @Value 这里我们使用第一种 首先我们创建一个application.yml文件, ...

  7. python的安装教学

    1.首先登陆到python的官方网站 https://www.python.org/ 2.鼠标放在Download上,点击下面对应的型号,我的是Windows 3.点击Windows到此页面,点击3. ...

  8. POJ2486 Apple Tree 【树上背包】

    一句话题意:一棵树,一共n个点,每个点上有一个权值,求从1出发,走k步,最多能遍历到的权值.可以往回走. 第一(二)道树上背包题,先是看了dalao的题解,改了一点就过样例了.然而....TLE??? ...

  9. HTML中a标签自动识别电话、邮箱

    HTML中a标签自动识别电话.邮箱 联系电话:<a href="tel:010-88888888">010-88888888</a><br> 联 ...

  10. Go JSON嵌套解析失败

    //结构体成员变量名首字母要大写,要小写需要用tag,请看范例. //如果没有tag,嵌套时就无法解析属性,解析失败就是空值. type messageRow struct { UserID stri ...