2017-09-06 15 views
0

私は、Webスタックとphonegapでパッケージを使用してモバイルアプリケーションを開発したいと思います。 私のindex.htmlページにはログインフォームが含まれています。私はjs関数を持っていますcheckLoggedIn(); は、ユーザーがlocalStorage変数を検索してログインしているかどうかを確認します。Phonegap - 自動的にページにリダイレクト

チャレンジ:私はindex.htmlが自動的member.html checkLoggedIn()かの戻りtrueにリダイレクトします。それ以外の場合はリダイレクトされず、ちょうどindex.htmlにとどまります。つまり、checkLoggedIn()は、ロード時にindex.htmlで実行されます。
これを達成するためにどのような種類のイベントを発生させるのか、どのように発射するのかはわかりません。何について

+0

を維持するために必要なすべてのようにuはこれまで、ユーザーがアプリを閉じて、それがページ内右にリダイレクトする必要があり、再びそれを開く自動ログイン機能が必要ですか? – Madpop

+0

そういうもの。 – bodesam

答えて

0

は、以下の両方のAndroidとiOS

は、ユーザー名とパスワードを入力した後、ログインボタンのコードであるため、この作品を願って機能

の下にこれを使用する

index.htmlを使用onloadハンドラのfillpasswordで、この後の
$('#login').click(function() { 
    var userName = $('#Username').val(); 
    var password = $('#password').val(); 

    if (userName == "" || password == "") { 
     window.plugins.toast.showLongBottom("Please enter a valid data"); 
     return false; 
    } 

     var options = { dimBackground: true }; 
    SpinnerPlugin.activityStart("Loading...", options); 

    $.ajax({ 
     type: 'GET', 
     url: xxxxxx.xxxx.xxxxx, 
     data: "uid=" + userName + "&pwd=" + password + "", 

     success: function (resp) { 
      SpinnerPlugin.activityStop(); 
      if (resp.status != 0) { 
       if (resp.RoleId == 1) { 

        mash.Session.getInstance().set({ 
         userId: resp.sno, 
         userName: resp.Name, 

        }); 
        var session = mash.Session.getInstance().get(); 
        window.open('Add.html', '_self', 'location=no'); 

        // Create session. 
        var today = new Date(); 
        var expirationDate = new Date(); 
        expirationDate.setTime(today.getTime() mash.Settings.sessionTimeoutInMSec); 
       } 
       else { 
        SpinnerPlugin.activityStop(); 
        mash.Session.getInstance().set({ 

         userId: resp.sno, 
         userName: resp.Name, 

        }); 
        var session = mash.Session.getInstance().get(); 

        var username = userName; 
        var password = password; 

        window.localStorage.setItem("uname", resp.Name); 
        window.localStorage.setItem("pass", password); 
        window.localStorage.setItem("sno", resp.sno); 

        window.localStorage.setItem("RoleId", resp.RoleId); 


        window.open('Main.html', '_self', 'location=no'); 
        //window.plugins.toast.showLongBottom("Login Successfull"); 
        // Create session. 
        var today = new Date(); 
        var expirationDate = new Date(); 
        expirationDate.setTime(today.getTime() + mash.Settings.sessionTimeoutInMSec); 

       } 

      } 
      else { 

       SpinnerPlugin.activityStop(); 
       window.plugins.toast.showLongBottom("Please Enter valid Username and password"); 
       SpinnerPlugin.activityStop(); 

      } 

     }, 
     error: function (e) { 
      SpinnerPlugin.activityStop(); 
      window.plugins.toast.showLongBottom("Invalid Data"); 
      SpinnerPlugin.activityStop(); 
     } 
    }); 


}); 

()

function fillpassword() { 

    if (window.localStorage.getItem("uname") != 0) { 


     mash.Session.getInstance().set({ 

      userId: window.localStorage.getItem("sno"), 
      userName: window.localStorage.getItem("uname"), 


     }); 
     if (window.localStorage.getItem("RoleId") != 1) { 

      document.getElementById('Username').value = window.localStorage.getItem("uname"); 
      document.getElementById('password').value = window.localStorage.getItem("pass"); 

      window.open('Main.html', '_self', 'location=no'); 
     } 
    } 
    else { 
     //alert("Yes") 

    } 
} 

上記のコードの作品とuはセッション

+0

onloadハンドラはどこですか – bodesam

+0

@bodesam fillpassword()は私のonloadハンドラです。アプリケーションを閉じて再度開くと、直接ログインを求められません。別のページにリダイレクトされます – Madpop

+0

ページロード時に自動的にその機能が実行される方法 – bodesam

1

:これは、自動ログイン機能のための私の作業コードである

if(checkLoggedIn()) window.location = "member.html"; 
関連する問題