randomNum(max, min) {
return parseInt(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 + ")";
},
getImgCode() {
let context = uni.createCanvasContext('imgcanvas', this),
w = 72,
h = 36;
context.setFillStyle("white");
context.setLineWidth(5);
context.fillRect(0, 0, w, h);
let letterStr = "a,b,c,d,e,f,g,h,i,j,k,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,1,2,3,4,5,6,7,8,9,0";
let pool = letterStr.split(','),
str = '';
for (let i = 0; i < 4; i++) {
let c = pool[this.randomNum(0, pool.length - 1)];
let deg = this.randomNum(-30, 30);
context.setFontSize(18);
context.setTextBaseline("top");
context.setFillStyle(this.randomColor(80, 150));
context.save();
context.translate(30 * i + 15, parseInt(h / 1.5));
context.rotate(deg * Math.PI / 180);
context.fillText(c, -15 + 5, -15);
context.restore();
str += c;
}
console.log('随机验证码', str)
this.verCode = str;
for (let i = 0; i < 40; i++) {
context.beginPath();
context.arc(this.randomNum(0, w), this.randomNum(0, h), 1, 0, 2 * Math.PI);
context.closePath();
context.setFillStyle(this.randomColor(150, 200));
context.fill();
}
context.draw();
},
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步