let icaptchaXML; let icaptchactx; let icaptchas = 2; let icaptchac = "#ffffff"; let icaptchat = "Secure Code Invalid"; let icaptchag = 0; // ======================= // 验证码验证 // ======================= function checking_icaptcha() { if (window.XMLHttpRequest) { icaptchaXML = new XMLHttpRequest(); } else { icaptchaXML = new ActiveXObject("Microsoft.XMLHTTP"); } icaptchaXML.open("POST", "/validate/", false); icaptchaXML.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded", ); icaptchaXML.send( "code=" + encodeURIComponent(document.getElementById("secure").value), ); return icaptchaXML.responseText; } // ======================= // 登录提交 // ======================= function login_submit() { let validate = checking_icaptcha(); console.log(validate); if (validate == "ok=True") { $("#submit_login").submit(); } else { alert("Secure Code Invalid"); document.getElementById("secure").value = ""; generateCaptcha(); return false; } } // ======================= // Load PHP Captcha // ======================= function loadicaptcha() { if (window.XMLHttpRequest) { icaptchaXML = new XMLHttpRequest(); } else { icaptchaXML = new ActiveXObject("Microsoft.XMLHTTP"); } icaptchaXML.open("POST", "/icaptcha/", true); icaptchaXML.onreadystatechange = readicaptcha; icaptchaXML.send(); } // ======================= // Draw PHP Captcha // ======================= function readicaptcha() { if (icaptchaXML.readyState != 4) { return; } if (icaptchaXML.status != 200) { return; } const captcha = document.getElementById("captchaCanvas"); if (!captcha) { return; } const container = document.getElementById("icaptcha"); if (!container) { return; } const ctx = captcha.getContext("2d"); const width = container.offsetWidth; const height = container.offsetHeight; captcha.width = width; captcha.height = height; ctx.clearRect(0, 0, width, height); const data = icaptchaXML.responseText; const points = new Array(6); const count = data.length / 12; const c = width / 250; const h = height / 100; // X / Y offset const f = icaptchag - 5; const l = icaptchag - 10; for (let e = 0; e < count; e++) { ctx.beginPath(); ctx.lineWidth = icaptchas; ctx.strokeStyle = icaptchac; for (let t = 0; t < 6; t++) { points[t] = parseInt(data.substr(e * 12 + t * 2, 2), 16); } ctx.moveTo((points[0] + f) * c, (points[1] + l) * h); ctx.quadraticCurveTo( (points[4] + f) * c, (points[5] + l) * h, (points[2] + f) * c, (points[3] + l) * h, ); ctx.stroke(); } } // ======================= // 初始化 Captcha // ======================= function generateCaptcha() { const container = document.getElementById("icaptcha"); if (!container) { return; } const canvas = document.getElementById("captchaCanvas"); if (!canvas) { return; } icaptchactx = canvas.getContext("2d"); loadicaptcha(); }