$(function () { var interval = 60; // 间隔函数,1秒执行 var curinterval; // 当前剩余秒数 var timerobj; // timer变量,控制时间 var requiredtips = '短信验证码 字段是必需的。'; var emptyphonenumbertips = '请填写手机号码。'; var data = $('[data-type=phonemessagevalidate]'); var reg = /^1(3[0-9]|4[57]|5[0-35-9]|6[6]|8[0-9]|9[1589]|7[035678])\d{8}$/; var requesturl, codeelement, basiselement, tips; var codeintervaltime = data.attr('data-val-codeintervaltime'); if (codeintervaltime == '' || codeintervaltime == null || codeintervaltime == undefined) { codeintervaltime = '60'; } var showgraphvalidatecodeurl = '/user/showgraphvalidatecode'; initcodeintervaltime(); $.each(data, function (i, v) { var getcodebtn = $(v).children('input[type=button]'); var element = $(v).attr('data-val-codeelement'); var phonemessagevalidatecode = $('#' + element); if (phonemessagevalidatecode.length > 0) { phonemessagevalidatecode.rules('add', { required: true, messages: { required: requiredtips } }); } getcodebtn.unbind().click(function () { getcode(getcodebtn); }); }); function getcode(_this) { var checkphone = $(_this).data('nocheckphone'); $(_this).attr("message", "重发验证码"); setinfo(_this); var modal = _this.parents().children('#showgraphvalidatecodemodal'); var isshow = modal.css('display'); tipsclasshandler(); var phonenumber = $('#' + basiselement).val(); if (phonenumber) { phonenumber = phonenumber.trim(); } if (phonenumber === '' || !phonenumber) { tips.html(gettipshtml(false, emptyphonenumbertips)); return; } if (!reg.test(phonenumber) && !checkphone) { return false; } $.get(showgraphvalidatecodeurl, function (data) { var result = false; var ticketid = $("#js-ticketid_phone-vc").val(); var jsticketvalue = $("#js-ticket_phone-vc").val(); if (data.tostring() == "true" || data.tostring() == "true") { result = true; } if (result && modal.length == 0) { tips.html( gettipshtml(false, "缺少图形验证码标记,请联系管理员!") ); return false; } if (result && isshow == "none" && !ticketid) { $("#js-btn-verify_phone-vc").attr( "captcha-after", "js-btn-verify_phone-vc" ); simcaptcha.setsuccesscallback( "js-btn-verify_phone-vc", function () { _this.click(); } ); $("#js-btn-verify_phone-vc").click(); return; } var code = $(modal).find("#graphvalidatecode").val(); var username = $("#username").val(); tipsclasshandler(); $.ajaxpreventcsrf({ type: "post", datatype: "json", url: requesturl, data: { phonenumber: phonenumber, username: username, captchaticketid:ticketid,captchaticket:jsticketvalue, }, success: function (result) { if (result.state == "error") { $(".graph-code-modal-backdrop").hide(); tips.html(gettipshtml(false, result.message)); } else { if ( !result.issuccess && result.isgraphvalidatecode ) { if ( $(modal).find("#graph-validation-error") .length == 0 ) { tips.html( gettipshtml(false, "缺少图形验证码") ); } else { $(modal) .find("#graph-validation-error") .html(result.tips); $(modal) .find(".validationcode img") .click(); } } else { if (result.issuccess) { $(".graph-code-modal-backdrop").hide(); // 设置按钮显示效果,倒计时 _this.attr("disabled", "true"); interval = result.surplussecond; curinterval = interval; timerobj = window.setinterval( setremaintime, 1000 ); // 启动计时器,1秒执行一次 tips.html(gettipshtml(true, result.tips)); } else { $(".graph-code-modal-backdrop").hide(); if (result.surplussecond > 0) { _this.attr("disabled", "true"); curinterval = result.surplussecond; timerobj = window.setinterval( setremaintime, 1000 ); // 启动计时器,1秒执行一次 } tips.html(gettipshtml(false, result.tips)); } window.timerobj = timerobj; } } refreshvalidatecode(); }, }); refreshvalidatecode(); }); } // timer处理函数 function setremaintime() { if (curinterval === 0) { window.clearinterval(timerobj); // 停止计时器 $.each(data, function (i, v) { var getcodebtn = $(v).children('input[type=button]'); var message = "发送验证码"; if ($(getcodebtn).attr("message")) { message = "重发验证码"; } getcodebtn.removeattr('disabled').val(message); // 启用按钮 }); } else { curinterval--; $.each(data, function (i, v) { var getcodebtn = $(v).children('input[type=button]'); getcodebtn.val(curinterval + 's'); }); } } function gettipshtml(issuccess, tipstext) { if (issuccess) { return '' + tipstext + ''; } return '' + tipstext + ''; } function tipsclasshandler() { tips.removeclass('field-validation-valid'); tips.addclass('field-validation-error'); } function initcodeintervaltime() { var cookie = getcookies(); var cookietime = cookie["power::phonevalidatecode::intervaltime"]; var nowtime = parseint(new date().gettime() / 1000); var intervaltime = parseint(codeintervaltime); if (intervaltime < 0 || intervaltime == 0) { intervaltime = 60; } if (nowtime - parseint(cookietime) < intervaltime) { if (nowtime - parseint(cookietime) < 0) { curinterval = 60; } else { curinterval = intervaltime - (nowtime - parseint(cookietime)); } // 设置按钮显示效果,倒计时 $.each(data, function (i, v) { var getcodebtn = $(v).children('input[type=button]'); getcodebtn.attr('disabled', 'true'); }); timerobj = window.setinterval(setremaintime, 1000); // 启动计时器,1秒执行一次 } } function getcookies() { var cookiestr = document.cookie; var cookie = {}; if (cookiestr != '') { var cookiestrs = cookiestr.split(';'); $.each(cookiestrs, function (i, v) { var item = v.split('='); cookie[item[0].replace(/(^\s*)|(\s*$)/g, "")] = item[1].replace(/(^\s*)|(\s*$)/g, ""); }); } return cookie; } function refreshvalidatecode() { window.captchacallbackdynamicname(function () { }); } $("#showgraphvalidatecodemodal .close").click(function () { $(this).parents('#showgraphvalidatecodemodal').hide(); $('.graph-code-modal-backdrop').hide(); }); function setheight(index) { $('#showgraphvalidatecodemodal .modal-dialog').css('margin-top', index); } function setinfo(_this) { var info = _this.parents('[data-type=phonemessagevalidate]'); requesturl = info.attr('data-val-requesturl'); codeelement = info.attr('data-val-codeelement'); basiselement = info.attr('data-val-basiselement'); tips = info.next('[data-valmsg-for=' + codeelement + ']'); } $(data).children('input').on('keydown', function () { var value = $(this).val(); if (value.length >= 8) { return false; } }); });