码迷,mamicode.com
首页 > Web开发 > 详细

passport.js

时间:2015-04-02 18:09:06      阅读:688      评论:0      收藏:0      [点我收藏+]

标签:

$(function(){

    function isPlaceholder(){  
        var input = document.createElement(input);  
        return placeholder in input;  
    }        
    if(!isPlaceholder()){  
        $("input").not("input[type=‘password‘]").each(//把input绑定事件 排除password框  
            function(){  
                if($(this).val()=="" && $(this).attr("placeholder")!=""){  
                    $(this).val($(this).attr("placeholder"));  
                    $(this).focus(function(){  
                        if($(this).val()==$(this).attr("placeholder")) $(this).val("");  
                    });  
                    $(this).blur(function(){  
                        if($(this).val()=="") $(this).val($(this).attr("placeholder"));  
                    });  
                }  
        });  
        //对password框的特殊处理1.创建一个text框 2获取焦点和失去焦点的时候切换  
        function removeInput(ele){
            var ele = $(.+ele);
            var pwdField    = ele.find("input[type=password]");  
            var pwdVal      = pwdField.attr(placeholder);  
            pwdField.after(<input class="input pwdPlaceholder" type="text" value=+pwdVal+ autocomplete="off" />);  
            var pwdPlaceholder = ele.find(.pwdPlaceholder);  
            pwdPlaceholder.show();  
            pwdField.hide();  

            pwdPlaceholder.focus(function(){  
                pwdPlaceholder.hide();  
                pwdField.show();  
                pwdField.focus();  
            });  
              
            pwdField.blur(function(){  
                if(pwdField.val() == ‘‘) {  
                    pwdPlaceholder.show();  
                    pwdField.hide();  
                }  
            }); 
        } 
        removeInput(loginBox);
        removeInput(registerBox);
    }  

    $.getScript(http://static.m1905.cn/passport/js/emailsuggest.js,function(){
        var arr = [ 163.com,
                    126.com,
                    qq.com,
                    yahoo.com.cn,
                    gmail.com,
                    sohu.com,
                    hotmail.com ,
                    sina.com
                  ];
        new $.ui.EmailSuggest( #mail_reg, {
            data : arr
        });
        new $.ui.EmailSuggest( #mail_sug, {
            data : arr
        });
    })

    var validateAPI = http://passport.1905.com/v2/api/,
        passBoxTag = $(<span class="msg-box"><i class="icons pass"><em class="arr"></em></i></span>),
        failBoxTag = $(.registerBox .error);
    var InterValObj; //timer变量,控制时间
    var count = 60; //间隔函数,1秒执行
    var curCount;//当前剩余秒数

    $.register = {};
    $.register = { 

        rEmail : /^\w+((\.\w+)|(-\w+))*\@[a-zA-Z0-9]+((\.|-)[a-zA-Z0-9]+)*\.[a-zA-Z0-9]+$/,
        rPhone : /^(^1[3|8]\d{9}$)|(^15[012356789]\d{8}$)|(^14[57]\d{8}$)$/,
        rUsername : /^[\.a-zA-Z0-9_-]{4,30}$/,
        rsmscode : /\d{4}/,
        trim : /\s*/,
        rPassword : {
            digital: /\d+/,
            lowercase: /[a-z]+/,
            uppercase: /[A-Z]+/,
            symbols: /[_!@#\$%\^&\*\(\)\\\|\/\?\.\<\>"\{\}\[\]=\-\~\,\;\:]+/,
            password: /^[a-zA-Z0-9_!@#\$%\^&\*\(\)\\\|\/\?\.\<\>"\{\}\[\]=\-\~\,\;\:\s]+$/,
            mobile_verify: /^\d$/
        },            
        checkEmailorPhone : function (){  //验证邮箱手机号
            var flag = "";
            $(.registerBox .error).html("");
            $(.registerBox .emailormobile).next(.msg-box).remove();
            if($(.ecope_emailsuggest li.current).length > 0){
                EmailorPhoneValue = $(.ecope_emailsuggest li.current).text();
                $(.registerBox .emailormobile).val(EmailorPhoneValue);
                $(.ecope_emailsuggest li.current).removeClass(current);
            }else{
                EmailorPhoneValue = $(.registerBox .emailormobile).val();
            }         
            $(.registerBox .emailormobile).removeAttr(data-type);
            if(!$.register.rEmail.test(EmailorPhoneValue) && !$.register.rPhone.test(EmailorPhoneValue)){
                if(/\s+/.test(EmailorPhoneValue)){
                    failBoxTag.html(<em class="icons fail"></em>输入框存在空格,请您重新输入);
                }else{
                    failBoxTag.html(<em class="icons fail"></em>请输入正确的手机号或邮箱);
                    flag = 0;
                }
            }else if($.register.rPhone.test(EmailorPhoneValue)){
                $.ajax({
                    url:validateAPI,
                    data : {m : user,a : checkEmailOrmobile , format : json,emailormobile : EmailorPhoneValue},
                    dataType: jsonp,
                    jsonpCallback : callback,
                    async:false,
                    success:function(data){
                        if(data.status == 200){
                            failBoxTag.html(<em class="icons fail"></em>手机号已经被注册,可<a href="javascript:void(0)" class="quick-login">直接登录</a>);
                            $(.registerBox .password).unbind(blur);
                            $(.registerBox .registerCode).unbind(blur);
                        }else{
                            $(.registerBox .emailormobile).siblings(.msg-box).remove();
                            passBoxTag.appendTo($(.emailormobile).parent(fieldset));
                            $(.ecope_emailsuggest).hide();
                            $(.registerBox .emailormobile).attr(data-type,true);
                            $(#smsCode).slideDown();
                        }
                    }
                });
            }else if($.register.rEmail.test(EmailorPhoneValue)){
                $(#smsCode).slideUp();
                $.ajax({
                    url:validateAPI,
                    data : {m : user,a : checkEmailOrmobile , format : json,emailormobile : EmailorPhoneValue},
                    dataType: jsonp,
                    jsonpCallback : callback,
                    async:false,
                    success:function(data){
                        if(data.status == 200){
                            failBoxTag.html(<em class="icons fail"></em>邮箱已经被注册,可<a href="javascript:void(0)" class="quick-login">直接登录</a>);
                            $(.registerBox .password).unbind(blur);
                            $(.registerBox .registerCode).unbind(blur);
                        }else{
                            $(.registerBox .emailormobile).siblings(.msg-box).remove();
                            passBoxTag.appendTo($(.emailormobile).parent(fieldset));
                            $(.ecope_emailsuggest).hide();
                            $(.registerBox .emailormobile).attr(data-type,true);
                            mobilecodeValue = $(.registerBox .mobilecode).val() == ‘‘;
                        }
                    }
                });           
            }           
        },       
        checkPassword : function(){  //验证密码
            $(.emailormobile).trigger(blur);
            failBoxTag.html("");
            passwordValue = $(.registerBox .password).val();
            $(.registerBox .password).removeAttr(data-type);
            if(passwordValue.length == 0){ 
                failBoxTag.html(<em class="icons fail"></em>密码不能为空);
            }else if(passwordValue.length < 6){
                $(".registerBox .password").trigger(keyup);
                $(.registerBox .password).next(.msg-box).remove();
                failBoxTag.html(<em class="icons fail"></em>输入密码至少6位);
            }else if(passwordValue.length > 16){
                failBoxTag.html(<em class="icons fail"></em>密码不能超过16位);
            }else if(/\s+/.test(passwordValue)){
                failBoxTag.html(<em class="icons fail"></em>输入框存在空格,请您重新输入);
                $(.registerBox .password).next(.msg-box).remove();              
            }else{
                $(.registerBox .password).attr(data-type,true);  
            }   
        },
        checkPassWord_strong : function(){  //检测密码强度
            passwordValue = $(.registerBox .password).val();
            var value = $.trim(passwordValue), b = 0, msg ="";
            switch ($.register.rPassword.digital.test(value) && b++, $.register.rPassword.lowercase.test(value) && b++, $.register.rPassword.uppercase.test(value) && b++, $.register.rPassword.symbols.test(value) && b++, b) {
                case 1:
                    msg = <i class="icons safe week"> 弱 </i>;
                    break;
                case 2:
                    msg = <i class="icons safe normal"> 中 </i>;
                    break;
                case 3:
                    msg = <i class="icons safe strong"> 强 </i>;
                    break;
            } 
            $(.registerBox .password).siblings(.msg-box).remove();
            $(<span class="msg-box">+ msg +</span>).appendTo($(.registerBox .password).parent(fieldset));
            return ‘‘;  
        },
        changeverify : function(imgID){  //刷新图片验证码
            var url = http://passport.1905.com/v2/api/?m=seccodech&a=display&rand=;
            $(#+imgID).attr(src, url  + new Date().getTime());
        }, 
        checkSecCode : function(ele){  //图片验证码
            $(.emailormobile).trigger(blur);
            $(.+ele).find(.error).html("");
            $(.+ele).find(.vcode).next(.msg-box).remove();
            secCodeValue = $(.+ele).find(.vcode).val();
            $(.+ele).find(.vcode).removeAttr(data-type);
            $.ajax({
                url:validateAPI,
                data : {m : user,a : checkseccode , format : json,seccode : secCodeValue},
                dataType: jsonp,
                jsonpCallback : callback,
                success:function(data){
                    if(data.status == 200){
                        $(.+ele).find(.vcode).siblings(.msg-box).remove(); 
                        $(<span class="msg-box"><i class="icons pass"><em class="arr"></em></i></span>).appendTo($(.+ele).find(.vcode).parent(fieldset)); 
                        $(.registerBox .vcode).attr(data-type,true);                     
                    }else{
                        $(.+ele).find(.vcode).siblings(.msg-box).remove(); 
                        $(.+ele).find(.error).html(<em class="icons fail"></em>验证码错误);
                    }
                }
            })
        }, 
        SetRemainTime : function() {
            if (curCount == 0) {                
                window.clearInterval(InterValObj);
                $("#issuedSMS").removeAttr("disabled");
                $("#issuedSMS").val("重新发送验证码");
                code = ""; 
            }
            else {
                curCount--;
                $("#issuedSMS").val(curCount + "秒后重新发送");
            }
        },
        getmobileCode : function() {
            curCount = count;
            if(EmailorPhoneValue != "" && $.register.rPhone.test(EmailorPhoneValue)){
                $("#issuedSMS").attr("disabled", "true");
                $("#issuedSMS").val(curCount + "秒后重新发送");
                InterValObj = window.setInterval($.register.SetRemainTime, 1000); //启动计时器,1秒执行一次
                $.ajax({
                    url:validateAPI,
                    data : {m : acode,a : sendmobile , mobile : EmailorPhoneValue},
                    dataType: jsonp,
                    success:function(data){
                        if(data.status == 200){
                            alert(短信已发送+data.info);
                        }else{
                            alert(data.info);
                        }
                    }
                })
            }else{
                failBoxTag.html(<em class="icons fail"></em>手机号码不能为空);
                $(.emailormobile).trigger(focus);
            }
        },
        checkSmsCode : function(){//短信验证码
            failBoxTag.html("");
            $(.registerBox .mobilecode).next(.msg-box).remove();   
            mobilecodeValue = $(.registerBox .mobilecode).val();  
            $(.registerBox .mobilecode).removeAttr(data-type);       
            if(mobilecodeValue.length == 0){
                failBoxTag.html(<em class="icons fail"></em>手机验证码不能为空);
                return false;
            }else{
                $.ajax({
                    url:validateAPI,
                    data : {m : acode,a : check , mobile : EmailorPhoneValue, acode : mobilecodeValue},
                    dataType: jsonp,
                    success:function(data){
                        if(data.status == 200){
                            $(.registerBox .mobilecode).siblings(.msg-box).remove();
                            passBoxTag.appendTo($(.mobilecode).parent(fieldset));
                            $(.registerBox .mobilecode).attr(data-type,true); 
                        }else{
                            failBoxTag.html(<em class="icons fail"></em>手机验证码错误);
                        }
                    }
                })
            }     
        },  
        checkAgreement : function (){//同意协议
            failBoxTag.html("");
            if (!$(.agree).is(":checked")) {
                $(.registerBox .agree).removeAttr(data-type);
                failBoxTag.html(<em class="icons fail"></em>同意后才能注册);
            }else{
                 $(.registerBox .agree).attr(data-type,true);
             }   
        }
    }//register end
   
    $(document).on(click,.quick-login, .mar-login a, function(){
        $(.tag li).eq(0).addClass(active).siblings(li).removeClass(active);
        $(.loginBox).addClass(active).siblings(.registerBox).removeClass(active);
    }).on(click,#img_reg_code, function(){
        $.register.changeverify(img_reg_code);
    }).on(click,#img_login_code, function(){
        $.register.changeverify(img_login_code);
    }).on(click,.tag li, function(){
        var nIndex = $(this).index();
        $(this).addClass(active).siblings(li).removeClass(active);
        $(.passContent).eq(nIndex).addClass(active).siblings().removeClass(active).find(input).empty();
        $(.nav-header).eq(nIndex).addClass(active).siblings(.nav-header).removeClass(active);
    }).on(click,#issuedSMS,function(){
        $.register.getmobileCode();
    }).on(keydown,.password,function(event){
        if ((event.ctrlKey&&event.which==67) || (event.ctrlKey&&event.which==86)) {
            return false;
        } 
    }).on(keydown,.loginBox .input,function(event){
        if(event.keyCode == 13){
            $(.loginBox .submit).trigger(click);
        }
    }).on(keydown,.registerBox .input,function(event){
        if(event.keyCode == 13){
            $(.registerBox .submit).trigger(click);
        }
    });

    $(".registerBox .emailormobile").blur(function(){
        // var text = $(this).val().toLowerCase();     
        // $(this).val(text);
        $.register.checkEmailorPhone();      
    });
    $(".registerBox .password").keyup(function(){$.register.checkPassWord_strong();})
    $(".registerBox .password").blur(function(){$.register.checkPassword();})  
    $(".registerBox .vcode").blur(function(){$.register.checkSecCode(registerBox);})
    $(".registerBox .mobilecode").blur(function(){$.register.checkSmsCode();})
    $(.registerBox .agree).click(function(){ $.register.checkAgreement();})
    $(".loginBox .vcode").blur(function(){$.register.checkSecCode(loginBox);})

    $(.registerBox .submit).click(function(){  
        var flagTag =0,
            eleLi = $(".registerBox li.normal:visible"),
            eleLilength = eleLi.length;
      
        eleLi.each(function(){
            if($(this).find("input").eq(0).attr("data-type")){
                flagTag++;
            }else{
                $(this).find("input").eq(0).trigger("blur");
                return false;
            }
        });

        if(eleLilength==flagTag){
            $.ajax({
                url:validateAPI,
                data : {m : user,a : register_v2 , format : json, emailormobile : EmailorPhoneValue, acode : mobilecodeValue , password: passwordValue , seccode:secCodeValue },
                dataType: jsonp,
                jsonpCallback : callback,
                success:function(data){
                    if(data.status == 200){                           
                        backUrl = getRequest(callback_url)  || "http://"+window.location.host;
                        top.location.href = backUrl;
                    }
                }
            })
        }                                      
    });
   
    $.login = {};
    $.login = {   
        errorTimes : $(#code_error_max).val() || 0, 
        def_vcode : 请输入验证码,
        rPhone : /^(^1[3|8]\d{9}$)|(^15[012356789]\d{8}$)|(^14[57]\d{8}$)$/,
        rUsername : /^[\.a-zA-Z0-9_-]{4,30}$/,
        rEmail : /^\w+((\.\w+)|(-\w+))*\@[a-zA-Z0-9]+((\.|-)[a-zA-Z0-9]+)*\.[a-zA-Z0-9]+$/,
        err_uname : 请检查通行证格式,
        err_empty : 请填写通行证,
        err_upass : 请输入密码,
        err_vcode : 验证码不能为空,

        init : function(){
            // 验证码
            if( $.login.errorTimes > 3 ) {
                $(.seccode).val( $.login.def_vcode );
                $(.loginBox .vcode).show();
            }     
        },
        // 验证表单提交数据
        checkForm : function(){         
            var value = $(.username).val();
            var upass = $(.loginBox .password).val();
            var vcode = $(#code).val();
            var testVal = value.replace(/[^\x00-\xff]/g, mm),
                unLen = testVal.length;
            if (unLen == 0) {//请填写通行证
                $.login.showMsg( $.login.err_empty );
                return false;
            }
            if (!$.login.rPhone.test(testVal) && !$.login.rEmail.test(testVal) && !$.login.rUsername.test(testVal)) {    //  请检查通行证格式  
                $.login.showMsg( $.login.err_uname );
                return false;
            } else if( upass.length == 0) {//请输入密码
                $.login.showMsg( $.login.err_upass );
                return false;
            } else if( $.login.errorTimes > 3 ) {
                if( vcode == ‘‘ || vcode == $.login.def_vcode ){
                    $.login.showMsg( $.login.err_vcode );
                    return false;
                }
            }
            
            $.login.submit( value, upass, vcode );  
        },
        // 回调函数
        loginDispose : function( data ){
            if( data.synsrc ){
                //同步登陆
                $.ajax({
                    url:data.synsrc,
                    dataType: jsonp,
                    data: {},
                    success: function (data) {                  
                        backUrl = $.login.getRequest(callback_url) || window.location.href.replace(/#.*$/,"");
                        var str = /v2\/\?m=user\&a=login$/;
                        if(str.test(backUrl)){
                            location.href = "http://passport.1905.com";
                        }else{
                            location.href = backUrl;
                        }                 
                    },error:function(){
                        backUrl = $.login.getRequest(callback_url) || window.location.href.replace(/#.*$/,"");
                        location.href = backUrl;
                    }
                });
            }
        },
        //提交
        submit : function( uname, upass , vcode){
            $.login.go( uname, upass, vcode, $.login.loginDispose );
        },
        // showMsg
        showMsg : function( msg ){
            $(.loginBox .error).html(<em class="icons fail"></em>+msg);
        },
        checkUsername : function(){
            if($(.ecope_emailsuggest li.current).length > 0){
                usernameValue = $(.ecope_emailsuggest li.current).text();
                $(.loginBox .username).val(usernameValue);
                $(.ecope_emailsuggest li.current).removeClass(current);
            }else{
                usernameValue = $(.loginBox .username).val();
            } 
            //var usernameValue = $(‘.loginBox .username‘).val();
            $.ajax({
                url:validateAPI,
                type: "GET",
                data : {m : user,a : checkoncheckEmailOrmobileOrusername,login_name : usernameValue},
                dataType: "jsonp",
                success: function(data) {
                    if(data.status == 200){
                        $.login.showMsg(账号不存在);
                        $(.loginBox .username).siblings(.msg-box).remove();
                        return false;
                    }else{
                        $(.loginBox .error).html(‘‘);
                        $(.loginBox .username).parent(fieldset).append(passBoxTag);                              
                    }
                }
            });
        },      
        go : function( username, password, vcode, callback ){//登陆
            var url =  window.location.host.split(".")[0] == "passport" ? "/v2/api/?m=user&a=login" : "http://passport.m1905.com/v2/api/?m=user&a=login";
            $.ajax({
                url: url,
                type: "GET",
                data: {username:username,password:password,seccode:vcode},
                dataType: "jsonp",
                success: function(data) {
                    if(data.status == 200){
                        if(callback){
                            callback(data.data);
                        }
                    }else{
                        //抛出错误
                        $.login.showMsg( data.info ); 
                        //登陆错误次数超过3次则提示输入验证码
                        if(data.data.error_max > 3 && $(.loginsecCode).is(:hidden)){
                            $(.loginsecCode).show();
                        }                                     
                    }
                }
            });
        },
        getRequest : function(name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)","i"),
            r = window.location.search.substr(1).match(reg);
            return (r!=null)?  unescape(r[2]) : null;
        } 
    };
    $(.loginBox .username).blur(function(){
        $.login.checkUsername();
    });
    $(.loginBox .submit).click(function(){
        $.login.checkForm();
    });

    function getRequest(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)","i"),
        r = window.location.search.substr(1).match(reg);
        return (r!=null)?  unescape(r[2]) : null;
    };
    var refer = document.referrer,
        currentUrl = window.location.href.replace(/#.*$/,""),
        backUrl = getRequest(callback_url) || currentUrl || refer || http://www.1905.com/,
        currentUrl = window.location.hash;
    $(document).on(click, [data-login], function() {
        var p = $(this).data(login),
        url = encodeURIComponent( backUrl );      
        p && ( window.location = http://openapi.passport.m1905.com/Login/Authorize/ + p + /?ReturnUrl= + url )
        return false;
    })
})

 

passport.js

标签:

原文地址:http://www.cnblogs.com/jiangtuzi/p/4387290.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!