【问题标题】:Phonegap - Automatically redirecting to a pagePhonegap - 自动重定向到页面
【发布时间】:2017-09-06 11:42:48
【问题描述】:

我想使用 Web 堆栈和带有 phonegap 的软件包开发移动应用程序。 我的 index.html 页面将包含一个登录表单。我有一个js函数checkLoggedIn(); 通过查找 localStorage 变量来检查用户是否已登录。

挑战:如果checkLoggedIn(),我希望index.html 自动重定向到member.html 返回true;否则它不会重定向,只会停留在index.html,这意味着checkLoggedIn() 将在index.html 上运行。
我不知道要触发什么样的事件或如何触发它来实现这一目标。

【问题讨论】:

  • 所以你需要一个自动登录功能,当用户关闭应用程序并再次打开它时,它必须重定向到内页对吗?
  • 类似的东西。

标签: javascript jquery cordova mobile phonegap


【解决方案1】:

怎么样:

if(checkLoggedIn()) window.location = "member.html";

【讨论】:

    【解决方案2】:

    这是我的自动登录功能的工作代码,希望这适用于 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 handler 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")
    
        }
    }
    

    上面的代码有效,你只需要维护一个会话

    【讨论】:

    • onload 处理程序在哪里
    • @bodesam fillpassword() 是我的加载处理程序。当我关闭我的应用程序并再次重新打开时,它不会直接要求登录我将重定向到另一个给定页面
    • 如何让该功能在页面加载时自动运行
    • @bodesam 首先,我将用户名和密码值保存在会话中,然后保存到本地存储中。在下一步中,用户终止应用程序,然后他再次打开它,然后我们从本地存储中获取 uname 和密码值并自动记录页面,我们将使用
    • 这就是我要找的。你确定这行得通吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-05
    • 1970-01-01
    • 2012-10-01
    • 1970-01-01
    • 2015-03-09
    • 2017-09-20
    • 1970-01-01
    相关资源
    最近更新 更多