// profile.js - 处理用户个人资料页面的功能 // 等待DOM加载完成 document.addEventListener('DOMContentLoaded', function() { // 检查用户登录状态 checkAuthStatus(); // 初始化表单事件监听器 initFormListeners(); // 初始化模态框事件监听器 initModalListeners(); }); /** * 检查用户的登录状态 */ async function checkAuthStatus() { try { // 确保Supabase客户端已初始化 if (window.supabaseClient) { const { data: { user }, error } = await window.supabaseClient.auth.getUser(); if (error || !user) { // 用户未登录,重定向到首页 showAlert('请先登录以访问个人资料页面', 'warning'); setTimeout(() => { window.location.href = 'index.html'; }, 2000); return; } // 用户已登录,加载个人资料数据 loadProfileData(user); // 显示用户信息 document.getElementById('username').textContent = user.email.split('@')[0]; document.getElementById('profile-username').textContent = user.email.split('@')[0]; document.getElementById('profile-email').textContent = user.email; // 显示用户界面元素 document.getElementById('auth-buttons').classList.add('d-none'); document.getElementById('user-profile').classList.remove('d-none'); } else { console.error('Supabase client not initialized'); } } catch (error) { console.error('Error checking auth status:', error); } } /** * 加载用户个人资料数据 * @param {Object} user - 当前登录的用户对象 */ async function loadProfileData(user) { try { if (window.supabaseClient) { // 从Supabase获取用户个人资料数据 const { data, error } = await window.supabaseClient .from('profiles') .select('*') .eq('user_id', user.id) .single(); if (error) { console.error('Error loading profile data:', error); return; } if (data) { // 填充表单数据 document.getElementById('display-name').value = data.display_name || ''; document.getElementById('profile-bio').value = data.bio || ''; document.getElementById('profile-company').value = data.company || ''; document.getElementById('profile-website').value = data.website || ''; document.getElementById('profile-location').value = data.location || ''; document.getElementById('contact-email').value = user.email; document.getElementById('contact-phone').value = data.phone || ''; document.getElementById('contact-wechat').value = data.wechat || ''; } } } catch (error) { console.error('Error loading profile data:', error); } } /** * 初始化表单事件监听器 */ function initFormListeners() { // 个人资料表单提交 const profileForm = document.getElementById('profile-form'); if (profileForm) { profileForm.addEventListener('submit', async function(e) { e.preventDefault(); await updateProfile(); }); } // 联系信息表单提交 const contactForm = document.getElementById('contact-form'); if (contactForm) { contactForm.addEventListener('submit', async function(e) { e.preventDefault(); await updateContactInfo(); }); } // 侧边栏退出登录 const sidebarLogout = document.getElementById('sidebar-logout'); if (sidebarLogout) { sidebarLogout.addEventListener('click', function(e) { e.preventDefault(); logout(); }); } // 导航栏退出登录 const logoutButton = document.getElementById('logout-button'); if (logoutButton) { logoutButton.addEventListener('click', function(e) { e.preventDefault(); logout(); }); } // 更改头像按钮 const changeAvatarBtn = document.getElementById('change-avatar-btn'); if (changeAvatarBtn) { changeAvatarBtn.addEventListener('click', function(e) { e.preventDefault(); // 此功能需要实现文件上传,暂时显示提示 showAlert('头像上传功能即将推出', 'info'); }); } } /** * 初始化模态框事件监听器 */ function initModalListeners() { // 更改密码按钮 const changePasswordBtn = document.getElementById('change-password-btn'); if (changePasswordBtn) { changePasswordBtn.addEventListener('click', function() { const changePasswordModal = new bootstrap.Modal(document.getElementById('changePasswordModal')); changePasswordModal.show(); }); } // 保存密码按钮 const savePasswordBtn = document.getElementById('save-password-btn'); if (savePasswordBtn) { savePasswordBtn.addEventListener('click', async function() { await updatePassword(); }); } // 查看会话按钮 const viewSessionsBtn = document.getElementById('view-sessions-btn'); if (viewSessionsBtn) { viewSessionsBtn.addEventListener('click', function() { const sessionsModal = new bootstrap.Modal(document.getElementById('sessionsModal')); sessionsModal.show(); // 此功能需要实现会话管理,暂时显示示例数据 }); } // 登出所有其他设备按钮 const logoutAllBtn = document.getElementById('logout-all-btn'); if (logoutAllBtn) { logoutAllBtn.addEventListener('click', async function() { // 此功能需要实现会话管理,暂时显示提示 showAlert('此功能即将推出', 'info'); }); } // 两步验证开关 const twoFactorToggle = document.getElementById('two-factor-toggle'); if (twoFactorToggle) { twoFactorToggle.addEventListener('change', function() { // 此功能需要实现两步验证,暂时显示提示 showAlert('两步验证功能即将推出', 'info'); // 重置开关状态 this.checked = false; }); } } /** * 更新用户个人资料 */ async function updateProfile() { try { if (window.supabaseClient) { const { data: { user }, error: userError } = await window.supabaseClient.auth.getUser(); if (userError || !user) { showAlert('获取用户信息失败,请重新登录', 'error'); return; } const displayName = document.getElementById('display-name').value; const bio = document.getElementById('profile-bio').value; const company = document.getElementById('profile-company').value; const website = document.getElementById('profile-website').value; const location = document.getElementById('profile-location').value; // 更新个人资料 const { error } = await window.supabaseClient .from('profiles') .upsert({ user_id: user.id, display_name: displayName, bio: bio, company: company, website: website, location: location, updated_at: new Date() }); if (error) { console.error('Error updating profile:', error); showAlert('更新个人资料失败', 'error'); return; } showAlert('个人资料已更新', 'success'); } } catch (error) { console.error('Error updating profile:', error); showAlert('更新个人资料时出错', 'error'); } } /** * 更新联系信息 */ async function updateContactInfo() { try { if (window.supabaseClient) { const { data: { user }, error: userError } = await window.supabaseClient.auth.getUser(); if (userError || !user) { showAlert('获取用户信息失败,请重新登录', 'error'); return; } const phone = document.getElementById('contact-phone').value; const wechat = document.getElementById('contact-wechat').value; // 更新联系信息 const { error } = await window.supabaseClient .from('profiles') .upsert({ user_id: user.id, phone: phone, wechat: wechat, updated_at: new Date() }); if (error) { console.error('Error updating contact info:', error); showAlert('更新联系信息失败', 'error'); return; } showAlert('联系信息已更新', 'success'); } } catch (error) { console.error('Error updating contact info:', error); showAlert('更新联系信息时出错', 'error'); } } /** * 更新密码 */ async function updatePassword() { try { if (window.supabaseClient) { const currentPassword = document.getElementById('current-password').value; const newPassword = document.getElementById('new-password').value; const confirmPassword = document.getElementById('confirm-password').value; // 验证新密码 if (newPassword.length < 8) { showAlert('密码必须至少包含8个字符', 'warning'); return; } // 验证密码匹配 if (newPassword !== confirmPassword) { showAlert('新密码和确认密码不匹配', 'warning'); return; } // 更新密码 const { error } = await window.supabaseClient.auth.updateUser({ password: newPassword }); if (error) { console.error('Error updating password:', error); showAlert('更新密码失败: ' + error.message, 'error'); return; } // 关闭模态框 const changePasswordModal = bootstrap.Modal.getInstance(document.getElementById('changePasswordModal')); changePasswordModal.hide(); // 清空表单 document.getElementById('change-password-form').reset(); showAlert('密码已更新', 'success'); } } catch (error) { console.error('Error updating password:', error); showAlert('更新密码时出错', 'error'); } } /** * 退出登录 */ async function logout() { try { if (window.supabaseClient) { const { error } = await window.supabaseClient.auth.signOut(); if (error) { console.error('Error signing out:', error); showAlert('退出登录失败', 'error'); return; } // 重定向到首页 window.location.href = 'index.html'; } } catch (error) { console.error('Error signing out:', error); showAlert('退出登录时出错', 'error'); } } /** * 显示提示信息 * @param {string} message - 提示消息 * @param {string} type - 提示类型 (success, info, warning, error) */ function showAlert(message, type = 'info') { // 创建提示元素 const alertDiv = document.createElement('div'); alertDiv.className = `alert alert-${type === 'error' ? 'danger' : type} alert-dismissible fade show position-fixed top-0 start-50 translate-middle-x mt-3`; alertDiv.style.zIndex = '9999'; alertDiv.style.maxWidth = '90%'; alertDiv.style.width = '400px'; // 添加提示内容 alertDiv.innerHTML = ` ${message} `; // 添加到页面 document.body.appendChild(alertDiv); // 自动关闭 setTimeout(() => { const bsAlert = new bootstrap.Alert(alertDiv); bsAlert.close(); }, 5000); }