锘?(function () {
var interval = 60; // 闂撮殧鍑芥暟锛?绉掓墽琛
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[012356789]|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;
}
});
});