// ============================================================ // 🚀 主入口 // ============================================================ let currentUser = null; function showToast(message, type = 'success', duration = 3000) { // 移除已有 toast const existing = document.querySelector('.toast'); if (existing) existing.remove(); const toast = document.createElement('div'); toast.className = 'toast ' + type; const icons = { success: ``, error: ``, warning: `` }; toast.innerHTML = ` ${icons[type] || icons.success} ${escapeHTML(message)} `; document.body.appendChild(toast); requestAnimationFrame(() => { toast.classList.add('show'); }); setTimeout(() => { toast.classList.remove('show'); setTimeout(() => toast.remove(), 400); }, duration); } let currentPage = 'feed'; let currentPageNum = 1; function renderNav() { const authBtns = document.getElementById('authButtons'); const userDropdown = document.getElementById('userDropdown'); if (currentUser) { authBtns.style.display = 'none'; userDropdown.style.display = 'block'; document.getElementById('avatarImg').src = currentUser.avatar_url || 'https://ui-avatars.com/api/?name=U&background=6366f1&color=fff'; document.getElementById('adminEntry').style.display = currentUser.role === 'admin' ? 'block' : 'none'; updateUnreadBadge(); } else { authBtns.style.display = 'flex'; userDropdown.style.display = 'none'; } } async function loadForumName() { try { const data = await API.getSettings(); const name = data.forum_name || CONFIG.FORUM_NAME || 'Forumlify'; document.getElementById('forumName').textContent = name; document.title = name; const titleEl = document.getElementById('pageTitle'); if (titleEl) titleEl.textContent = name; } catch (e) { const name = CONFIG.FORUM_NAME || 'Forumlify'; document.getElementById('forumName').textContent = name; document.title = name; const titleEl = document.getElementById('pageTitle'); if (titleEl) titleEl.textContent = name; } } function switchPage(page, param) { const url = new URL(window.location); if (page === 'user' && param) { url.searchParams.set('user', param); url.searchParams.delete('page'); url.searchParams.delete('post'); window.history.pushState({ page: 'user', username: param }, '', url); showUserPage(param); return; } if (page === 'post' && param) { url.searchParams.set('post', param); url.searchParams.delete('page'); url.searchParams.delete('user'); window.history.pushState({ page: 'post', postId: param }, '', url); showPostPage(param); return; } if (page === 'custom' && param) { url.searchParams.set('custom', param); url.searchParams.delete('page'); url.searchParams.delete('post'); url.searchParams.delete('user'); window.history.pushState({ page: 'custom', custom: param }, '', url); showCustomPage(param); return; } if (page === 'feed') { url.searchParams.delete('page'); url.searchParams.delete('post'); url.searchParams.delete('user'); url.searchParams.delete('custom'); } else { url.searchParams.set('page', page); url.searchParams.delete('post'); url.searchParams.delete('user'); url.searchParams.delete('custom'); } window.history.pushState({ page: page }, '', url); document.getElementById('app').style.display = 'none'; document.querySelectorAll('.page-slide').forEach(el => { el.classList.remove('active', 'slide-out'); }); const customContainer = document.getElementById('customPageContainer'); if (customContainer) { customContainer.classList.remove('active'); customContainer.style.display = 'none'; } if (page === 'feed') { document.getElementById('app').style.display = 'flex'; currentPage = 'feed'; renderFeed(); renderStats(); renderLinks(); return; } const pageMap = { messages: 'pageMessages', settings: 'pageSettings', admin: 'pageAdmin', new: 'pageNew' }; const el = document.getElementById(pageMap[page]); if (el) { el.classList.add('active'); el.style.animation = 'none'; void el.offsetHeight; el.style.animation = ''; currentPage = page; if (page === 'admin') { document.querySelectorAll('.admin-tab').forEach((t, i) => { t.classList.toggle('active', i === 0); }); renderAdminReports(); } if (page === 'settings') { renderSettingsPage('profile'); } if (page === 'messages') { renderMessagesPage(); } if (page === 'new') { document.getElementById('postTitle').value = ''; document.getElementById('postContent').value = ''; document.getElementById('imagePreview').innerHTML = ''; document.getElementById('fileInput').value = ''; document.getElementById('postCaptchaInput').value = ''; refreshCaptcha('post'); const dropText = document.getElementById('dropZoneText'); if (dropText) dropText.textContent = '点击或拖拽上传图片'; } } } // ============================================================ // ✉️ 私信系统 // ============================================================ let currentChatUserId = null; let currentChatUsername = null; let currentConversationId = null; let messagePollInterval = null; async function updateUnreadBadge() { const badge = document.getElementById('messageBadge'); if (!badge || !currentUser) return; try { const conversations = await API.getConversations(); const totalUnread = conversations.reduce((sum, c) => sum + (c.unread_count || 0), 0); if (totalUnread > 0) { badge.style.display = 'inline-block'; badge.textContent = totalUnread > 99 ? '99+' : totalUnread; } else { badge.style.display = 'none'; } } catch (e) {} } function openMessageList() { document.getElementById('messageListModal').classList.add('active'); renderMessageList(); } function closeMessageList() { document.getElementById('messageListModal').classList.remove('active'); if (messagePollInterval) { clearInterval(messagePollInterval); messagePollInterval = null; } } async function renderMessageList() { const container = document.getElementById('messageListContent'); container.innerHTML = '
加载中...
'; try { const conversations = await API.getConversations(); if (conversations.length === 0) { container.innerHTML = '
暂无私信
'; return; } let html = ''; conversations.forEach(c => { const unread = c.unread_count || 0; const lastMsg = c.last_message || '暂无消息'; const time = c.last_message_time ? new Date(c.last_message_time).toLocaleString('zh-CN') : ''; const avatar = c.other_avatar_url || 'https://ui-avatars.com/api/?name=' + encodeURIComponent(c.other_username) + '&background=6366f1&color=fff&size=64'; html += `
${escapeHTML(c.other_username)} ${time}
${escapeHTML(lastMsg)} ${unread > 0 ? `${unread}` : ''}
`; }); container.innerHTML = sanitizeHTML(html); container.querySelectorAll('.message-list-item').forEach(item => { item.addEventListener('click', function() { openChat(this.dataset.conversationId, this.dataset.userId, this.dataset.username); }); }); } catch (err) { container.innerHTML = '
加载失败
'; } } function openChat(conversationId, otherUserId, otherUsername) { currentConversationId = conversationId; currentChatUserId = otherUserId; currentChatUsername = otherUsername; document.getElementById('chatModal').classList.add('active'); document.getElementById('chatTitle').textContent = otherUsername; renderMessages(conversationId); if (messagePollInterval) clearInterval(messagePollInterval); messagePollInterval = setInterval(() => { if (currentConversationId) { renderMessages(currentConversationId, true); } }, 3000); } function closeChat() { document.getElementById('chatModal').classList.remove('active'); if (messagePollInterval) { clearInterval(messagePollInterval); messagePollInterval = null; } currentConversationId = null; currentChatUserId = null; currentChatUsername = null; updateUnreadBadge(); } async function renderMessages(conversationId, silent = false) { const container = document.getElementById('chatMessages'); if (!silent) { container.innerHTML = '
加载中...
'; } try { const messages = await API.getMessages(conversationId); if (messages.length === 0) { container.innerHTML = '
还没有消息,打个招呼吧
'; return; } let html = ''; messages.forEach(m => { const isMine = m.sender_id === currentUser.id; const time = m.created_at ? new Date(m.created_at).toLocaleString('zh-CN') : ''; const avatar = m.sender_avatar_url || 'https://ui-avatars.com/api/?name=' + encodeURIComponent(m.sender_username) + '&background=6366f1&color=fff&size=64'; html += `
${!isMine ? `` : ''}
${renderPlainText(m.content)}
${time} ${isMine ? (m.is_read ? '✓✓' : '✓') : ''}
`; }); container.innerHTML = sanitizeHTML(html); container.scrollTop = container.scrollHeight; } catch (err) { if (!silent) { container.innerHTML = '
加载失败
'; } } } async function sendMessage() { const input = document.getElementById('chatInput'); const content = input.value.trim(); if (!content || !currentConversationId) return; try { await API.sendMessage(currentConversationId, content); input.value = ''; renderMessages(currentConversationId); updateUnreadBadge(); if (document.getElementById('messageListModal').classList.contains('active')) { renderMessageList(); } } catch (err) { alert('发送失败:' + err.message); } } async function openPrivateChat(otherUserId, otherUsername) { try { const result = await API.getOrCreateConversation(otherUserId); currentConversationId = result.id; currentChatUserId = otherUserId; currentChatUsername = otherUsername; document.getElementById('chatModal').classList.add('active'); document.getElementById('chatTitle').textContent = otherUsername; renderMessages(currentConversationId); if (messagePollInterval) clearInterval(messagePollInterval); messagePollInterval = setInterval(() => { if (currentConversationId) { renderMessages(currentConversationId, true); } }, 3000); } catch (err) { alert('打开私信失败:' + err.message); } } // ============================================================ // 📸 图片上传(拖拽上传) // ============================================================ function handleImageFiles(files) { const preview = document.getElementById('imagePreview'); if (!preview) return; for (let file of files) { if (!file.type.startsWith('image/')) continue; if (file.size > 5 * 1024 * 1024) { alert('图片 ' + file.name + ' 超过 5MB,请压缩后上传'); continue; } const reader = new FileReader(); reader.onload = function(e) { const img = document.createElement('img'); img.src = e.target.result; img.style.cssText = 'width:80px;height:80px;object-fit:cover;border-radius:4px;border:1px solid var(--border);'; preview.appendChild(img); }; reader.readAsDataURL(file); } const fileInput = document.getElementById('fileInput'); if (fileInput) fileInput.value = ''; } // ============================================================ // 📄 自定义页面导航 // ============================================================ let customPagesNav = []; async function loadCustomPagesNav() { try { const pages = await API.getCustomPages(); customPagesNav = pages; renderCustomPagesNav(); } catch (e) { customPagesNav = []; } } function renderCustomPagesNav() { const container = document.getElementById('customNavLinks'); if (!container) return; container.innerHTML = ''; customPagesNav.forEach(page => { const link = document.createElement('a'); link.href = '#'; link.dataset.custom = page.name; link.textContent = page.title; link.style.cssText = 'color:var(--text-secondary);text-decoration:none;font-size:14px;padding:4px 10px;border-radius:4px;transition:color 0.15s;'; link.addEventListener('mouseenter', function() { this.style.color = 'var(--text)'; }); link.addEventListener('mouseleave', function() { this.style.color = 'var(--text-secondary)'; }); link.addEventListener('click', function(e) { e.preventDefault(); switchPage('custom', page.name); }); container.appendChild(link); }); } // ============================================================ // 📄 自定义页面渲染 // ============================================================ function showCustomPage(pageName) { document.getElementById('app').style.display = 'none'; document.querySelectorAll('.page-slide').forEach(el => { el.classList.remove('active', 'slide-out'); }); let container = document.getElementById('customPageContainer'); if (!container) { container = document.createElement('div'); container.id = 'customPageContainer'; container.className = 'page-slide'; container.style.cssText = 'display:none;position:fixed;inset:0;background:var(--bg);z-index:50;padding:84px 32px 40px;overflow-y:auto;transition:background 0.2s;'; document.body.appendChild(container); } container.innerHTML = '
加载中...
'; container.classList.add('active'); container.style.display = 'block'; currentPage = 'custom'; API.getCustomPage(pageName).then(page => { const iframe = document.createElement('iframe'); iframe.style.cssText = 'width:100%;min-height:70vh;border:none;border-radius:8px;background:var(--surface);'; iframe.sandbox = 'allow-scripts allow-modals allow-top-navigation allow-same-origin allow-popups'; iframe.srcdoc = ` ${page.content} `; container.innerHTML = ''; container.appendChild(iframe); document.querySelectorAll('.custom-page-nav-link').forEach(el => { el.style.color = el.dataset.custom === pageName ? 'var(--primary)' : 'var(--text-secondary)'; }); }).catch(err => { container.innerHTML = '
页面加载失败:' + escapeHTML(err.message) + '
'; }); } // ============================================================ // 📩 消息页面 // ============================================================ async function renderMessagesPage() { const container = document.getElementById('messagesContent'); if (!container) return; container.innerHTML = '
加载中...
'; try { const notifications = await apiFetch('/notifications'); if (notifications.error) throw new Error(notifications.error); if (notifications.length === 0) { container.innerHTML = `

