2017-09-06 61 views
0

我想开发一个使用Web栈和手机包的移动应用程序。 我的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

下面是登录按钮的代码输入用户名和密码后,

$('#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(); 
     } 
    }); 


}); 

在此之后在使用index.html onload处理fillpassword()使用此功能下面

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";