// 用户个人中心页面交互 document.addEventListener('DOMContentLoaded', function() { // 获取表单对象 const profileForm = document.getElementById('profileForm'); const passwordForm = document.getElementById('passwordForm'); // 表单验证逻辑 if (profileForm) { profileForm.addEventListener('submit', function(event) { let valid = true; // 清除之前的错误提示 clearValidationErrors(); // 验证邮箱 const emailField = document.getElementById('email'); if (emailField.value.trim() !== '') { const emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; if (!emailPattern.test(emailField.value.trim())) { showError(emailField, '请输入有效的邮箱地址'); valid = false; } } // 验证手机号 const phoneField = document.getElementById('phone'); if (phoneField.value.trim() !== '') { const phonePattern = /^1[3456789]\d{9}$/; if (!phonePattern.test(phoneField.value.trim())) { showError(phoneField, '请输入有效的手机号码'); valid = false; } } if (!valid) { event.preventDefault(); } }); } // 密码修改表单验证 if (passwordForm) { passwordForm.addEventListener('submit', function(event) { let valid = true; // 清除之前的错误提示 clearValidationErrors(); // 验证当前密码 const currentPasswordField = document.getElementById('current_password'); if (currentPasswordField.value.trim() === '') { showError(currentPasswordField, '请输入当前密码'); valid = false; } // 验证新密码 const newPasswordField = document.getElementById('new_password'); if (newPasswordField.value.trim() === '') { showError(newPasswordField, '请输入新密码'); valid = false; } else if (newPasswordField.value.length < 6) { showError(newPasswordField, '密码长度至少为6个字符'); valid = false; } // 验证确认密码 const confirmPasswordField = document.getElementById('confirm_password'); if (confirmPasswordField.value.trim() === '') { showError(confirmPasswordField, '请确认新密码'); valid = false; } else if (confirmPasswordField.value !== newPasswordField.value) { showError(confirmPasswordField, '两次输入的密码不一致'); valid = false; } if (!valid) { event.preventDefault(); } }); } // 获取用户统计数据 fetchUserStats(); // 获取用户活动记录 const activityFilter = document.getElementById('activityFilter'); if (activityFilter) { // 初始加载 fetchUserActivities('all'); // 监听过滤器变化 activityFilter.addEventListener('change', function() { fetchUserActivities(this.value); }); } // 处理URL中的tab参数 const urlParams = new URLSearchParams(window.location.search); const tabParam = urlParams.get('tab'); if (tabParam) { const tabElement = document.getElementById(`${tabParam}-tab`); if (tabElement) { $('#profileTabs a[href="#' + tabParam + '"]').tab('show'); } } // 清除表单验证错误 function clearValidationErrors() { const invalidFields = document.querySelectorAll('.is-invalid'); const feedbackElements = document.querySelectorAll('.invalid-feedback'); invalidFields.forEach(field => { field.classList.remove('is-invalid'); }); feedbackElements.forEach(element => { element.parentNode.removeChild(element); }); } // 显示错误消息 function showError(field, message) { field.classList.add('is-invalid'); const feedback = document.createElement('div'); feedback.className = 'invalid-feedback'; feedback.innerText = message; field.parentNode.appendChild(feedback); } // 获取用户统计数据 function fetchUserStats() { fetch('/user/api/stats') .then(response => { if (!response.ok) { throw new Error('Network response was not ok'); } return response.json(); }) .then(data => { updateUserStats(data); }) .catch(error => { console.error('Error fetching user stats:', error); // 出错时使用默认值 updateUserStats({borrow: 0, returned: 0, overdue: 0}); }); } // 更新用户统计显示 function updateUserStats(data) { const borrowCount = document.getElementById('borrowCount'); const returnedCount = document.getElementById('returnedCount'); const overdueCount = document.getElementById('overdueCount'); if (borrowCount) borrowCount.textContent = data.borrow; if (returnedCount) returnedCount.textContent = data.returned; if (overdueCount) overdueCount.textContent = data.overdue; } // 获取用户活动记录 function fetchUserActivities(type) { const timelineContainer = document.getElementById('activityTimeline'); if (!timelineContainer) return; // 显示加载中 timelineContainer.innerHTML = `
加载中...