/*! * jquery-captcha-lgh v1.0 (https://github.com/honguangli/jquery-captcha-lgh) * Copyright honguangli * Licensed under the MIT license */ ; let Captcha; (function($) { "use strict"; // 暺䁅恕�滨蔭 const defaults = { element: null, // canvas���� length: 4, // �嵗撉𣬚��鵭摨� code: [], // �嵗撉𣬚� autoRefresh: false, // 靚��鍂�嵗撉峕𦻖�藁��擧糓�炏�䌊�𢆡���鰵 }; const sCode = "A,B,C,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,W,X,Y,Z,1,2,3,4,5,6,7,8,9,0"; const aCode = sCode.split(","); const aLength = aCode.length; //�繮��硋��㺭蝏����鵭摨� Captcha = function(element, options) { const self = this; self.options = $.extend(true, defaults, options); self.element = element; self.refresh(); self.element.on('click', function () { self.refresh(); }); }; Captcha.prototype.refresh = function() { const self = this; const canvas_width = self.element.width(); const canvas_height = self.element.height(); const canvas = self.element[0]; //�繮��硋�canvas��撖寡情嚗峕�𥪜�� const context = canvas.getContext("2d"); //�繮��硋�canvas�𤫇�㦛���㴓憓�嚗峕�𥪜�䁅”瞍𠉛���𧼮蝱 canvas.width = canvas_width; canvas.height = canvas_height; const code = []; for (let i = 0; i < self.options.length; i++) { const j = Math.floor(Math.random() * aLength); //�繮��硋���𤩺㦤��蝝W�訫�� const deg = Math.random() * 30 * Math.PI / 180; //鈭抒��0~30銋钅𡢿����𤩺㦤撘批漲 const txt = aCode[j]; //敺堒���𤩺㦤��銝�銝芸��摰� code.push(txt.toLowerCase()); const x = 10 + i * 50; //����堒銁canvas銝羓�x��鞉�� const y =60 + Math.random() * 8; //����堒銁canvas銝羓�y��鞉�� context.font = "bold 3rem Arial"; context.translate(x, y); context.rotate(deg); context.fillStyle = randomColor(); context.fillText(txt, 0, 0); context.rotate(-deg); context.translate(-x, -y); } self.options.code = code; for (let i = 0; i <= 5; i++) { context.strokeStyle = randomColor(); context.beginPath(); // �遬蝷箇瑪�辺 context.moveTo(Math.random() * canvas_width, Math.random() * canvas_height); context.lineTo(Math.random() * canvas_width, Math.random() * canvas_height); // �遬蝷箏�讐�� const x = Math.random() * canvas_width; const y = Math.random() * canvas_height; context.moveTo(x, y); context.lineTo(x + 1, y + 1); context.stroke(); } //敺堒���𤩺㦤��憸𡏭𠧧�� function randomColor() { const r = Math.floor(Math.random() * 256); const g = Math.floor(Math.random() * 256); const b = Math.floor(Math.random() * 256); return "rgb(" + r + "," + g + "," + b + ")"; } }; Captcha.prototype.getCode = function() { return this.options.code.join(''); }; Captcha.prototype.valid = function(code) { const self = this; const ans = code.toString().toLowerCase() === self.getCode().toLowerCase(); if (!ans && self.options.autoRefresh) { self.refresh(); } return ans; }; })($); // /*! // * jquery-captcha-lgh v1.0 (https://github.com/honguangli/jquery-captcha-lgh) // * Copyright honguangli // * Licensed under the MIT license // */ // ; // let Captcha; // (function($) { // "use strict"; // // 默认配置 // const defaults = { // length: 4, // 校验码长度 // code: [], // 校验码 // width: 200, // canvas宽度 // height: 100, // canvas高度 // font: 'bold', // 文本字体样式 // resourceType: 'aA0', // 资源类型:a-小写字母、A-大写字母、0-数字,可任意组合 // resourceExtra: [], // 额外资源 // clickRefresh: true, // 点击刷新 // autoRefresh: true, // 调用校验接口后是否自动刷新 // caseSensitive: false, // 大小写是否敏感 // }; // const resourceUpper = ["A","B","C","E","F","G","H","J","K","L","M","N","P","Q","R","S","T","W","X","Y","Z"]; // const resourceLower = ["a","b","c","e","f","g","h","j","k","l","m","n","p","q","r","s","t","w","x","y","z"]; // const resourceNumber = ["0","1","2","3","4","5","6","7","8","9"]; // Captcha = function(element, options) { // const self = this; // self.options = $.extend(true, defaults, options); // self.element = element; // if (self.options.clickRefresh) { // self.element.on('click', function () { // self.refresh(); // }); // } // self.refresh(); // }; // // 刷新 // Captcha.prototype.refresh = function() { // const self = this; // let resource = []; // let resourceLength = 0; // // 合并资源 // if (self.options.resourceType.length > 0) { // if (self.options.resourceType.indexOf('A') !== -1) { // resource = resource.concat(resourceUpper); // } // if (self.options.resourceType.indexOf('a') !== -1) { // resource = resource.concat(resourceLower); // } // if (self.options.resourceType.indexOf('0') !== -1) { // resource = resource.concat(resourceNumber); // } // } // if (self.options.resourceExtra.length > 0) { // resource = resource.concat(self.options.resourceExtra); // } // // 配置资源为空 // if (resource.length === 0) { // resource = resourceUpper.concat(resourceLower).concat(resourceNumber) // } // resourceLength = resource.length; // const canvas = self.element[0]; // 获取canvas对象 // const context = canvas.getContext("2d"); // 获取canvas环境 // canvas.width = self.options.width; // canvas.height = self.options.height; // const code = []; // context.font = self.options.font; // for (let i = 0; i < self.options.length; i++) { // const txt = resource[Math.floor(Math.random() * resourceLength)]; // 得到随机的一个资源码 // code.push(txt); // const deg = Math.random() * 30 * Math.PI / 180; //产生0~30之间的随机弧度 // const x = 10 + i * 50; // 文本的x坐标 // const y = 40 + Math.random() * 8; // 文本的y坐标 // context.translate(x, y); // context.rotate(deg); // context.fillStyle = self.randomColor(); // context.fillText(txt, 0, 0); // context.rotate(-deg); // context.translate(-x, -y); // } // self.options.code = code; // for (let i = 0; i <= 5; i++) { // context.strokeStyle = self.randomColor(); // context.beginPath(); // // 显示线条 // context.moveTo(Math.random() * self.options.width, Math.random() * self.options.height); // context.lineTo(Math.random() * self.options.width, Math.random() * self.options.height); // // 显示小点 // const x = Math.random() * self.options.width; // const y = Math.random() * self.options.height; // context.moveTo(x, y); // context.lineTo(x + 1, y + 1); // context.stroke(); // } // }; // // 获取当前验证码 // Captcha.prototype.getCode = function() { // return this.options.code.join(''); // }; // // 校验 // Captcha.prototype.valid = function(code) { // const self = this; // let ans = false; // console.log(self.options.caseSensitive); // if (!self.options.caseSensitive) { // ans = code.toLowerCase() === self.getCode().toLowerCase(); // } else { // ans = code === self.getCode(); // } // if (!ans && self.options.autoRefresh) { // self.refresh(); // } // return ans; // }; // // 获取随机的颜色值 // Captcha.prototype.randomColor = function() { // const r = Math.floor(Math.random() * 256); // const g = Math.floor(Math.random() * 256); // const b = Math.floor(Math.random() * 256); // return "rgb(" + r + "," + g + "," + b + ")"; // } // })($);