smsVerify.js 3.54 KB
(function($) {    
  // 插件的定义    
  $.fn.smsVerify = function(options) {
	  var defaults = {
			 'sendSmsButton' : '#sendSmsButton',
			 'sendSmsUrl' : '/center/smscode',
			 'validatorUrl' : '/center/validator',
			 'validatorButton' : '',
			 'mobileText' : '#mobile_num',
			 'errorBox' : '#checkInfoBox',
			 'maxTime' : 60,
			 'btnClass' : 'btn_b_gr',
			 'retryBtnClass' : 'btn_retry',
			 'sendType' : 'validator',
			 'mobileCodeTxt' : '#mobile_code',
			 'codeLength' : 4,
			 'infoBox' : '#checkMobileBox',
			 'infoTpl' : '#verifyMobileSuccess',
			 'callback' : ''
	  };
	
	var opts = $.extend(defaults, options);
	if(typeof opts.uid == undefined || opts.uid == 0)
	{
		window.console.log('用户ID错误');
		return;
	}
	var mobile = $(opts.mobileText).val();
	
	var time = opts.maxTime || 60;
	
    // iterate and reformat each matched element    
    return this.each(function() { 
    	$this = $(this);
    	$this.click(function(){
    		methods.init(opts).sendSms();
    	});
    	if(opts.validatorButton != ''){
			$(opts.validatorButton).click(function(){
				methods.init(opts).sendValidator();
			});
		}
    });
  };
  var methods = {
		  options : {},
		  mobile : '',
		  time : 60,
		  init :function(options){
			  this.options = options;
			  this.mobile = $(options.mobileText).val();
			  this.time = options.maxTime;
			  return this;
		  },
		  sendSms : function(){
			  $(this.options.sendSmsButton).attr('disabled', true);
			  if(!(/^1[3|4|5|8][0-9]\d{4,8}$/.test(this.mobile)))
			  {
				  alert('手机号码错误');
				  $(this.options.sendSmsButton).attr('disabled', false);
				  return;
			  }
			  $.getJSON('/center/smscode',{'mobile':this.mobile,'sendType':this.options.sendType},function(jsonData){
				  if(jsonData.code != 200)
				  {
					  $(methods.options.sendSmsButton).attr('disabled', false);
					  return;
				  }
				  $(methods.options.sendSmsButton).removeClass(methods.options.btnClass).addClass(methods.options.retryBtnClass).html('(<span id="timeBox">'+methods.time+'秒</span>)后重新发送');
				  methods.changeTime();
			  });
		  },
		  changeTime : function()
		  {
			  methods.time--;
			  if(methods.time > 0){
				  $('#timeBox').html(methods.time + '秒');
				  setTimeout(methods.changeTime,1000);
			  }else{
				  $(methods.options.sendSmsButton).removeClass(methods.options.retryBtnClass).addClass(methods.options.btnClass).html('重新发送');
				  $(methods.options.sendSmsButton).attr('disabled', false);
			  }
		  },
		  sendValidator : function(){
			  $(methods.options.validatorButton).attr('disabled', true);
			  var code = $(this.options.mobileCodeTxt).val();
			  if(!/^[0-9]{4}$/.test(code)){
				  $(this.options.errorBox).html('校验码错误,请输入正确的校验码!').show();
				  $(this.options.validatorButton).attr('disabled', false);
				  return;
			  }
			  $.getJSON('/center/validator', {'mobile':this.mobile,'mobile_code' : code, 'sendType' : this.options.sendType}, function (jsonData){
				  if(jsonData.code != 200){
					  $(methods.options.errorBox).html('手机校验码错误!').show();
					  $(methods.options.validatorButton).attr('disabled', false);
					  return;
				  }
				  $(methods.options.infoBox).html($(methods.options.infoTpl).html()).removeClass('cod_red').addClass('cod_green');
				  $(methods.options.validatorButton).attr('disabled', false);
				  if(typeof methods.options.callback != undefined && methods.options.callback != '')
				  {
					  eval(methods.options.callback +'()',1);
				  }
				  return;
			  });
		  }
  };
// 闭包结束    
})(jQuery);