42 lines
995 B
JavaScript
42 lines
995 B
JavaScript
/**
|
|
* 认证和会话管理
|
|
*/
|
|
|
|
// 检查用户是否已登录
|
|
function checkAuth() {
|
|
const user = localStorage.getItem("currentUser");
|
|
if (!user) {
|
|
// 用户未登录,重定向到登录页面
|
|
window.location.href = "/login.html";
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
// 获取当前登录的用户信息
|
|
function getCurrentUser() {
|
|
const user = localStorage.getItem("currentUser");
|
|
return user ? JSON.parse(user) : null;
|
|
}
|
|
|
|
// 保存用户信息(登录时使用)
|
|
function setCurrentUser(userData) {
|
|
localStorage.setItem("currentUser", JSON.stringify(userData));
|
|
}
|
|
|
|
// 注销用户
|
|
function logout() {
|
|
localStorage.removeItem("currentUser");
|
|
window.location.href = "/login.html";
|
|
}
|
|
|
|
// 在页面加载时检查认证(仅在非登录页面使用)
|
|
document.addEventListener("DOMContentLoaded", function () {
|
|
const currentPage = window.location.pathname;
|
|
|
|
// 排除登录页面的检查
|
|
if (!currentPage.includes("login.html")) {
|
|
checkAuth();
|
|
}
|
|
});
|