//页面逻辑
 (function(){        
//从url上截取参数（从第一个页面调过来的参数）（只能截取英文和数字）（需要用到一个插件）
var $phone = getQueryStringByName('phone');
//给推荐人手机号赋值
$("#urlPhone").val($phone);
var urlPhoneVal = $("#urlPhone").val();
var key = hex_sha1("YOUKA_#SECURITY!_KEY");
////3DES解密
var des3jiemi = DES3.decrypt(key,urlPhoneVal);
//console.info(key);
//console.info(des3jiemi);
/*公共变量*/
 var date = Date.parse(new Date());
 //加密
hash = hex_md5(""+'Y0UKA@2018'+date);
var validCode = true;//验证码标记
var time = 60;//倒计时
var telephoneReg = new RegExp(/^(1[3,4,5,7,8]{1}[\d]{9})/) ;   
//验证手机号
function validPhone(ele){
   var flag = true;
    if(!(telephoneReg.test(ele.val()))){
       $("#msgError").removeClass("none");
       $("#msgFail").html("您输入的手机号码格式有误，请重新输入");
       flag = false;
   }else{
       flag = true;
   }
   return flag;
}
  //点击获取手机验证码
 $(".wrap").on("click","#captchaButton",function(){
   var flag =true;
   var phone = $("#phone").val();
   if(!phone){
       $("#msgError").removeClass("none");
       $("#msgFail").html("请输入手机号");
       flag = false;
   }else{
       flag = validPhone($("#phone"));
   }
   var code = $(this);
   if(flag){
       var content = {
           subMerchantCode:'',
           timestamp:date,
           sign:hash,
           mobile:phone,
           function:'1'
       }
       if(validCode){
           validCode = false;
            $.ajax({
                 url: 'http://youkapay.com:8080/youka-api/api/user/getIdentifyCode',
                 data: content,
                 dataType: 'json',
                 type: 'POST',
                 success:function(data){
                     console.info("验证码")
                    console.info(data)
                   if(data.status == '2000'){
                       code.css("background","#ddd");
                       var t = setInterval(function() {
                           time--;
                           code.html(time + "s");
                           if (time == 0) {
                               clearInterval(t);
                               code.html("重新获取");
                               time = 60;
                               validCode = true;
                               code.css("background","#FF4E0C");
                           }
                       }, 1000);
                   }
                 },
                error: function(data){  
                    
                } 

            });
   }
   }
});
       
//点击提交按钮验证信息
$("#submit").on("click",function(){
    var phone = $("#phone").val();
    var password = $("#password").val();
    var yzm = $("#yzm").val();
    var tjPhone = des3jiemi;
    console.info(tjPhone);
    if(phone == ""){
        $("#msgError").removeClass("none");
        $("#msgFail").html("请输入手机号");
        return false;
    }else if(!telephoneReg.test(phone)){
        $("#msgError").removeClass("none");
        $("#msgFail").html("您输入的手机号码格式有误，请重新输入");
    }else if(password == ""){
        $("#msgError").removeClass("none");
        $("#msgFail").html("请输入密码");
        return false;
    }else if(password.length>12 || password.length<6){
        $("#msgError").removeClass("none");
        $("#msgFail").html("请设置6-12位长度密码");
        return false;
    }else if(yzm == ""){
        $("#msgError").removeClass("none");
        $("#msgFail").html("请输入验证码");
        return false;
    }else{
        var content = {
           subMerchantCode:'',
           timestamp:date,
           sign:hash,
           mobile:phone,
           newPassword:password,
           identityCode:yzm,
           function:'1',
           recommendedMobile:tjPhone
       }
        console.info("提交")
        console.info(content)
        $.ajax({
             type: 'POST',
             url: 'http://youkapay.com:8080/youka-api/api/user/signUp',
             data: content,
             dataType: 'json',
             success:function(data){
                console.info(data)
                if(data.status == '2000'){
                   //跳转到市场
//                        window.location = "apk下载地址";  
                    var user = navigator.userAgent;
                    if (user.match(/(iphone os)/i)) {
                        console.log("isphone");
//                            window.location.href='https://appsto.re/cn/KG6P2.i'
                    }else if(user.match(/ipad/i)){
                        console.log("isipad");
                    }else if(user.match(/(midp|ucweb|android|windows ce|windows mobile)/i)){
                        console.log("android");
//                            window.location.href='http://tstatics.iyuwan.com/files/apk/201605/Iyuwan.apk'
                    };
               }else{
                   $("#registError").removeClass("none");
                   $("#registFail").html(data.message);

               }
             },
        });
    }
});
     
 //关闭弹窗
 $("#closeBtn").on("click",function(){
     $(".regist-alert").addClass("none");
 })
 $("#msgCloseBtn").on("click",function(){
     $(".regist-alert-msg").addClass("none");
 })
     
 })();