diff --git a/js/main.js b/js/main.js new file mode 100644 index 0000000..f417269 --- /dev/null +++ b/js/main.js @@ -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'; +} \ No newline at end of file