锘?(function () {
var interval = 60; // 闂撮殧鍑芥暟锛?绉掓墽琛
var curInterval; // 褰撳墠鍓╀綑绉掓暟
var timerObj; // timer鍙橀噺锛屾帶鍒舵椂闂
var requiredTips = '閭楠岃瘉鐮佷笉涓虹┖銆?;
var emptyEmailTips = '璇峰~鍐欓偖绠便€?;
var data = $('[data-type=emailmessagevalidate]');
var requestUrl,
codeElement,
basisElement,
tips;
var codeIntervalTime = data.attr('data-val-codeIntervalTime');
var reg = /^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]*[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/;
var showGraphValidateCodeUrl = '/user/ShowGraphValidateCode';
if (codeIntervalTime == '' || codeIntervalTime == null || codeIntervalTime == undefined) {
codeIntervalTime = '60';
}
InitCodeIntervalTime();
$.each(data,
function (i, v) {
var getCodeBtn = $(v).children('input[type=button]');
var element = $(v).attr('data-val-codeelement');
var emailMessageValidateCode = $('#' + element);
if (emailMessageValidateCode.length > 0) {
emailMessageValidateCode.rules('add', { required: true, messages: { required: requiredTips } });
}
getCodeBtn.unbind().click(function () {
getCode(getCodeBtn);
});
});
function getCode(_this) {
var checkEmail = $(_this).data('nocheckemail');
$(_this).attr("message", "閲嶅彂楠岃瘉鐮?);
setInfo(_this);
var modal = _this.parents().children('#ShowGraphValidateCodeModal');
var isShow = modal.css('display');
tipsClassHandler();
var emailAddress = $('#' + basisElement).val();
if (emailAddress) {
emailAddress = emailAddress.trim();
}
if (emailAddress === '' || !emailAddress) {
tips.html(getTipsHtml(false, emptyEmailTips));
return;
}
if (!reg.test(emailAddress) && !checkEmail) {
return false;
}
$.get(showGraphValidateCodeUrl,
function (data) {
if (data == 'True' && modal.length == 0) {
tips.html(getTipsHtml(false, "缂哄皯鍥惧舰楠岃瘉鐮佹爣璁帮紝璇疯仈绯荤鐞嗗憳锛?));
return false;
}
if (data == 'True' && isShow == 'none') {
refreshValidateCode();
$(modal).find('#GraphValidateCode').val('');
$(modal).find('#graph-validation-error').empty();
modal.show();
$('.graph-code-modal-backdrop').show();
$(modal).find("#graph_confirmNotApprove").addClass('email-confirmNotApprove');
$(modal).find('.email-confirmNotApprove').unbind().click(function () {
getCode(_this);
});
var height = $(_this).offset().top - $(document).scrollTop() - $('#ShowGraphValidateCodeModal .modal-content').height();
SetHeight(height);
return;
}
if ((data == 'True' && isShow != 'none') || data == 'False') {
var code = $(modal).find('#GraphValidateCode').val();
var userName = $("#UserName").val();
tipsClassHandler();
if (data == 'True') {
if (code === '') {
modal.find('#graph-validation-error').html("楠岃瘉鐮佷笉鑳戒负绌?);
return;
}
}
$.ajaxPreventCSRF({
type: 'post',
dataType: 'json',
url: requestUrl,
data: { 'emailAddress': emailAddress, 'userName': userName, 'code': code },
success: function (result) {
if (result.state == 'error') {
modal.hide();
$('.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) {
modal.hide();
$('.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 {
modal.hide();
$('.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;
$(modal).find('.validationcode img').click();
$(modal).find('#graph-validation-error').html('');
}
}
}
});
}
});
}
// 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::ValidateCode::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() {
$('#ShowGraphValidateCodeModal [data-type=searchcaptcha]')
.each(function () {
var $captchaImage = $(''),
self = $(this),
setting = self.data();
if (self.find("img").length === 0) {
$captchaImage.attr('title', '鐪嬩笉娓咃紵鎹竴寮?);
$captchaImage.attr('src', setting.captchaUrl);
$captchaImage.click(function () {
$captchaImage.attr('src', setting.captchaUrl + '?code=' + Math.random());
});
self.append($captchaImage);
}
});
}
$("#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=emailmessagevalidate]');
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;
}
});
});