怎么在微信小程序中实现一个验证码功能

  介绍

这篇文章将为大家详细讲解有关怎么在微信小程序中实现一个验证码功能,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

1。在<代码>跑龙套文件中新建<代码> mcaptcha。js代码文件,写入以下代码:

module.exports =, class  Mcaptcha  {//画才能板   ,构造函数(选项){   时间=this.options 才能;选项;   时间=this.fontSize 才能;options.height  *, 3,/, 4;   this.init才能();   this.refresh才能(this.options.code);   ,}   ,init () {   时间=this.ctx 才能;wx.createCanvasContext (this.options.el);   this.ctx.setTextBaseline才能(“middle");   this.ctx.setFillStyle才能(this.randomColor (180,, 240));   this.ctx.fillRect才能(0,0,this.options.width,, this.options.height);   ,}//才能绘制彩图   ,刷新(代码){   let 才能;arr =,(时间+ code  & # 39; & # 39;) .split (& # 39; & # 39;);   let 才能;width =, this.options.width;   let 才能;height =, this.options.height;   let 才能;ctx =, this.ctx;   if 才能;(arr.length ===, 0), {   ,才能arr =, (& # 39; e # 39;,, & # 39; " # 39;,, & # 39; " # 39;, & # 39; o # 39;, & # 39; " # 39;];   ,,};   let 才能;offsetLeft =, width  *, 0.6,/, (arr.length 作用;1);   let 才能;marginLeft =, width  *, 0.2;   arr.forEach才能((项目,,指数),=祝辞,{   ,,ctx.setFillStyle (this.randomColor (0, 180));   ,,let  size =, this.randomNum(24日,this.fontSize);   ,,ctx.setFontSize(大小);   ,,let  dis =, offsetLeft  *, index  +, marginLeft 作用;size  *, 0.3;   ,,let  deg =, this.randomNum (-30,, 30);   ,,ctx.translate(说,,身高* 0.5);   ,,ctx.rotate (deg  *, Math.PI /, 180);   ,,ctx.fillText(项目,,0,0);   ,,ctx.rotate (-deg  *, Math.PI /, 180);   ,,ctx.translate (dis, -height  *, 0.5);   })才能//才能绘制干扰线   for 才能;(var 小姐:=,0;,小姐:& lt,, 2,,我+ +),{   时间=ctx.strokeStyle 才能;this.randomColor (40, 180);   ctx.beginPath才能();   ctx.moveTo才能(this.randomNum(0,宽度),this.randomNum(0,高度));   ctx.lineTo才能(this.randomNum(20,,宽度),this.randomNum(2,高度));   ctx.stroke才能();   ,,}//才能绘制干扰点   for 才能;(var 小姐:=,0;,小姐:& lt;, 30;,我+ +),{   时间=ctx.fillStyle 才能;this.randomColor (0, 255);   ctx.beginPath才能();   ctx.arc才能(this.randomNum(0,宽度),this.randomNum(0,高度),,1,,0,,2,*,Math.PI);   ctx.fill才能();   ,}   ctx.draw才能();   ,}//才能设置随机数的颜色   ,randomNum(最小值,最大值),{   return 才能;Math.floor (math . random (), *, (max 安康;分钟),+,min);   ,}   ,randomColor(最小值,最大值),{   let 才能;r =, this.randomNum(最小值,最大值);   let 才能;g =, this.randomNum(最小值,最大值);   let 才能;b =, this.randomNum(最小值,最大值);   return 才能“rgb(“时间+大敌;;r  +,,,,, +, g  +,,,,, +, b  +,“)“;   ,}   }

2。在需要验证码的<代码> js代码文件中引入

let  Mcaptcha =,要求(“. ./. ./. ./跑龙套/mcaptcha.js");

<代码> res。数据是要传到<代码> mcaptcha。js代码中的<代码> 值代码,也就是验证码,可以随意写为如:<代码>“ho55”、“a2sd”等等。我这里是从后台传过来的数据,(前人写的代码,不好改动,只在前端加上干扰线和彩图)。

new  Mcaptcha ({   el才能:& # 39;帆布# 39;   ,,宽度:100年,   高度:才能,30日   代码:才能res.data   });

<代码> wxml 文件,<代码> bindtap=癵etImgYZM" 是<代码>新Mcaptcha> & lt; view  bindtap=癵etImgYZM",类=皔zm"比;   & lt;才能canvas , canvas-id=癱anvas"祝辞& lt;/canvas>   & lt;/view>

如果是在前端设置随机数,可以在<代码>数据:{}里面写

,数据:,{   ,,str:“0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F, G, H, I, J, K, L, M, N, P, Q, R, S, T, U, V, W, X, Y, Z"   ,,randStr:“;”   ,}//获取随机数   var  res =,““   (var 小姐:=,0;,小姐:& lt;, str.length ;,小姐:+ +),{   var 才能;id =, Math.ceil (math . random () * 35);   +=res 才能;字符(id);   }   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

怎么在微信小程序中实现一个验证码功能