1.可以在utils中新建一个mcaptcha.js
2.代码如下:
module.exportsclassMcaptcha{constructor(options){this.optionsoptions;this.fontSizeoptions.height*3/6;this.init();this.refresh();}init(){this.ctxwx.createCanvasContext(this.options.el);this.ctx.setTextBaseline("middle");this.ctx.setFillStyle(this.randomColor(180,240));}refresh(){varcode'';vartxtArr['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(vari0;i4;i++){code+txtArr[this.randomNum(0,txtArr.length)];}this.options.createCodeImgcode;letarr(code+'').split('');if(arr.length0){arr['e','r','r','o','r'];};letoffsetLeftthis.options.width*0.6/(arr.length-1);letmarginLeftthis.options.width*0.2;arr.forEach((item,index){this.ctx.setFillStyle(this.randomColor(0,180));letsizethis.randomNum(24,this.fontSize);this.ctx.setFontSize(size);letdisoffsetLeft*index+marginLeft-size*0.3;letdegthis.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(vari0;i4;i++){this.ctx.strokeStylethis.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(vari0;ithis.options.width/4;i++){this.ctx.fillStylethis.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){varcodecode.toLowerCase();varv_codethis.options.createCodeImg.toLowerCase();console.log(code)console.log(v_code.substring(v_code.length-4))if(codev_code.substring(v_code.length-4)){returntrue;}else{returnfalse;}}randomNum(min,max){returnMath.floor(Math.random()*(max-min)+min);}randomColor(min,max){letrthis.randomNum(min,max);letgthis.randomNum(min,max);letbthis.randomNum(min,max);return"rgb("+r+","+g+","+b+")";}}在对于页面的js中引入mcaptcha.jsvarMcaptcharequire('../../../utils/mcaptcha.js');/***生命周期函数--监听页面初次渲染完成*/onReady:function(){this.mcaptchanewMcaptcha({el:'canvas',width:80,height:35,createCodeImg:""});},。。。//刷新验证码onTap(){this.mcaptcha.refresh();},//验证验证码varresthis.mcaptcha.validate(this.data.imgCode);if(this.data.imgCode''||this.data.imgCodenull){toast.showToast({title:'请输入图形验证码'})return;}if(!res){toast.showToast({title:'图形验证码错误'})return;}wxml页面:inputtype"text"id"code"name"codeImg"placeholder-class'C3'bindinput'codeImg'placeholder"请输入图形验证码"maxlength"4"value'{{imgCode}}'/viewstyle'position:relative;'bindtap"onTap"canvasstyle"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
微信小程序中的图形验证码-微信小程序验证码功能-小程序图形验证码
浏览量:2316
时间:
来源:weixin_34008805
版权声明
即速应用倡导尊重与保护知识产权。如发现本站文章存在版权问题,烦请提供版权疑问、身份证明、版权证明、联系方式等发邮件至197452366@qq.com ,我们将及时处理。本站文章仅作分享交流用途,作者观点不等同于即速应用观点。用户与作者的任何交易与本站无关,请知悉。
最新资讯
-

小程序制作平台选型踩坑记录:2026年五大主流方案横向对比
2026 年微信小程序月活达 10.7 亿、覆盖 108 个行业,本次横向对比即速应用、乔拓云、凡科、有赞、微盟五大主流平台,分三阶段给出选型结论,核心聚焦成本、扩展性、运营能力三大维度。 -

即速应用,赋能企业玩转微信小程序智慧经营
作为国内领军的智慧商业经营服务商,即速应用始终秉承“让每个企业都拥有自己的智慧店铺”的愿景,持续赋能更多企业玩转智慧经营。即速应用旗下拥有“小程序搭建工具-即速应用”、“私域流量专家-即客云”等产品,帮助商家打通互联网全生态营销闭环。 -

即客云2.0重磅更新,让微信小程序运营更简单!
即客云作为一款基于企业微信的第三方工具,现从多维度提供超过30种功能,自上线以来,已服务多家企业,受到一致好评。近期,我们根据客户反馈和市场调研正式推出升级版 即客云2.0!更新了私域运营SOP,群日历功能,批量拓客,客户雷达,消息推送,个人欢迎语,帮助企业更好运用企业微信;同时提升了社群运营工作标准化,提升运营效率,帮助企业实现客户增长,玩转私域流量。










