小程序实现图形验证码

分类:小程序开发示例 时间:2021-04-03 浏览:231
0

代码:https://gitee.com/wmc1125/daily-test/blob/master/%E5%B0%8F%E7%A8%8B%E5%BA%8F/2021/%E5%B0%8F%E7%A8%8B%E5%BA%8F%E5%AE%9E%E7%8E%B0%E5%9B%BE%E5%BD%A2%E9%AA%8C%E8%AF%81%E7%A0%81.php

 

初始化

var Mcaptcha = require('../../utils/mcaptcha.js');

 

显示

<view class="editInfo_bd_li clearfix">
    <text class="ednam left">图形验证码</text>
    <input class="editi_txt2 left" type="text" placeholder="" bindinput='changePic' value=" "></input>
    <view class="right ">
      <canvas bindtap="onTap" style="width:160rpx;height:40rpx;position:absolute;right:0rpx;bottom:240rpx;text-align: center;z-index:9999;" canvas-id="canvas"></canvas>
    </view>
  </view>
   
changePic(e){
  console.log(e.detail.value)
  this.setData({
    pic_code:e.detail.value
  })
},
 

刷新验证码

//刷新验证码
  onTap(){
    this.mcaptcha.refresh();
  },

 

判断

if (this.data.pic_code == '' || this.data.pic_code==null) {
    graceJS.msg("请输入图形验证码")
    return;
  }
  var res = this.mcaptcha.validate(this.data.pic_code);
  if(!res) {
    graceJS.msg("图形验证码错误")
    return;
  }

 

 

插件原始文件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]
var txtArr = [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.3);
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.3);
})
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 + ")";
}
}

 

小程序验证码生成+刷新+验证 https://developers.weixin.qq.com/s/9lsYvHmA7coi

 

 

 

本站文章如未注明出处则为原创,转载请注明出处,如有侵权请邮件联系站长

用户评论

评论前必须