暂无消息

当有人回复你的帖子或处理你的举报时,会在这里通知你

`; return; } await apiFetch('/notifications/read-all', { method: 'PUT' }); let html = ''; const typeMap = { reply: getIcon('message'), post_deleted: getIcon('delete'), report_handled: getIcon('shield'), system: getIcon('megaphone') }; notifications.forEach(n => { const icon = typeMap[n.type] || getIcon('pin'); const time = n.created_at ? new Date(n.created_at).toLocaleString('zh-CN') : ''; html += `
${icon}
${escapeHTML(n.title)}
${escapeHTML(n.content)}
${n.link ? `查看详情 →` : ''}
${time}
`; }); container.innerHTML = sanitizeHTML(html); } catch (err) { container.innerHTML = '
加载失败
'; } } // ============================================================ // 📊 统计数据 // ============================================================ function renderStats() { API.getStats().then(stats => { // 主页的统计 const topicsEl = document.getElementById('statTopics'); const postsEl = document.getElementById('statPosts'); const usersEl = document.getElementById('statUsers'); if (topicsEl) topicsEl.textContent = stats.topics || 0; if (postsEl) postsEl.textContent = stats.posts || 0; if (usersEl) usersEl.textContent = stats.users || 0; // 帖子详情页的统计(ID 带 2) const topics2El = document.getElementById('statTopics2'); const posts2El = document.getElementById('statPosts2'); const users2El = document.getElementById('statUsers2'); if (topics2El) topics2El.textContent = stats.topics || 0; if (posts2El) posts2El.textContent = stats.posts || 0; if (users2El) users2El.textContent = stats.users || 0; }).catch(() => {}); } // ============================================================ // 🔗 友情链接 // ============================================================ function renderLinks() { API.getLinks().then(links => { // 主页的友链 const ul = document.getElementById('friendlyLinks'); if (ul) { if (!links || links.length === 0) { ul.innerHTML = '
  • 暂无链接
  • '; } else { let html = ''; links.forEach(l => { html += '
  • ' + escapeHTML(l.title) + '
  • '; }); ul.innerHTML = sanitizeHTML(html); } } // 帖子详情页的友链 const ul2 = document.getElementById('friendlyLinks2'); if (ul2) { if (!links || links.length === 0) { ul2.innerHTML = '
  • 暂无链接
  • '; } else { let html = ''; links.forEach(l => { html += '
  • ' + escapeHTML(l.title) + '
  • '; }); ul2.innerHTML = sanitizeHTML(html); } } }).catch(() => {}); } // ============================================================ // ⚙️ 设置页面(含侧边栏) // ============================================================ let currentSettingsTab = 'profile'; async function renderSettingsPage(tab = 'profile') { const container = document.getElementById('settingsContent'); if (!container) return; // 更新侧边栏高亮 document.querySelectorAll('.settings-nav-item').forEach(el => el.classList.remove('active')); document.querySelector(`.settings-nav-item[data-settings-tab="${tab}"]`)?.classList.add('active'); currentSettingsTab = tab; if (!currentUser) { container.innerHTML = '
    请先登录
    '; return; } const user = currentUser; try { const userPosts = await apiFetch('/posts?user_id=' + user.id); const postCount = userPosts.data ? userPosts.data.length : 0; let recoveryCount = 0; try { const countData = await API.getRecoveryCodesCount(); recoveryCount = countData.count || 0; } catch (e) {} if (tab === 'profile') { container.innerHTML = `

    ${getIcon('user')} 个人资料

    ${escapeHTML(user.username)}

    ${escapeHTML(user.bio || '这个人很懒,什么都没写')}

    ${getIcon('calendar')} 加入 ${user.created_at ? new Date(user.created_at).toLocaleDateString('zh-CN') : '未知'} ${getIcon('file')} ${postCount} 帖 ${user.role === 'admin' ? `${getIcon('shield')} 管理员` : ''}
    用 --- 分隔,支持 Markdown
    `; // ===== 头像上传 ===== const avatarBtn = document.getElementById('avatarUploadBtn'); const avatarInput = document.getElementById('avatarFileInput'); const avatarPreview = document.getElementById('avatarPreview'); const statusEl = document.getElementById('avatarUploadStatus'); if (avatarBtn && avatarInput) { avatarBtn.addEventListener('click', function() { avatarInput.click(); }); avatarInput.addEventListener('change', async function() { const file = this.files[0]; if (!file) return; if (!file.type.startsWith('image/')) { showToast('请选择图片文件', 'error'); return; } if (file.size > 5 * 1024 * 1024) { showToast('图片不能超过 5MB', 'error'); return; } showToast('上传中...', 'warning'); try { const formData = new FormData(); formData.append('file', file); const uploadRes = await fetch(CONFIG.API_BASE_URL + '/upload', { method: 'POST', headers: { 'Authorization': 'Bearer ' + localStorage.getItem('forumlify-token') }, body: formData }); const uploadData = await uploadRes.json(); if (uploadData.error) throw new Error(uploadData.error); const avatarUrl = uploadData.url; const updateRes = await API.updateAvatar(user.id, avatarUrl); if (updateRes.error) throw new Error(updateRes.error); currentUser.avatar_url = avatarUrl; avatarPreview.src = avatarUrl; showToast('头像更新成功!', 'success'); renderNav(); } catch (err) { showToast(err.message, 'error'); } }); } // ===== 保存设置(含签名) ===== document.getElementById('settingsSaveBtn').addEventListener('click', async function() { const username = document.getElementById('settingsUsername').value.trim(); const bio = document.getElementById('settingsBio').value.trim(); const signature = document.getElementById('settingsSignature').value.trim(); if (!username) { alert('用户名不能为空'); return; } try { await API.updateProfile(user.id, username, bio, signature); currentUser.username = username; currentUser.bio = bio; currentUser.signature = signature; alert('保存成功!'); renderNav(); renderSettingsPage('profile'); } catch (err) { alert('保存失败:' + err.message); } }); } else if (tab === 'security') { container.innerHTML = `

    ${getIcon('lock')} 安全设置

    修改密码

    修改邮箱

    `; // ===== 修改密码 ===== document.getElementById('changePasswordBtn').addEventListener('click', async function() { const oldPassword = document.getElementById('changeOldPassword').value; const newPassword = document.getElementById('changeNewPassword').value; const statusEl = document.getElementById('passwordChangeStatus'); if (!oldPassword || !newPassword) { statusEl.textContent = '请填写完整信息'; statusEl.style.color = '#ef4444'; return; } if (newPassword.length < 6) { statusEl.textContent = '新密码至少6位'; statusEl.style.color = '#ef4444'; return; } try { await API.changePassword(oldPassword, newPassword); document.getElementById('changeOldPassword').value = ''; document.getElementById('changeNewPassword').value = ''; statusEl.textContent = getIcon('success') + ' 密码修改成功!'; statusEl.style.color = '#22c55e'; } catch (err) { statusEl.textContent = getIcon('error') + ' ' + err.message; statusEl.style.color = '#ef4444'; } }); // ===== 修改邮箱 ===== document.getElementById('changeEmailBtn').addEventListener('click', async function() { const password = document.getElementById('changeEmailPassword').value; const newEmail = document.getElementById('changeNewEmail').value; const statusEl = document.getElementById('emailChangeStatus'); if (!password || !newEmail) { statusEl.textContent = '请填写完整信息'; statusEl.style.color = '#ef4444'; return; } try { await API.changeEmail(password, newEmail); document.getElementById('changeEmailPassword').value = ''; document.getElementById('changeNewEmail').value = ''; currentUser.email = newEmail; statusEl.textContent = getIcon('success') + ' 邮箱修改成功!'; statusEl.style.color = '#22c55e'; } catch (err) { statusEl.textContent = getIcon('error') + ' ' + err.message; statusEl.style.color = '#ef4444'; } }); } else if (tab === 'recovery') { container.innerHTML = `

    ${getIcon('key')} 恢复码

    用于忘记密码时重置账户。每个恢复码只能使用一次。

    剩余 ${recoveryCount} 个可用恢复码
    `; // ===== 恢复码管理 ===== const viewBtn = document.getElementById('viewRecoveryCodesBtn'); const regenBtn = document.getElementById('regenerateRecoveryCodesBtn'); const recoveryStatus = document.getElementById('recoveryCodesStatus'); if (viewBtn) { viewBtn.addEventListener('click', async function() { try { const data = await API.generateRecoveryCodes(); showRecoveryCodesModal(data.codes); const countData = await API.getRecoveryCodesCount(); if (recoveryStatus) recoveryStatus.textContent = '剩余 ' + (countData.count || 0) + ' 个可用恢复码'; } catch (err) { alert('获取恢复码失败:' + err.message); } }); } if (regenBtn) { regenBtn.addEventListener('click', async function() { if (!confirm('重新生成将替换所有旧的恢复码,确定继续吗?')) return; try { const data = await API.generateRecoveryCodes(); showRecoveryCodesModal(data.codes); const countData = await API.getRecoveryCodesCount(); if (recoveryStatus) recoveryStatus.textContent = '剩余 ' + (countData.count || 0) + ' 个可用恢复码'; } catch (err) { alert('重新生成失败:' + err.message); } }); } } } catch (err) { container.innerHTML = '
    加载失败
    '; } } // ============================================================ // 设置页面侧边栏切换 // ============================================================ document.querySelector('.settings-nav')?.addEventListener('click', function(e) { const tabLink = e.target.closest('.settings-nav-item'); if (!tabLink) return; e.preventDefault(); const tab = tabLink.dataset.settingsTab; if (!tab) return; renderSettingsPage(tab); }); // ============================================================ // ✏️ 编辑帖子模态框 // ============================================================ function openEditModal(postId, currentTitle, currentContent) { const modal = document.createElement('div'); modal.className = 'modal active'; modal.style.display = 'flex'; modal.innerHTML = ` `; document.body.appendChild(modal); modal.querySelector('.close').addEventListener('click', function() { modal.remove(); }); modal.addEventListener('click', function(e) { if (e.target === this) modal.remove(); }); modal.querySelector('#editPostSaveBtn').addEventListener('click', async function() { const title = document.getElementById('editPostTitle').value.trim() || '无标题'; const content = document.getElementById('editPostContent').value.trim(); if (!content) { alert('请填写内容'); return; } try { await API.updatePost(postId, title, content); modal.remove(); if (currentPage === 'post') { renderPostDetail(currentPostId); } else { renderFeed(); } alert('编辑成功!'); } catch (err) { alert('编辑失败:' + err.message); } }); } // ============================================================ // 🔑 恢复码显示模态框 // ============================================================ function showRecoveryCodesModal(codes) { let modal = document.getElementById('recoveryCodesModal'); if (!modal) { modal = document.createElement('div'); modal.id = 'recoveryCodesModal'; modal.className = 'modal active'; modal.style.display = 'flex'; document.body.appendChild(modal); } let codesHtml = ''; codes.forEach((code, i) => { codesHtml += `
    ${String(i + 1).padStart(2, '0')}. ${code}
    `; }); modal.innerHTML = ` `; modal.style.display = 'flex'; modal.classList.add('active'); document.getElementById('closeRecoveryCodesBtn').addEventListener('click', function() { modal.remove(); }); document.getElementById('copyRecoveryCodesBtn').addEventListener('click', function() { const text = codes.join('\n'); navigator.clipboard.writeText(text).then(() => { document.getElementById('copyStatus').textContent = getIcon('success') + ' 已复制到剪贴板'; }).catch(() => { const textarea = document.createElement('textarea'); textarea.value = text; document.body.appendChild(textarea); textarea.select(); document.execCommand('copy'); textarea.remove(); document.getElementById('copyStatus').textContent = getIcon('success') + ' 已复制到剪贴板'; }); }); modal.addEventListener('click', function(e) { if (e.target === this) modal.remove(); }); } // ============================================================ // 🖼️ 图片查看器 // ============================================================ function openImageViewer(imageUrl) { const existing = document.getElementById('imageViewerModal'); if (existing) existing.remove(); const modal = document.createElement('div'); modal.id = 'imageViewerModal'; modal.style.cssText = ` position:fixed;inset:0;background:rgba(0,0,0,0.85);z-index:9999; display:flex;align-items:center;justify-content:center; cursor:pointer;animation:fadeIn 0.2s ease-out; `; const img = document.createElement('img'); img.src = imageUrl; img.style.cssText = ` max-width:90vw;max-height:90vh;object-fit:contain; border-radius:8px;box-shadow:0 8px 32px rgba(0,0,0,0.5); cursor:default;user-select:none; `; modal.addEventListener('click', function(e) { if (e.target === this) { this.remove(); } }); modal.addEventListener('keydown', function(e) { if (e.key === 'Escape') this.remove(); }); modal.appendChild(img); document.body.appendChild(modal); modal.focus(); } // 添加淡入动画 (function() { const style = document.createElement('style'); style.textContent = ` @keyframes fadeIn { from { opacity: 0; transform: scale(0.95); } to { opacity: 1; transform: scale(1); } } `; document.head.appendChild(style); })(); // ============================================================ // 🚀 初始化 // ============================================================ async function init() { applyTheme(); document.getElementById('forumName').textContent = CONFIG.FORUM_NAME || 'Forumlify'; refreshCaptcha('reg'); refreshCaptcha('post'); refreshCaptcha('reply'); if (token) { try { const user = await API.getMe(); currentUser = user; } catch (e) { token = null; localStorage.removeItem('forumlify-token'); } } renderNav(); await loadForumName(); await loadCustomPagesNav(); renderStats(); renderLinks(); const urlParams = new URLSearchParams(window.location.search); const postParam = urlParams.get('post'); const pageParam = urlParams.get('page'); const userParam = urlParams.get('user'); const postPageParam = urlParams.get('postpage'); const customParam = urlParams.get('custom'); if (postPageParam) { currentPageNum = parseInt(postPageParam) || 1; } if (customParam) { showCustomPage(customParam); } else if (userParam) { showUserPage(userParam); } else if (postParam) { showPostPage(postParam); } else if (pageParam && ['messages', 'settings', 'admin', 'new'].includes(pageParam)) { if (pageParam === 'admin' && currentUser?.role !== 'admin') { switchPage('feed'); } else { switchPage(pageParam); } } else { switchPage('feed'); } // ============================================================ // 绑定所有事件 // ============================================================ const themeToggle = document.getElementById('themeToggle'); if (themeToggle) { themeToggle.addEventListener('click', function(e) { e.preventDefault(); e.stopPropagation(); toggleTheme(e); document.getElementById('dropdownMenu').classList.remove('show'); }); } document.getElementById('avatarImg').addEventListener('click', function(e) { e.stopPropagation(); document.getElementById('dropdownMenu').classList.toggle('show'); }); document.addEventListener('click', function() { document.getElementById('dropdownMenu').classList.remove('show'); }); document.querySelectorAll('[data-page]').forEach(el => { el.addEventListener('click', function(e) { e.preventDefault(); const page = this.dataset.page; if (page === 'admin' && currentUser?.role !== 'admin') { alert('无权限访问'); return; } document.getElementById('dropdownMenu').classList.remove('show'); switchPage(page); }); }); document.querySelectorAll('.back-btn').forEach(btn => { btn.addEventListener('click', function() { switchPage('feed'); }); }); document.getElementById('forumName').addEventListener('click', function() { switchPage('feed'); }); // ===== 忘记密码 ===== const forgotLink = document.getElementById('forgotPasswordLink'); if (forgotLink) { forgotLink.addEventListener('click', function(e) { e.preventDefault(); document.getElementById('forgotPasswordModal').classList.add('active'); document.getElementById('resetStatus').textContent = ''; }); } document.getElementById('resetPasswordSubmit').addEventListener('click', async function() { const email = document.getElementById('resetEmail').value.trim(); const code = document.getElementById('resetRecoveryCode').value.trim().toUpperCase(); const newPassword = document.getElementById('resetNewPassword').value; const statusEl = document.getElementById('resetStatus'); if (!email || !code || !newPassword) { statusEl.textContent = '请填写完整信息'; statusEl.style.color = '#ef4444'; return; } if (newPassword.length < 6) { statusEl.textContent = '密码至少6位'; statusEl.style.color = '#ef4444'; return; } try { await API.resetPassword(email, code, newPassword); statusEl.textContent = getIcon('success') + ' 重置成功!请登录'; statusEl.style.color = '#22c55e'; setTimeout(() => { document.getElementById('forgotPasswordModal').classList.remove('active'); document.getElementById('resetEmail').value = ''; document.getElementById('resetRecoveryCode').value = ''; document.getElementById('resetNewPassword').value = ''; }, 1500); } catch (err) { statusEl.textContent = getIcon('error') + ' ' + err.message; statusEl.style.color = '#ef4444'; } }); // ===== 发帖 ===== document.getElementById('fab').addEventListener('click', () => { if (!currentUser) { alert('请先登录'); return; } switchPage('new'); }); document.getElementById('postSubmit').addEventListener('click', async () => { if (!currentUser || !currentUser.id) { alert('请先登录'); switchPage('feed'); return; } const title = document.getElementById('postTitle').value.trim() || '无标题'; const content = document.getElementById('postContent').value.trim(); const captchaInput = document.getElementById('postCaptchaInput').value.trim(); const captchaAnswer = parseInt(document.getElementById('postCaptchaInput').dataset.answer); if (!content) { alert('请填写内容'); return; } if (parseInt(captchaInput) !== captchaAnswer) { alert('验证码错误,请重新计算'); refreshCaptcha('post'); return; } const images = []; document.querySelectorAll('#imagePreview img').forEach(img => { images.push(img.src); }); try { await API.createPost(title, content, images); alert('发布成功!'); switchPage('feed'); renderFeed(); renderStats(); } catch (err) { alert('发布失败:' + err.message); } }); document.getElementById('postCaptchaQuestion').addEventListener('click', function() { refreshCaptcha('post'); }); document.getElementById('regCaptchaQuestion').addEventListener('click', function() { refreshCaptcha('reg'); }); // ===== 拖拽上传 ===== const dropZone = document.getElementById('dropZone'); const fileInput = document.getElementById('fileInput'); const dropZoneText = document.getElementById('dropZoneText'); if (dropZone && fileInput) { dropZone.addEventListener('click', function() { fileInput.click(); }); fileInput.addEventListener('change', function() { handleImageFiles(this.files); }); dropZone.addEventListener('dragover', function(e) { e.preventDefault(); this.style.borderColor = 'var(--primary)'; this.style.background = 'var(--primary-bg)'; if (dropZoneText) dropZoneText.textContent = '松开上传'; }); dropZone.addEventListener('dragleave', function(e) { e.preventDefault(); this.style.borderColor = 'var(--border)'; this.style.background = 'var(--bg)'; if (dropZoneText) dropZoneText.textContent = '点击或拖拽上传图片'; }); dropZone.addEventListener('drop', function(e) { e.preventDefault(); this.style.borderColor = 'var(--border)'; this.style.background = 'var(--bg)'; if (dropZoneText) dropZoneText.textContent = '点击或拖拽上传图片'; handleImageFiles(e.dataTransfer.files); }); } // ===== 举报 ===== document.getElementById('reportSubmit').addEventListener('click', async () => { if (!reportTargetPostId) return; const reason = document.getElementById('reportReason').value; try { await API.createReport(reportTargetPostId, reason); document.getElementById('reportModal').classList.remove('active'); alert('举报已提交,管理员将尽快处理'); reportTargetPostId = null; } catch (err) { alert('举报失败:' + err.message); } }); // ===== 模态框关闭 ===== document.querySelectorAll('.modal .close').forEach(btn => { btn.addEventListener('click', function() { document.getElementById(this.dataset.modal).classList.remove('active'); }); }); document.querySelectorAll('.modal').forEach(m => { m.addEventListener('click', function(e) { if (e.target === this) this.classList.remove('active'); }); }); // ===== 前进后退 ===== window.addEventListener('popstate', function(e) { const state = e.state || {}; const page = state.page || 'feed'; const postId = state.postId || null; const username = state.username || null; const custom = state.custom || null; if (postId) { showPostPage(postId); } else if (username) { showUserPage(username); } else if (custom) { showCustomPage(custom); } else { switchPage(page); } }); // ===== 私信按钮 ===== const messageBtn = document.getElementById('messageBtn'); if (messageBtn) { const newBtn = messageBtn.cloneNode(true); messageBtn.parentNode.replaceChild(newBtn, messageBtn); newBtn.addEventListener('click', function(e) { e.stopPropagation(); if (!currentUser) { alert('请先登录'); return; } openMessageList(); }); } const closeMessageListBtn = document.querySelector('#messageListModal .close'); if (closeMessageListBtn) { closeMessageListBtn.addEventListener('click', closeMessageList); } const closeChatBtn = document.querySelector('#chatModal .close'); if (closeChatBtn) { closeChatBtn.addEventListener('click', closeChat); } const chatInput = document.getElementById('chatInput'); if (chatInput) { chatInput.addEventListener('keydown', function(e) { if (e.key === 'Enter') { e.preventDefault(); sendMessage(); } }); } document.querySelectorAll('.modal').forEach(m => { m.addEventListener('click', function(e) { if (e.target === this) { this.classList.remove('active'); if (this.id === 'messageListModal') { closeMessageList(); } if (this.id === 'chatModal') { closeChat(); } } }); }); } document.addEventListener('DOMContentLoaded', init);