繪制微信小程序驗(yàn)證碼功能的實(shí)例代碼
1.在 utils 文件中新建 mcaptcha.js 文件,寫(xiě)入以下代碼:
module.exports = class Mcaptcha {
//畫(huà)板
constructor(options) {
this.options = 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);
}
//繪制彩圖
refresh(code) {
let arr = (code + '').split('');
let width = this.options.width;
let height = this.options.height;
let ctx = this.ctx;
if (arr.length === 0) {
arr = ['e', 'r', 'r','o','r'];
};
let offsetLeft = width * 0.6 / (arr.length - 1);
let marginLeft = width * 0.2;
arr.forEach((item, index) => {
ctx.setFillStyle(this.randomColor(0, 180));
let size = this.randomNum(24, this.fontSize);
ctx.setFontSize(size);
let dis = offsetLeft * index + marginLeft - size * 0.3;
let deg = this.randomNum(-30, 30);
ctx.translate(dis, height*0.5);
ctx.rotate(deg * Math.PI / 180);
ctx.fillText(item, 0, 0);
ctx.rotate(-deg * Math.PI / 180);
ctx.translate(-dis, -height * 0.5);
})
//繪制干擾線
for (var i = 0; i < 2; i++) {
ctx.strokeStyle = this.randomColor(40, 180);
ctx.beginPath();
ctx.moveTo(this.randomNum(0, width), this.randomNum(0, height));
ctx.lineTo(this.randomNum(20, width), this.randomNum(2, height));
ctx.stroke();
}
//繪制干擾點(diǎn)
for (var i = 0; i < 30; i++) {
ctx.fillStyle = this.randomColor(0, 255);
ctx.beginPath();
ctx.arc(this.randomNum(0, width), this.randomNum(0, height), 1, 0, 2 * Math.PI);
ctx.fill();
}
ctx.draw();
}
//設(shè)置隨機(jī)數(shù)的顏色
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 + ")";
}
}
2.在需要驗(yàn)證碼的js文件中引入
let Mcaptcha = require("../../../utils/mcaptcha.js");
res.data 是要傳到mcaptcha.js 中的 code 值,也就是驗(yàn)證碼,可以隨意寫(xiě)為如:“ho55”,“a2sd” 等等。我這里是從后臺(tái)傳過(guò)來(lái)的數(shù)據(jù),(前人寫(xiě)的代碼,不好改動(dòng),只在前端加上干擾線和彩圖)。
new Mcaptcha({
el: 'canvas',
width: 100,
height: 30,
code: res.data
});
wxml 文件, bindtap="getImgYZM" 是 new Mcaptcha 的方法名,再次點(diǎn)擊可以換驗(yàn)證碼圖片。
<view bindtap="getImgYZM" class="yzm">
<canvas style="width:{{cvs.width}};height:{{cvs.height}};" canvas-id="canvas"></canvas>
</view>
如果是在前端設(shè)置隨機(jī)數(shù),可以在data:{} 里面寫(xiě)
data: {
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:""
}
//獲取隨機(jī)數(shù)
var res = "";
for(var i = 0; i < str.length ; i ++) {
var id = Math.ceil(Math.random()*35);
res += chars[id];
}
return res;
這一步可不看,以下是后端C#語(yǔ)言產(chǎn)生隨機(jī)數(shù)返給前端的代碼:
using JobClass;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.SessionState;
namespace WebSite.pcode
{
/// <summary>
/// getcode 的摘要說(shuō)明
/// </summary>
public class getcode : IHttpHandler, IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string 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";
string randStr = "";
//生成隨機(jī)數(shù) Random
Random randobj = new Random();
string[] arr = str.Split(',');
for (int i = 0; i < 4; i++)
{
randStr += arr[randobj.Next(arr.Length)];
}
context.Response.Write(randStr);
return;
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
到此這篇關(guān)于繪制微信小程序驗(yàn)證碼功能的實(shí)例代碼的文章就介紹到這了,更多相關(guān)微信小程序驗(yàn)證碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
js實(shí)現(xiàn)鼠標(biāo)拖動(dòng)功能
本文主要介紹了js實(shí)現(xiàn)鼠標(biāo)拖動(dòng)功能的實(shí)例代碼。具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-03-03
解決html按鈕切換綁定不同函數(shù)后點(diǎn)擊時(shí)執(zhí)行多次函數(shù)問(wèn)題
這篇文章主要介紹了如何解決html按鈕切換綁定不同函數(shù)后點(diǎn)擊時(shí)執(zhí)行多次函數(shù)問(wèn)題,需要的朋友可以參考下2014-05-05
Layer彈出層動(dòng)態(tài)獲取數(shù)據(jù)的方法
今天小編就為大家分享一篇Layer彈出層動(dòng)態(tài)獲取數(shù)據(jù)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
BootStrap Validator 根據(jù)條件在JS中添加或移除校驗(yàn)操作
這篇文章主要介紹了BootStrap Validator 根據(jù)條件在JS中添加或移除校驗(yàn)的相關(guān)資料,需要的朋友可以參考下2017-10-10
JS實(shí)現(xiàn)短信驗(yàn)證碼一鍵登錄功能
短信驗(yàn)證碼一鍵登錄是一種方便快捷的登錄方式,本文介紹了其原理并給出了一個(gè)簡(jiǎn)單的JavaScript示例,感興趣的朋友跟隨小編一起看看吧2024-05-05
javascript和jQuery實(shí)現(xiàn)網(wǎng)頁(yè)實(shí)時(shí)聊天的ajax長(zhǎng)輪詢
在做網(wǎng)頁(yè)實(shí)時(shí)聊天的時(shí)候常常需要長(zhǎng)輪詢,本文由于采用原生的JS及AJAX,所以簡(jiǎn)單易懂,通過(guò)這篇文章就可以建立一個(gè)簡(jiǎn)單的聊天室程序。2016-07-07

