$(function () {
var interval = 60; // 间隔函数,1秒执行
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) {
var result = false;
var ticketId = $("#js-ticketId_mail-vc").val();
var jsTicketValue = $("#js-ticket_mail-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_mail-vc").attr("captcha-after","js-btn-verify_mail-vc");
SimCaptcha.SetSuccessCallBack("js-btn-verify_mail-vc",function(){
_this.click();
});
$("#js-btn-verify_mail-vc").click();
return;
}
var code = ticketId;
var userName = $("#UserName").val();
tipsClassHandler();
if (result) {
if (code === '') {
modal.find('#graph-validation-error').html("验证码不能为空");
return;
}
}
$.ajaxPreventCSRF({
type: 'post',
dataType: 'json',
url: requestUrl,
data: { 'emailAddress': emailAddress, '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) {
refreshValidateCode()
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;
}
}
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_EmailValidateCode_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=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;
}
});
});