小程序加记住密码(3天)

This commit is contained in:
majintao
2024-12-23 10:26:39 +08:00
parent aa6ec4e50a
commit 0e723d4bba
@@ -88,6 +88,47 @@
}
return true;
}
// 设置 Cookie 函数
function setLoginCookie(username, token) {
var expirationDate = new Date();
expirationDate.setTime(expirationDate.getTime() + (3 * 24 * 60 * 60 * 1000)); // 3天后过期
var expires = "expires=" + expirationDate.toUTCString();
// 将用户信息(用户名和 token)存入 Cookie
document.cookie = "login_username=" + encodeURIComponent(username) + ";" + expires + ";path=/";
document.cookie = "login_token=" + encodeURIComponent(token) + ";" + expires + ";path=/";
}
// 获取 Cookie 中的值
function getCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') c = c.substring(1, c.length);
if (c.indexOf(nameEQ) == 0) return decodeURIComponent(c.substring(nameEQ.length, c.length));
}
return null;
}
// 检查登录状态
function checkLoginStatus() {
var username = getCookie("login_username");
var token = getCookie("login_token");
if (username && token) {
// 自动登录
console.log("自动登录用户:" + username);
window.location.href = "reqCityChoicePage?type=gb";
// 可以在这里用 token 验证用户身份,或者直接跳转到主页面
// 比如:window.location.href = 'dashboard.html';
} else {
console.log("没有登录信息,跳转到登录页面");
// 如果没有登录信息,跳转到登录页面
// window.location.href = 'login.html';
// window.location.href = "aiLoginPage";
}
}
$(".login_btn").click(function(){
var username = $("#username").val();
@@ -115,6 +156,8 @@
});
}
});
// 在页面加载时检查登录状态
checkLoginStatus();
});
</script>
</body>