2025-12-2genxin

This commit is contained in:
2025-12-02 14:58:52 +08:00
parent 4fef65bd93
commit be0954828c
36 changed files with 3352 additions and 1638 deletions

View File

@@ -393,9 +393,9 @@
<a href="#" class="nav-link">监控中心</a>
<a href="#" class="nav-link">数据分析</a>
<a href="#" class="nav-link">帮助文档</a>
<a href="user-center.html" class="nav-link" id="userMenu" style="display: none;">👤 用户中心</a>
<a href="user-center.html" class="nav-link" id="userCenterLink">👤 用户中心</a>
<a href="login.html" class="nav-link" id="loginLink">🔐 登录</a>
<a href="#" class="nav-link" id="logoutLink" style="display: none;" onclick="logout()">🚪 退出</a>
<div id="userMenu" class="user-info"></div>
</nav>
</div>
</header>
@@ -736,11 +736,18 @@ ${article.content.replace(/<[^>]*>/g, '')}
// 登录状态管理
function checkLoginStatus() {
const isLoggedIn = localStorage.getItem('isLoggedIn') === 'true';
const username = localStorage.getItem('username');
// 检查认证数据
const authData = localStorage.getItem('authData') || sessionStorage.getItem('authData');
if (isLoggedIn && username) {
showLoggedInState(username);
if (authData) {
try {
const auth = JSON.parse(authData);
const username = auth.user_info && auth.user_info.username ? auth.user_info.username : '用户';
showLoggedInState(username);
} catch (e) {
console.error('解析登录数据失败:', e);
showLoggedOutState();
}
} else {
showLoggedOutState();
}
@@ -748,38 +755,87 @@ ${article.content.replace(/<[^>]*>/g, '')}
// 显示已登录状态
function showLoggedInState(username) {
document.getElementById('loginLink').style.display = 'none';
document.getElementById('userMenu').style.display = 'block';
document.getElementById('logoutLink').style.display = 'block';
const loginLink = document.getElementById('loginLink');
const userMenu = document.getElementById('userMenu');
// 为用户中心菜单添加点击事件
document.getElementById('userMenu').onclick = function() {
window.location.href = 'user-center.html';
};
if (loginLink) loginLink.style.display = 'none';
if (userMenu) {
userMenu.innerHTML = `
<span style="margin-right: 10px;">👋 ${username}</span>
<button onclick="logout()" style="
background: rgba(255, 255, 255, 0.2);
color: white;
border: 1px solid rgba(255, 255, 255, 0.3);
padding: 6px 12px;
border-radius: 4px;
cursor: pointer;
font-size: 13px;
transition: all 0.3s;
" onmouseover="this.style.background='rgba(255, 255, 255, 0.3)'"
onmouseout="this.style.background='rgba(255, 255, 255, 0.2)'">
退出登录
</button>
`;
userMenu.style.display = 'flex';
}
}
// 显示未登录状态
function showLoggedOutState() {
document.getElementById('loginLink').style.display = 'block';
document.getElementById('userMenu').style.display = 'none';
document.getElementById('logoutLink').style.display = 'none';
const loginLink = document.getElementById('loginLink');
const userMenu = document.getElementById('userMenu');
if (loginLink) loginLink.style.display = 'inline';
if (userMenu) userMenu.innerHTML = '';
}
// 登出功能
function logout() {
if (confirm('确定要退出登录吗?')) {
localStorage.removeItem('isLoggedIn');
localStorage.removeItem('username');
localStorage.removeItem('token');
sessionStorage.removeItem('userSession');
// 获取认证数据
const authData = localStorage.getItem('authData') || sessionStorage.getItem('authData');
let token = '';
showLoggedOutState();
alert('已成功退出登录!');
// 如果在用户中心页面,跳转到首页
if (window.location.pathname.includes('user-center.html')) {
window.location.href = 'frontend.html';
if (authData) {
try {
const auth = JSON.parse(authData);
token = auth.token || '';
} catch (e) {
console.error('解析token失败:', e);
}
}
// 调用后端登出API
fetch('http://localhost:8080/api/user/logout', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': token ? `Bearer ${token}` : ''
}
}).then(response => {
// 无论API调用成功与否都清除本地数据
localStorage.removeItem('authData');
localStorage.removeItem('isLoggedIn');
localStorage.removeItem('username');
localStorage.removeItem('token');
sessionStorage.removeItem('authData');
sessionStorage.removeItem('userSession');
alert('已成功退出登录!');
window.location.href = 'login.html';
}).catch(error => {
console.error('登出请求失败:', error);
// 即使API调用失败也清除本地数据
localStorage.removeItem('authData');
localStorage.removeItem('isLoggedIn');
localStorage.removeItem('username');
localStorage.removeItem('token');
sessionStorage.removeItem('authData');
sessionStorage.removeItem('userSession');
alert('已成功退出登录!');
window.location.href = 'login.html';
});
}
}