feat: 首页登录状态检测与退出功能
This commit is contained in:
56
js/main.js
Normal file
56
js/main.js
Normal file
@@ -0,0 +1,56 @@
|
||||
/**
|
||||
* 主页JavaScript
|
||||
*/
|
||||
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
// 检查用户是否已登录
|
||||
checkLoginStatus();
|
||||
|
||||
// 初始化页面事件
|
||||
initEvents();
|
||||
});
|
||||
|
||||
/**
|
||||
* 检查用户登录状态
|
||||
*/
|
||||
function checkLoginStatus() {
|
||||
const isLoggedIn = localStorage.getItem('isLoggedIn');
|
||||
|
||||
if (!isLoggedIn) {
|
||||
// 如果未登录,重定向到登录页面
|
||||
window.location.href = 'login.html';
|
||||
return;
|
||||
}
|
||||
|
||||
// 获取并显示用户名
|
||||
const username = localStorage.getItem('username');
|
||||
if (username) {
|
||||
document.getElementById('username').textContent = username;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化页面事件
|
||||
*/
|
||||
function initEvents() {
|
||||
// 退出登录
|
||||
const logoutBtn = document.getElementById('logout');
|
||||
if (logoutBtn) {
|
||||
logoutBtn.addEventListener('click', function(e) {
|
||||
e.preventDefault();
|
||||
logout();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 退出登录
|
||||
*/
|
||||
function logout() {
|
||||
// 清除本地存储中的登录信息
|
||||
localStorage.removeItem('isLoggedIn');
|
||||
localStorage.removeItem('username');
|
||||
|
||||
// 重定向到登录页面
|
||||
window.location.href = 'login.html';
|
||||
}
|
||||
Reference in New Issue
Block a user