这是自动登录功能我工作的代码希望这同时适用于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需要保持会话
所以u需要一个自动登录功能,其中当过用户关闭应用程序,然后再次打开它重定向到内页吧? – Madpop
就是这样的。 – bodesam