Ajax 登录控件(三)

-------------上一篇文章中的ajaxLogin.js中有两处BUG

f
           function ajaxLogin(lgoinViewId,loginCtrlId,lgUserName,lgUserPwd,lgCmd,loginStatusId,loginStatusName,loginOutId,loginRemberMe){
             
                 this.loginViewId=lgoinViewId ;

                 this.loginControlId=loginCtrlId ;
                 this.loginUserName=lgUserName ;
                 this.loginPassword=lgUserPwd ;
                 this.loginButton=lgCmd ;
             
                 this.loginStatusId=loginStatusId ;
               
                 this.loginStatusName=loginStatusName ;
                 this.loginOutButton=loginOutId ;
         
                 this.loginRemberMeId=loginRemberMe;

                 this.timerId=0;


                 var cTh=this ;
                
                 if(typeof ajaxLogin.__RegisteredView =="undefined"){
                       ajaxLogin.__RegisteredView=new Array ();
                 }
                 ajaxLogin.__RegisteredView.push(this);

                 //防止多次发送 chk.aspx的请求
                 if (typeof ajaxLogin.__IsChecked=="undefined"){
                      ajaxLogin.__IsChecked=0;
                }


                 this.init=function(){
                        var http=false ;
                        if(typeof ActiveXObject !="undefined"){
                            try
                            {
                                http=new ActiveXObject("Msxml2.XMLHTTP");
                            }
                            catch(e)
                            {
                                try
                                {
                                    http=new ActiveXObject("Microsoft.XMLHTTP");
                                }
                                catch(E)
                                {
                                    http=false ;
                                }
                            }
                        } else if(XMLHttpRequest ){
                               try
                               {
                                   http=new XMLHttpRequest();
                               }
                               catch(e)
                               {
                                  http=false ;
                               }
                        }
                        if(http)
                        {
                          
                            $Event(cTh.loginButton,"click",cTh.loginClick );
                            $Event(cTh.loginOutButton ,"click",cTh.loginOutClick );
                            $Event(cTh.loginUserName ,"keydown",cTh.nameKeyDown);
                            $Event(cTh.loginPassword ,"keydown",cTh.pwdKeyDown);
                        }
                        return http ;
                 }
                 this.showIn=function(){
                        $(cTh.loginControlId).style.display="none";
                        $(cTh.loginStatusId).style.display="";
                 }
                 this.showOut=function(){
               
                        $(cTh.loginStatusId).style.display="none";
                        $(cTh.loginControlId).style.display="";
                 }
                 this.clear=function(){
                     $(cTh.loginUserName ).value="";
                     $(cTh.loginPassword ).value="";
                     try
                     {
                        $(cTh.loginRemberMeId ).checked=false ;
                     }
                     catch(e)
                     {
                     }
                 }
               
                 this.loginOut=function(onCompleted,onRunning,onError){
                         var http=cTh.init();
                         if(!http)
                          return ;
                         http.open("GET","LoginOut.aspx",true);
                         http.onreadystatechange=function(){
                                if(http.readyState==4){
                                     if(http.status==200){
                                        onCompleted();
                                     } else {
                                        onError();
                                     }
                                } else {
                                     onRunning();
                                }
                         }
                         http.send(null);
                 }
               
                 this.loginOutComplected=function(){
                         var   regView =ajaxLogin.__RegisteredView;
                         for(var i=0;i<regView.length;i++)
                         {
                            regView[i].showOut ();
                         }
//这个是为了防止同一用户在不同的机子上同时登录用的。
                        if(cTh.timerId!=0){
                                window.clearInterval(cTh.timerId);
                        }
                   

                 }
                 this.loginOutRunning=function(){
                 }
                 this.loginOutErr=function (){
                 }
                 //单击事件的处理
                 this.loginClick=function(evt){
              
                        var name=$F(cTh.loginUserName );
                        var pwd=$F(cTh.loginPassword );
                       
                        name=name.trim();
                        if(name ==""){
                              alert("用户名不能为空");
                              $(cTh.loginUserName).focus();
                              if(window.event){
                                    window.event.returnValue=false ;
                              } else {
                                    evt.preventDefault();
                              }
                              return ;
                        }
                        pwd=pwd.trim();
                        if(pwd ==""){
                             alert("密码不能为空");
                             $(cTh.loginPassword ).focus();
                             if(window.event){
                                    window.event.returnValue=false ;
                              } else {
                                    evt.preventDefault();
                              }
                              return ;
                        }
                    
                        var rm="0"
                        try
                        {
                          rm=$(cTh.loginRemberMeId ).checked   ?  "1":"0";
                        }
                        catch(e)
                        {
                        }
                        cTh.login(name ,pwd,rm,cTh.onLoginCompleted ,cTh.onHttpStatusError ,cTh.onLoginRunning);
                        if(window.event){
                             window.event.returnValue=false ;
                        } else {
                            evt.preventDefault();
                        }
                 }
                 this.loginOutClick=function(evt){
                        cTh.loginOut(cTh.loginOutComplected ,cTh.loginOutRunning ,cTh.loginOutErr);
                        if(window.event){
                             window.event.returnValue=false ;
                        }   else {
                            evt.preventDefault();
                        }
                 }
                 //keydown事件的处理
                 this.nameKeyDown=function (evt){
                       var srcObject;
                       if(window.event){
                               srcObject =event.srcElement;
                               if(event.keyCode==13 ){
                                   $(cTh.loginPassword ).focus();
                                   event.returnValue=false ;
                               }
                       } else {
                               srcObject =evt.target;
                               if(evt.keyCode==13){
                                   $(cTh.loginPassword ).focus();
                                   evt.returnValue=false ;
                               }
                       }
                     
                 }
                 this.pwdKeyDown=function(evt){
                       var srcObject;
                       if(window.event){
                               srcObject =event.srcElement;
                               if(event.keyCode==13 ){
                                   $(cTh.loginButton).click();
                                   event.returnValue=false ;
                               }
                       } else {
                               srcObject =evt.target;
                               if(evt.keyCode==13){
                                   $(cTh.loginButton ).click();
                                   evt.returnValue=false ;
                               }
                       }
                 }
                
              
                 this.login=function(userName,passwrod,remberMe,onCompletedCallback,onErrorCallback,onRunningCallback){
                       var http= cTh.init();
                       if(!http)
                        return
                       http.open("POST","Login.aspx",true );
                       http.onreadystatechange=function(){
                            var state=http.readyState;
                            if(http.readyState==4){
                               if(http.status==200){
                                    onCompletedCallback(http.responseText);
                               } else {
                                    onErrorCallback(http.status);      
                               }
                            } else {
                               if(onRunningCallback ){
                                   onRunningCallback(state);
                               }
                            }
                       }
                       var params="UserName="+escape(userName)+"&Password="+ escape (passwrod )+"&RememberMe="+remberMe;
                        http.send(params);
                 }
                 this.onLoginRunning=function (state){
                 }
                 this.onLoginCompleted=function(data){
                 debugger
                      var msg=new messageData(data);
                      var code=msg.msgCode ();
                      debugger
                      var desc=msg.msgDesc ();
                       switch(code )
                      {
                             case 0:
                               var regView=ajaxLogin.__RegisteredView;
                               for(var i=0;i<regView.length;i++){
                                     regView[i].showIn();
                                     $Text(regView[i].loginStatusName,desc );
                                     regView[i].clear();
                               }
                              
cTh.timerId=window.setInterval(cTh.notifyServer,2*60*1000);
                               break ;
                             case 1:
                             case 2:
                             case 3:
                             case 4:
                             case 5:
                                alert(desc);
                                break;
                             default :
                                alert("浏览器脚本错误");
                                break ;
                              
                      }
                 }

                this.notifyServer=function(){
                   
                       var http=cTh.init();
                       if(!http) return;
                       http.open("POST","ImOnLine.aspx",false);
                       http.send(null);
                      
           }

                 this.isOnline=function(onCompletedCallback,onErrorCallback){
                      var http=cTh.init ();
                      if(!http )
                         return false ;
                  
    if(ajaxLogin.__IsChecked==1)                      
                          return;  
                     
ajaxLogin.__IsChecked=1;

                      http.open("GET","Chk.aspx",true );
                      http.onreadystatechange=function(){
                          var state=http.readyState;
                          if(http.readyState==4){
                             if(http.status==200){
                                onCompletedCallback(http.responseText);
                             } else {
                                 onErrorCallback(http.status);
                             }
                          }
                      }
                      http.send(null);
                 }
                 this.onCheckUserCompleted=function(data){
                        var msg=new messageData(data );
                        var code=msg.msgCode ();
                        var regView=ajaxLogin.__RegisteredView;
                        switch(code )
                        {
                             case 0:
                                 for(var i=0;i<regView.length;i++)
                                 {
                                    regView[i].showIn ();
                                    $Text(regView[i].loginStatusName ,msg.msgDesc ());
                                 }
                                 cTh.timerId=window.setInterval(cTh.notifyServer,2*60*1000);
                                break;
                             case 1:
                                 for(var i=0;i<regView.length;i++)
                                 {
                                    regView[i].showOut ();
                                    $Text(regView[i].loginStatusName ,"");
                                 }
                                break;
                             default:
                                break;
                        }
                 }
                 this.onHttpStatusError=function(status){
                       alert("服务器错误:"+status );
                 }
           }

--------------------
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值