From 0aadca89a7c77106cec67be9e42c0ac223d14a85 Mon Sep 17 00:00:00 2001 From: beppeb Date: Wed, 16 Jul 2025 03:27:28 +0000 Subject: [PATCH] =?UTF-8?q?feat:=20=E9=A6=96=E9=A1=B5=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E7=8A=B6=E6=80=81=E6=A3=80=E6=B5=8B=E4=B8=8E=E9=80=80=E5=87=BA?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- js/main.js | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 js/main.js 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