/*2017-01-16 add by yb
 * describe:微信前端，用户手机号码绑定
 */
$(function(){
	var wait=60;
	var login = {
		init: function(){
			this.bindEvent();
		},
		bindEvent: function(){
			var _this = this;
			//手机输入框失去焦点
			$(".phone-input").on("blur",function(){
				var reg  = /^(?:13\d|15\d|18\d|17\d|14\d)-?\d{5}(\d{3}|\*{3})$/; //手机号码
				var phone = $(this).val();
				if(!reg.test(phone)){
					$(this).addClass("error")
				}else{
					$(this).removeClass("error")
				}
			});
			//手机验证码输入框失去焦点
			$(".code-input").on("blur",function(){
				var code = $(this).val();
				if(!code){
					$(this).addClass("error")
				}else{
					$(this).removeClass("error")
				}
			})
			//手机短息验证码输入框失去焦点
			$(".msg-input").on("blur",function(){
				
				var msg = $(this).val();
				if(!msg){
					$(this).addClass("error")
				}else{
					$(this).removeClass("error")
				}
			});
			//换取图片验证
			$("#imgss").on("click",function(){
				var contextPath=$("#contextPath").val();
				var login = $("#login").val();
				$("#imgss").attr("src",contextPath+"/wechat/code/imageCode?superId=" + login + "&date="+ Math.random() + 100);
			})
		    //点击获取验证码
		    $(".msg-code-btn").on("click",function(event){
		    	var telVal  = $(".phone-input").val();
		    	var $this = $(this)
		    	if(!telVal) {
		    		$(".phone-input").addClass("error");
		    		return false;
		        }else{
		    		_this.time($this);
		    		var contextPath=$("#contextPath").val();
		    		$.ajax({
		    	    	url: contextPath+"/wechat/sms/getAuthCode",
		    	    	data: {
		    	    		userMobile:telVal,
		    	    		openId:$("#openId").val(),
		    	    		orgId:_this.GetQueryString("organizationId")
		    	    	},
		    	        type: "POST",
		    	        dataType: "json",
		    	        success: function(json){
		    	        	
		    	        },
		    	        error: function(){
		    	        }
		    	    });	
		    	}
		    });
			//确认登录
			$(".login-btn").on("click",function(){
				_this.ajaxSubmit();
			})
		},
		/**
		 * 获取url的参数
		 * @param name 参数名
		 */
		GetQueryString: function (name) {
		    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
		    var r = window.location.search.substr(1).match(reg);
		    if (r != null) return unescape(r[2]); return null;
		},
		/**
		 * 倒计时
		 * @param $dom
		 * 
		 */
		time: function($dom){
			
			var _this =this;
			if (wait == 0) {
				$dom.attr("disabled",false);
				$dom.html("获取验证码")
				wait = 60;
			} else {
				$dom.attr("disabled", true);
				$dom.html("重新发送(" + wait + ")");
				wait--;
				setTimeout(function() {
					_this.time($dom)
				},1000)
			}
		},
		//登录
		ajaxSubmit: function(){
			    var telVal  = $(".phone-input").val();
		        var veriVal = $(".msg-input").val();
		        var codes = $(".code-input").val();
		        var organizationId = this.GetQueryString("organizationId");
		        if(!telVal) {
		        	$(".phone-input").addClass("error");
		        	return false;
		        }
		        if(!veriVal) {
		        	$(".msg-input").addClass("error");
		        	return false;
		        }
		        if(!codes){
		        	$(".code-input").addClass("error");
		        	return false;
		        }
	        	var contextPath=$("#contextPath").val();
	        	var openId=$("#openId").val();
	        	var developId=$("#developId").val();
	        	var refererId=$("#refererId").val();
	        	var serviceCode=$("#serviceCode").val();
	        	var st=$("#st").val();
	        	var mECode=$("#mECode").val();
	        	$.ajax({
	    	    	url: contextPath+"/wechat/appUser/submitphone",
	    	    	data: {openId:openId,developId:developId,authCode:veriVal,userMobile:telVal,st:st,fromType:$("#fromType").val(),codes:codes},
	    	        type: "POST",
	    	        dataType: "json",
	    	        success: function(result){
	    	        	if("1"==result.result&&"200"==result.resultCode){
	    	        		var userId=result.returnDTO.userId;
	    	        		var status=result.returnDTO.status;
	    	        		var stResult=result.returnDTO.st;
	    	        		var shopUrlKey=result.returnDTO.shopUrlKey;
	    	        		var fromType=result.returnDTO.fromType; //来源于哪个入口
	    	        		var redirectUrl=$("#redirectUrl").val();
	    	        		
	    	        		if("index"==fromType){
	    	        			var redirectUrl=$("#redirectUrl").val();
	    	        			window.location.href=contextPath+redirectUrl+"?userId="+userId+"&developId="+developId+"&openId="+openId+"&serviceCode="+serviceCode;
	    	        		}
	    	        		//福利商城
	    	        		else if(status!=null && "1"==status && stResult != '0'&& stResult!=""){
	    	        			var redirectUrl=$("#redirectUrl").val();
	    	        			var urlKey=$("#urlKey").val();
	    	        			window.location.href=contextPath+redirectUrl+"?userId="+userId+"&developId="+developId+"&openId="+openId+"&urlKey="+urlKey;
	    	        		}
	    	        		//C端商城
	    	        		else if(stResult == '0'){
	    	        			window.location.href=contextPath+"/wechat/sikpMall/sikpMallCterminal"+"?userId="+userId+"&developId="+developId+"&openId="+openId+"&urlKey="+shopUrlKey+"&referer="+referer+"&mECode="+mECode+"&refererId="+refererId;
	    	        		}
	    	        		else if(redirectUrl!=null && (redirectUrl.indexOf("ChannelInterop")!=-1 || redirectUrl.indexOf("neigouFirstJump")!=-1)){//内购网
	    	        			//window.location.href=contextPath+redirectUrl+"?userId="+userId+"&developId="+developId+"&openId="+openId;
	    	        			window.location.href=contextPath+"/wechat/neigou/neigouFirstJump?userId="+userId+"&developId="+developId+"&openId="+openId;
	    	        			
	    	        		}
	    	        		else{
	    	        			var url = contextPath+"/wechat/appUser/newmine?openId="+openId+"&organizationId="+organizationId+"&developId="+developId+"&userId="+userId;
//	    	        			var url=contextPath+"/wechat/appUser/userInfo?userId="+userId+"&developId="+developId+"&openId="+openId+"&rdm="+Math.random();
//	    	        			var url=contextPath+"/wechat/release/mine?userId="+userId+"&developId="+developId+"&openId="+openId+"&rdm="+Math.random();
	    	        			window.location.href=url;
	    	        		}
	    	        	}
	    	        	else{
	    	        		alert(result.resultDesc);
	    	        	}
	    	        },
	    	        error: function(){
	    	        	alert("请检查您的网络，通信超时.");
	    	        }
	    	    });	
		    }
	};
	login.init();
})
/*$(document).ready(function(){
    
     * js手机号码正则校验
     * value -- 获取手机号
     
    function checkTel(value){
        var isMob=/^(?:13\d|15\d|18\d|17\d|14\d)-?\d{5}(\d{3}|\*{3})$/; //手机号码
		if(!(isMob.test(value))){
			 return false;
		}
		else{
			 return true;
		}
    }

    $(".logintel").blur(function(event) {
        var _this = $(this);
        var val = _this.val();
        if(!val){
        	$("#tel").addClass("tel-wrong");
        } else {
            var valtest = checkTel(val);  //手机正则校验
            if(!valtest) {
                _this.val("");
                _this.attr({
                    "placeholder": "请输入正确的手机号码"
                });
                $("#tel").addClass("tel-wrong");
            } else {
            	$("#tel").removeClass("tel-wrong");
            }
        }
    });
    
    $(".loginpsw").blur(function(event) {
        var _this = $(this);
        var veriVal = $.trim(_this.val());
        if(!veriVal){
           _this.val("");
           $("#pws").addClass("veri-wrong");
        } else {
           $("#pws").removeClass("veri-wrong");
        }
    });
    
    
    //点击获取验证码
    $(".obtain").click(function(event){
    	var telVal  = $(".logintel").val();
    	if(!telVal) {
    		$("#tel").addClass("tel-wrong");
        }
    	else{
    		time(this);
    		var contextPath=$("#contextPath").val();
    		$.ajax({
    	    	url: contextPath+"/wechat/sms/getAuthCode",
    	    	data: {userMobile:telVal},
    	        type: "POST",
    	        dataType: "json",
    	        success: function(json){
    	        },
    	        error: function(){
    	        }
    	    });	
    	}
    });
    
    // 点击确认提交
    $(".login-btn").click(function(event){
        event.preventDefault();
        var telVal  = $(".logintel").val();
        var veriVal = $(".loginpsw").val();
        if(!telVal) {
        	$("#tel").addClass("tel-wrong");
        } else if(!veriVal) {
        	$("#
").addClass("veri-wrong");
        } else {
        	var contextPath=$("#contextPath").val();
        	var openId=$("#openId").val();
        	var developId=$("#developId").val();
        	var refererId=$("#refererId").val();
        	var serviceCode=$("#serviceCode").val();
        	var codes=$("#codes").val();
        	var st=$("#st").val();
        	var mECode=$("#mECode").val();
        	$("#tcc").show();
        	$.ajax({
    	    	url: contextPath+"/wechat/appUser/submitphone",
    	    	data: {openId:openId,developId:developId,authCode:veriVal,userMobile:telVal,st:st,fromType:$("#fromType").val(),codes:codes},
    	        type: "POST",
    	        dataType: "json",
    	        success: function(result){
    	        	if("1"==result.result&&"200"==result.resultCode){
    	        		var userId=result.returnDTO.userId;
    	        		var status=result.returnDTO.status;
    	        		var stResult=result.returnDTO.st;
    	        		var shopUrlKey=result.returnDTO.shopUrlKey;
    	        		var fromType=result.returnDTO.fromType; //来源于哪个入口
    	        		var redirectUrl=$("#redirectUrl").val();
    	        		
    	        		if("index"==fromType){
    	        			var redirectUrl=$("#redirectUrl").val();
    	        			window.location.href=contextPath+redirectUrl+"?userId="+userId+"&developId="+developId+"&openId="+openId+"&serviceCode="+serviceCode;
    	        		}
    	        		//福利商城
    	        		else if(status!=null && "1"==status && stResult != '0'&& stResult!=""){
    	        			var redirectUrl=$("#redirectUrl").val();
    	        			var urlKey=$("#urlKey").val();
    	        			window.location.href=contextPath+redirectUrl+"?userId="+userId+"&developId="+developId+"&openId="+openId+"&urlKey="+urlKey;
    	        		}
    	        		//C端商城
    	        		else if(stResult == '0'){
    	        			window.location.href=contextPath+"/wechat/sikpMall/sikpMallCterminal"+"?userId="+userId+"&developId="+developId+"&openId="+openId+"&urlKey="+shopUrlKey+"&referer="+referer+"&mECode="+mECode+"&refererId="+refererId;
    	        		}
    	        		else if(redirectUrl!=null && (redirectUrl.indexOf("ChannelInterop")!=-1 || redirectUrl.indexOf("neigouFirstJump")!=-1)){//内购网
    	        			//window.location.href=contextPath+redirectUrl+"?userId="+userId+"&developId="+developId+"&openId="+openId;
    	        			window.location.href=contextPath+"/wechat/neigou/neigouFirstJump?userId="+userId+"&developId="+developId+"&openId="+openId;
    	        			
    	        		}
    	        		else{
    	        			var url=contextPath+"/wechat/appUser/userInfo?userId="+userId+"&developId="+developId+"&openId="+openId+"&rdm="+Math.random();
    	        			window.location.href=url;
    	        		}
    	        	}
    	        	else{
    	        		alert(result.resultDesc);
    	        		$("#tcc").hide();
    	        	}
    	        },
    	        error: function(){
    	        	alert("请检查您的网络，通信超时.");
    	        	$("#tcc").hide();
    	        }
    	    });	
        }
    });
});

var wait=60;
function time(o) {
	if (wait == 0) {
		o.removeAttribute("disabled");			
		o.value="获取验证码";
		wait = 60;
	} else {
		o.setAttribute("disabled", true);
		o.value="重新发送(" + wait + ")";
		wait--;
		setTimeout(function() {
			time(o)
		},
		1000)
	}
}

function imgchange() {
    $("#tel").addClass("tel-wrong");
    $("#pws").addClass("veri-wrong");
    var contextPath=$("#contextPath").val();
	var login = $("#login").val();
	$("#imgss").attr("src",contextPath+"/wechat/code/imageCode?superId=" + login + "&date="+ Math.random() + 100);
}*/