/** * 用户中心页面脚本 */ // 使用web-auth.js中的全局webAuth实例 // const webAuth = new WebAuth(); // 已在web-auth.js中创建 // 当前Tab let currentTab = 'profile'; // 登录历史分页 let historyPage = 1; const historyPageSize = 10; /** * 页面加载完成后初始化 */ document.addEventListener('DOMContentLoaded', function() { // 检查登录状态 if (!webAuth.isLoggedIn()) { window.location.href = '/'; return; } // 初始化页面 initUserCenter(); // 绑定Tab切换事件 bindTabEvents(); // 绑定表单事件 bindFormEvents(); }); /** * 初始化用户中心 */ async function initUserCenter() { try { const userInfo = await webAuth.getUserInfo(); if (!userInfo) { window.location.href = '/'; return; } // 更新导航栏 document.getElementById('navUsername').textContent = userInfo.username; // 更新侧边栏 updateSidebar(userInfo); // 加载个人资料 loadProfile(userInfo); } catch (error) { console.error('初始化用户中心失败:', error); alert('加载用户信息失败,请重新登录'); window.location.href = '/'; } } /** * 更新侧边栏 */ function updateSidebar(userInfo) { // 用户头像(显示用户名首字母) const avatar = document.getElementById('userAvatar'); avatar.textContent = userInfo.username.charAt(0).toUpperCase(); // 用户名 document.getElementById('sidebarUsername').textContent = userInfo.username; // 用户等级 const levelBadge = document.getElementById('sidebarLevel'); levelBadge.textContent = getLevelText(userInfo.level); levelBadge.className = 'level-badge level-' + userInfo.level; } /** * 加载个人资料 */ function loadProfile(userInfo) { document.getElementById('profileUsername').textContent = userInfo.username; const levelBadge = document.getElementById('profileLevel'); levelBadge.textContent = getLevelText(userInfo.level); levelBadge.className = 'level-badge level-' + userInfo.level; const statusBadge = document.getElementById('profileStatus'); statusBadge.textContent = userInfo.is_active ? '正常' : '已禁用'; statusBadge.className = userInfo.is_active ? 'badge bg-success' : 'badge bg-danger'; document.getElementById('profileCreatedAt').textContent = formatDateTime(userInfo.created_at); document.getElementById('profileLastLogin').textContent = userInfo.last_login_at ? formatDateTime(userInfo.last_login_at) : '从未登录'; } /** * 绑定Tab切换事件 */ function bindTabEvents() { const navLinks = document.querySelectorAll('.sidebar .nav-link'); navLinks.forEach(link => { link.addEventListener('click', function(e) { e.preventDefault(); const tab = this.getAttribute('data-tab'); switchTab(tab); }); }); } /** * 切换Tab */ function switchTab(tab) { // 更新导航状态 document.querySelectorAll('.sidebar .nav-link').forEach(link => { link.classList.remove('active'); }); document.querySelector(`[data-tab="${tab}"]`).classList.add('active'); // 隐藏所有内容 document.querySelectorAll('.content-card').forEach(card => { card.style.display = 'none'; }); // 显示对应内容 currentTab = tab; switch(tab) { case 'profile': document.getElementById('profileTab').style.display = 'block'; break; case 'password': document.getElementById('passwordTab').style.display = 'block'; break; case 'history': document.getElementById('historyTab').style.display = 'block'; loadLoginHistory(); break; case 'devices': document.getElementById('devicesTab').style.display = 'block'; loadDevices(); break; } } /** * 绑定表单事件 */ function bindFormEvents() { // 修改密码表单 document.getElementById('changePasswordForm').addEventListener('submit', handleChangePassword); } /** * 处理修改密码 */ async function handleChangePassword(e) { e.preventDefault(); const oldPassword = document.getElementById('oldPassword').value; const newPassword = document.getElementById('newPassword').value; const confirmPassword = document.getElementById('confirmPassword').value; // 隐藏提示 document.getElementById('passwordError').style.display = 'none'; document.getElementById('passwordSuccess').style.display = 'none'; // 验证新密码 if (newPassword !== confirmPassword) { showPasswordError('两次输入的新密码不一致'); return; } if (newPassword.length < 6 || newPassword.length > 32) { showPasswordError('新密码长度必须在6-32个字符之间'); return; } if (!/[a-zA-Z]/.test(newPassword) || !/[0-9]/.test(newPassword)) { showPasswordError('新密码必须包含字母和数字'); return; } try { await webAuth.changePassword(oldPassword, newPassword, confirmPassword); showPasswordSuccess('密码修改成功,请重新登录'); // 清空表单 document.getElementById('changePasswordForm').reset(); // 3秒后跳转到首页 setTimeout(() => { window.location.href = '/'; }, 3000); } catch (error) { showPasswordError(error.message || '修改密码失败'); } } function showPasswordError(message) { const errorDiv = document.getElementById('passwordError'); errorDiv.textContent = message; errorDiv.style.display = 'block'; } function showPasswordSuccess(message) { const successDiv = document.getElementById('passwordSuccess'); successDiv.textContent = message; successDiv.style.display = 'block'; } /** * 加载登录历史 */ async function loadLoginHistory() { try { const response = await fetch(`/api/v1/web-auth/login-history?page=${historyPage}&page_size=${historyPageSize}`, { headers: { 'Authorization': `Bearer ${webAuth.getToken()}` } }); if (!response.ok) { throw new Error('加载登录历史失败'); } const data = await response.json(); renderLoginHistory(data.items); renderHistoryPagination(data.total, data.page, data.page_size); } catch (error) { console.error('加载登录历史失败:', error); document.getElementById('loginHistoryList').innerHTML = '