From 93d971484a22de6d6feb0f9bf4a59a5a7cc14c3e Mon Sep 17 00:00:00 2001 From: Furrium Network Date: Sun, 9 Aug 2026 18:53:33 +0800 Subject: [PATCH 01/30] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BA=86=E6=97=A0?= =?UTF-8?q?=E6=B3=95=E5=88=87=E6=8D=A2=E4=BA=AE=E8=89=B2/=E6=9A=97?= =?UTF-8?q?=E8=89=B2=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- js/app.js | 45 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 3 deletions(-) diff --git a/js/app.js b/js/app.js index 93bf4b3..7c0c987 100644 --- a/js/app.js +++ b/js/app.js @@ -39,6 +39,8 @@ async function loadForumName() { function switchPage(page, param) { const url = new URL(window.location); + + // 用户主页跳转 if (page === 'user' && param) { url.searchParams.set('user', param); url.searchParams.delete('page'); @@ -47,6 +49,18 @@ function switchPage(page, param) { 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; + } + + // 其他页面(feed, settings, admin, new, messages) if (page === 'feed') { url.searchParams.delete('page'); url.searchParams.delete('post'); @@ -153,7 +167,22 @@ async function init() { switchPage('feed'); } - // 绑定事件(在 app.js 中绑) + // ============================================================ + // 绑定所有事件 + // ============================================================ + + // 1. 主题切换 + const themeToggle = document.getElementById('themeToggle'); + if (themeToggle) { + themeToggle.addEventListener('click', function(e) { + e.preventDefault(); + e.stopPropagation(); + toggleTheme(e); + document.getElementById('dropdownMenu').classList.remove('show'); + }); + } + + // 2. 头像下拉菜单 document.getElementById('avatarImg').addEventListener('click', function(e) { e.stopPropagation(); document.getElementById('dropdownMenu').classList.toggle('show'); @@ -162,6 +191,7 @@ async function init() { document.getElementById('dropdownMenu').classList.remove('show'); }); + // 3. 导航菜单页面切换 document.querySelectorAll('[data-page]').forEach(el => { el.addEventListener('click', function(e) { e.preventDefault(); @@ -175,12 +205,14 @@ async function init() { }); }); + // 4. 返回按钮 document.querySelectorAll('.back-btn').forEach(btn => { btn.addEventListener('click', function() { switchPage('feed'); }); }); + // 5. 设置保存 document.getElementById('settingsSave').addEventListener('click', async () => { if (!currentUser) { alert('请先登录'); return; } const username = document.getElementById('settingsUsername').value.trim(); @@ -204,16 +236,18 @@ async function init() { } }); + // 6. 点击论坛名回首页 document.getElementById('forumName').addEventListener('click', function() { switchPage('feed'); }); - // 发帖按钮 + // 7. 发帖按钮 document.getElementById('fab').addEventListener('click', () => { if (!currentUser) { alert('请先登录'); return; } switchPage('new'); }); + // 8. 发帖提交 document.getElementById('postSubmit').addEventListener('click', async () => { if (!currentUser || !currentUser.id) { alert('请先登录'); @@ -242,10 +276,12 @@ async function init() { } }); + // 9. 验证码刷新 document.getElementById('postCaptchaQuestion').addEventListener('click', function() { refreshCaptcha('post'); }); + // 10. 图片上传预览 document.getElementById('imageUpload').addEventListener('change', function() { const preview = document.getElementById('imagePreview'); preview.innerHTML = ''; @@ -261,6 +297,7 @@ async function init() { } }); + // 11. 举报提交 document.getElementById('reportSubmit').addEventListener('click', async () => { if (!reportTargetPostId) return; const reason = document.getElementById('reportReason').value; @@ -274,6 +311,7 @@ async function init() { } }); + // 12. 模态框关闭 document.querySelectorAll('.modal .close').forEach(btn => { btn.addEventListener('click', function() { document.getElementById(this.dataset.modal).classList.remove('active'); @@ -285,6 +323,7 @@ async function init() { }); }); + // 13. 浏览器前进后退 window.addEventListener('popstate', function(e) { const state = e.state || {}; const page = state.page || 'feed'; @@ -300,4 +339,4 @@ async function init() { }); } -document.addEventListener('DOMContentLoaded', init); \ No newline at end of file +document.addEventListener('DOMContentLoaded', init); From fd7d0c95b89577284c5a1a03f537e74fbd46a6a0 Mon Sep 17 00:00:00 2001 From: Furrium Network Date: Sun, 9 Aug 2026 18:59:54 +0800 Subject: [PATCH 02/30] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=A7=81=E4=BF=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- schema.sql | 3 +++ 1 file changed, 3 insertions(+) diff --git a/schema.sql b/schema.sql index f2a613e..3030cd8 100644 --- a/schema.sql +++ b/schema.sql @@ -85,6 +85,9 @@ END; $$ LANGUAGE plpgsql; DROP TRIGGER IF EXISTS update_posts_updated_at ON posts; + +cd /root/forumlify +nano schema.sql CREATE TRIGGER update_posts_updated_at BEFORE UPDATE ON posts FOR EACH ROW EXECUTE FUNCTION update_updated_at(); From 6489075781307b1a8df451b2aa3a081f8e4cdb4c Mon Sep 17 00:00:00 2001 From: Furrium Network Date: Sun, 9 Aug 2026 19:04:28 +0800 Subject: [PATCH 03/30] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=A7=81=E4=BF=A1API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server.js | 172 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 171 insertions(+), 1 deletion(-) diff --git a/server.js b/server.js index 5993734..13e4e29 100644 --- a/server.js +++ b/server.js @@ -288,7 +288,6 @@ app.get('/api/posts', async (req, res) => { `; const params = []; - // 如果传了 user_id,筛选该用户的帖子 if (req.query.user_id) { query += ' WHERE p.user_id = $1'; params.push(req.query.user_id); @@ -630,6 +629,177 @@ app.post('/api/upload', auth, upload.single('file'), (req, res) => { res.json({ url: '/uploads/' + req.file.filename }); }); +// ============================================================ +// 私信系统 +// ============================================================ + +// 获取会话列表 +app.get('/api/conversations', auth, async (req, res) => { + try { + const r = await pool.query(` + SELECT + c.id, + c.user1_id, + c.user2_id, + c.last_message_at, + c.created_at, + u.id as other_user_id, + u.username as other_username, + u.avatar_url as other_avatar_url, + (SELECT COUNT(*) FROM messages WHERE conversation_id = c.id AND sender_id != $1 AND is_read = false) as unread_count, + (SELECT content FROM messages WHERE conversation_id = c.id ORDER BY created_at DESC LIMIT 1) as last_message, + (SELECT created_at FROM messages WHERE conversation_id = c.id ORDER BY created_at DESC LIMIT 1) as last_message_time + FROM conversations c + JOIN users u ON (u.id = c.user1_id OR u.id = c.user2_id) AND u.id != $1 + WHERE c.user1_id = $1 OR c.user2_id = $1 + ORDER BY c.last_message_at DESC + `, [req.user.id]); + res.json(r.rows); + } catch (err) { + res.status(500).json({ error: '服务器错误' }); + } +}); + +// 获取或创建会话 +app.post('/api/conversations', auth, async (req, res) => { + const { other_user_id } = req.body; + if (!other_user_id) { + return res.status(400).json({ error: '缺少对方用户ID' }); + } + if (other_user_id === req.user.id) { + return res.status(400).json({ error: '不能与自己私信' }); + } + + try { + // 检查用户是否存在 + const userCheck = await pool.query('SELECT id FROM users WHERE id = $1', [other_user_id]); + if (userCheck.rows.length === 0) { + return res.status(404).json({ error: '用户不存在' }); + } + + // 查找已有会话 + const existing = await pool.query(` + SELECT id FROM conversations + WHERE (user1_id = $1 AND user2_id = $2) OR (user1_id = $2 AND user2_id = $1) + `, [req.user.id, other_user_id]); + + if (existing.rows.length > 0) { + return res.json({ id: existing.rows[0].id }); + } + + // 创建新会话 + const r = await pool.query(` + INSERT INTO conversations (user1_id, user2_id) + VALUES ($1, $2) + RETURNING id + `, [req.user.id, other_user_id]); + + res.json({ id: r.rows[0].id }); + } catch (err) { + res.status(500).json({ error: '服务器错误' }); + } +}); + +// 获取会话消息 +app.get('/api/conversations/:id/messages', auth, async (req, res) => { + const conversationId = req.params.id; + + try { + // 检查用户是否属于该会话 + const check = await pool.query(` + SELECT id FROM conversations + WHERE id = $1 AND (user1_id = $2 OR user2_id = $2) + `, [conversationId, req.user.id]); + + if (check.rows.length === 0) { + return res.status(403).json({ error: '无权限访问此会话' }); + } + + // 获取消息 + const r = await pool.query(` + SELECT + m.*, + u.username as sender_username, + u.avatar_url as sender_avatar_url + FROM messages m + JOIN users u ON m.sender_id = u.id + WHERE m.conversation_id = $1 + ORDER BY m.created_at ASC + `, [conversationId]); + + // 标记所有消息为已读 + await pool.query(` + UPDATE messages SET is_read = true + WHERE conversation_id = $1 AND sender_id != $2 AND is_read = false + `, [conversationId, req.user.id]); + + res.json(r.rows); + } catch (err) { + res.status(500).json({ error: '服务器错误' }); + } +}); + +// 发送消息 +app.post('/api/conversations/:id/messages', auth, async (req, res) => { + const conversationId = req.params.id; + const { content } = req.body; + + if (!content || content.trim().length === 0) { + return res.status(400).json({ error: '请填写消息内容' }); + } + + try { + // 检查用户是否属于该会话 + const check = await pool.query(` + SELECT id FROM conversations + WHERE id = $1 AND (user1_id = $2 OR user2_id = $2) + `, [conversationId, req.user.id]); + + if (check.rows.length === 0) { + return res.status(403).json({ error: '无权限访问此会话' }); + } + + // 插入消息 + const r = await pool.query(` + INSERT INTO messages (conversation_id, sender_id, content) + VALUES ($1, $2, $3) + RETURNING * + `, [conversationId, req.user.id, content]); + + // 更新会话最后消息时间 + await pool.query(` + UPDATE conversations SET last_message_at = NOW() + WHERE id = $1 + `, [conversationId]); + + // 获取发送者信息 + const userInfo = await pool.query(` + SELECT username, avatar_url FROM users WHERE id = $1 + `, [req.user.id]); + + res.json({ + ...r.rows[0], + sender_username: userInfo.rows[0].username, + sender_avatar_url: userInfo.rows[0].avatar_url + }); + } catch (err) { + res.status(500).json({ error: '发送失败,请稍后重试' }); + } +}); + +// 标记消息已读 +app.put('/api/messages/:id/read', auth, async (req, res) => { + try { + await pool.query(` + UPDATE messages SET is_read = true + WHERE id = $1 AND sender_id != $2 + `, [req.params.id, req.user.id]); + res.json({ success: true }); + } catch (err) { + res.status(500).json({ error: '服务器错误' }); + } +}); + // ============================================================ // 托管前端文件 // ============================================================ From e74c3c35eafa98603085a00b5f4354718636b445 Mon Sep 17 00:00:00 2001 From: Furrium Network Date: Sun, 9 Aug 2026 19:05:56 +0800 Subject: [PATCH 04/30] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=A7=81=E4=BF=A1API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- js/api.js | 79 ++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 69 insertions(+), 10 deletions(-) diff --git a/js/api.js b/js/api.js index 3285446..91c30e2 100644 --- a/js/api.js +++ b/js/api.js @@ -19,7 +19,9 @@ function apiFetch(path, options = {}) { } const API = { - // 认证 + // ============================================================ + // 认证 + // ============================================================ async login(email, password) { const data = await apiFetch('/auth/login', { method: 'POST', @@ -54,7 +56,9 @@ const API = { return data; }, - // 帖子 + // ============================================================ + // 帖子 + // ============================================================ async getPosts(sort) { const data = await apiFetch('/posts?sort=' + (sort || 'latest')); if (data.error) throw new Error(data.error); @@ -82,7 +86,9 @@ const API = { return data; }, - // 回复 + // ============================================================ + // 回复 + // ============================================================ async getReplies(postId) { const data = await apiFetch('/posts/' + postId + '/replies'); if (data.error) throw new Error(data.error); @@ -104,7 +110,9 @@ const API = { return data; }, - // 用户管理 + // ============================================================ + // 用户管理 + // ============================================================ async getUsers() { const data = await apiFetch('/users'); if (data.error) throw new Error(data.error); @@ -120,7 +128,9 @@ const API = { return data; }, - // 举报 + // ============================================================ + // 举报 + // ============================================================ async getReports() { const data = await apiFetch('/reports'); if (data.error) throw new Error(data.error); @@ -145,14 +155,18 @@ const API = { return data; }, - // 统计 + // ============================================================ + // 统计 + // ============================================================ async getStats() { const data = await apiFetch('/stats'); if (data.error) throw new Error(data.error); return data; }, - // 友情链接 + // ============================================================ + // 友情链接 + // ============================================================ async getLinks() { const data = await apiFetch('/links'); if (data.error) throw new Error(data.error); @@ -174,7 +188,9 @@ const API = { return data; }, - // 事件日志 + // ============================================================ + // 事件日志 + // ============================================================ async getEventLogs() { const data = await apiFetch('/event-logs'); if (data.error) throw new Error(data.error); @@ -190,7 +206,9 @@ const API = { } catch (e) { /* 静默失败 */ } }, - // 论坛设置 + // ============================================================ + // 论坛设置 + // ============================================================ async getSettings() { const data = await apiFetch('/settings'); if (data.error) throw new Error(data.error); @@ -204,5 +222,46 @@ const API = { }); if (data.error) throw new Error(data.error); return data; + }, + + // ============================================================ + // 私信 + // ============================================================ + async getConversations() { + const data = await apiFetch('/conversations'); + if (data.error) throw new Error(data.error); + return data || []; + }, + + async getOrCreateConversation(other_user_id) { + const data = await apiFetch('/conversations', { + method: 'POST', + body: JSON.stringify({ other_user_id }) + }); + if (data.error) throw new Error(data.error); + return data; + }, + + async getMessages(conversationId) { + const data = await apiFetch('/conversations/' + conversationId + '/messages'); + if (data.error) throw new Error(data.error); + return data || []; + }, + + async sendMessage(conversationId, content) { + const data = await apiFetch('/conversations/' + conversationId + '/messages', { + method: 'POST', + body: JSON.stringify({ content }) + }); + if (data.error) throw new Error(data.error); + return data; + }, + + async markMessageRead(messageId) { + const data = await apiFetch('/messages/' + messageId + '/read', { + method: 'PUT' + }); + if (data.error) throw new Error(data.error); + return data; } -}; \ No newline at end of file +}; From c17fc68ea365847fd7a83ffca8ed653facbe909e Mon Sep 17 00:00:00 2001 From: Furrium Network Date: Sun, 9 Aug 2026 19:06:55 +0800 Subject: [PATCH 05/30] Update theme.js --- js/theme.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/js/theme.js b/js/theme.js index b205a3c..48c9209 100644 --- a/js/theme.js +++ b/js/theme.js @@ -38,4 +38,9 @@ function applyTheme() { const theme = getTheme(); document.documentElement.setAttribute('data-theme', theme); updateThemeUI(theme); -} \ No newline at end of file +} + +// 确保 DOM 加载完成后执行 +document.addEventListener('DOMContentLoaded', function() { + applyTheme(); +}); From 4a1ed6c1399afede3d3885e39a934178c250e5e5 Mon Sep 17 00:00:00 2001 From: Furrium Network Date: Sun, 9 Aug 2026 19:07:15 +0800 Subject: [PATCH 06/30] Update captcha.js --- js/captcha.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/captcha.js b/js/captcha.js index 97f9aca..ad89972 100644 --- a/js/captcha.js +++ b/js/captcha.js @@ -25,4 +25,4 @@ function refreshCaptcha(type) { document.getElementById('replyCaptchaQuestion').textContent = q.question; document.getElementById('replyCaptchaInput').dataset.answer = q.answer; } -} \ No newline at end of file +} From 3b5ba899e82550ef22f7e19266602fa1059ed18d Mon Sep 17 00:00:00 2001 From: Furrium Network Date: Sun, 9 Aug 2026 19:07:42 +0800 Subject: [PATCH 07/30] Update auth.js --- js/auth.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/auth.js b/js/auth.js index 04b1646..89c8042 100644 --- a/js/auth.js +++ b/js/auth.js @@ -85,4 +85,4 @@ document.getElementById('logoutBtn').addEventListener('click', async () => { renderNav(); document.querySelectorAll('.page-slide').forEach(el => el.classList.remove('active')); switchPage('feed'); -}); \ No newline at end of file +}); From b4214359f6f002a12857b1bdae0957dacba9f3ab Mon Sep 17 00:00:00 2001 From: Furrium Network Date: Sun, 9 Aug 2026 19:08:09 +0800 Subject: [PATCH 08/30] Update feed.js --- js/feed.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/feed.js b/js/feed.js index abef2cb..7407952 100644 --- a/js/feed.js +++ b/js/feed.js @@ -125,4 +125,4 @@ document.querySelectorAll('.tab').forEach(tab => { renderFeed(); } }); -}); \ No newline at end of file +}); From c231f7684dd188135ed1dce196e44409b9009d60 Mon Sep 17 00:00:00 2001 From: Furrium Network Date: Sun, 9 Aug 2026 19:08:39 +0800 Subject: [PATCH 09/30] Update post.js --- js/post.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/post.js b/js/post.js index 630a766..25aeef2 100644 --- a/js/post.js +++ b/js/post.js @@ -178,4 +178,4 @@ async function handleReplySubmit(postId) { } catch (err) { alert('回复失败:' + err.message); } -} \ No newline at end of file +} From 6473a66265665b62d342b89f91cc85eec6f74df4 Mon Sep 17 00:00:00 2001 From: Furrium Network Date: Sun, 9 Aug 2026 19:09:42 +0800 Subject: [PATCH 10/30] Update admin.js --- js/admin.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/admin.js b/js/admin.js index ef751bf..98b0a9c 100644 --- a/js/admin.js +++ b/js/admin.js @@ -258,4 +258,4 @@ document.querySelectorAll('.admin-tab').forEach(tab => { }; if (tabMap[this.dataset.tab]) tabMap[this.dataset.tab](); }); -}); \ No newline at end of file +}); From e4d9e7dcd06695b1fc7bc1c01466bf1bcab03cc9 Mon Sep 17 00:00:00 2001 From: Furrium Network Date: Sun, 9 Aug 2026 19:09:56 +0800 Subject: [PATCH 11/30] Update user.js --- js/user.js | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/js/user.js b/js/user.js index 5a54008..50fea44 100644 --- a/js/user.js +++ b/js/user.js @@ -7,7 +7,7 @@ async function renderUserProfile(username) { container.innerHTML = '
加载中...
'; try { - // 1. 获取用户信息(通过 username 参数) + // 1. 获取用户信息 const users = await apiFetch('/users?username=' + encodeURIComponent(username)); const user = users && users.length > 0 ? users[0] : null; @@ -18,7 +18,7 @@ async function renderUserProfile(username) { document.getElementById('userPageTitle').textContent = '👤 ' + user.username; - // 2. 获取该用户的帖子(通过 user_id 筛选) + // 2. 获取该用户的帖子 const posts = await apiFetch('/posts?user_id=' + user.id); const avatar = user.avatar_url || 'https://ui-avatars.com/api/?name=' + encodeURIComponent(user.username) + '&background=6366f1&color=fff&size=128'; @@ -33,6 +33,12 @@ async function renderUserProfile(username) { 📝 发了 ${posts.length} 个帖子 ${user.role === 'admin' ? '🛡️ 管理员' : ''} + ${currentUser && currentUser.id !== user.id ? ` + + ` : ''}

📝 发布的帖子

`; @@ -54,6 +60,14 @@ async function renderUserProfile(username) { container.innerHTML = html; + // 绑定私信按钮 + const dmBtn = document.getElementById('dmBtn'); + if (dmBtn) { + dmBtn.addEventListener('click', function() { + openPrivateChat(user.id, user.username); + }); + } + } catch (err) { container.innerHTML = '
加载失败:' + err.message + '
'; } From fae95c5aa585cf424a8b34a0eb363b7c02fe1544 Mon Sep 17 00:00:00 2001 From: Furrium Network Date: Sun, 9 Aug 2026 19:10:14 +0800 Subject: [PATCH 12/30] Update app.js --- js/app.js | 334 +++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 253 insertions(+), 81 deletions(-) diff --git a/js/app.js b/js/app.js index 7c0c987..bc0aeb3 100644 --- a/js/app.js +++ b/js/app.js @@ -14,6 +14,8 @@ function renderNav() { 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'; @@ -121,6 +123,256 @@ function switchPage(page, param) { } } +// ============================================================ +// ✉️ 私信系统 +// ============================================================ + +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') : ''; + html += ` +
+ +
+
+ ${c.other_username} + ${time} +
+
+ ${lastMsg} + ${unread > 0 ? `${unread}` : ''} +
+
+
+ `; + }); + container.innerHTML = html; + } 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') : ''; + html += ` +
+ ${!isMine ? `` : ''} +
+
+ ${m.content} +
+
+ ${time} ${isMine ? (m.is_read ? '✓✓' : '✓') : ''} +
+
+
+ `; + }); + container.innerHTML = 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); + } +} + +// 绑定私信按钮事件 +document.addEventListener('DOMContentLoaded', function() { + const messageBtn = document.getElementById('messageBtn'); + if (messageBtn) { + messageBtn.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); + } + + // 聊天输入框 Enter 发送 + 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(); + } + } + }); + }); +}); + // ============================================================ // 🚀 初始化 // ============================================================ @@ -259,84 +511,4 @@ async function init() { 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); - API.logEvent('create_post').catch(() => {}); - alert('发布成功!'); - switchPage('feed'); - renderFeed(); - renderStats(); - } catch (err) { - alert('发布失败:' + err.message); - } - }); - - // 9. 验证码刷新 - document.getElementById('postCaptchaQuestion').addEventListener('click', function() { - refreshCaptcha('post'); - }); - - // 10. 图片上传预览 - document.getElementById('imageUpload').addEventListener('change', function() { - const preview = document.getElementById('imagePreview'); - preview.innerHTML = ''; - for (let file of this.files) { - 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 #e2e8f0;'; - preview.appendChild(img); - }; - reader.readAsDataURL(file); - } - }); - - // 11. 举报提交 - 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); - } - }); - - // 12. 模态框关闭 - 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'); - }); - }); - - // 13. 浏览器前进后退 - window.addEventListener('popstate', function(e) { - const state = e.state || {}; - const page = state.page || 'feed'; - const postId = state.postId || null; - const username = state.username || null; - if (postId) { - showPostPage(postId); - } else if (username) { - showUserPage(username); - } else { - switchPage(page); - } - }); -} - -document.addEventListener('DOMContentLoaded', init); + if (parseInt(captchaInput) !== captchaAnswer) { alert(' From e2d141d693e3a7ce1a58a9b144fc699e123bc951 Mon Sep 17 00:00:00 2001 From: Furrium Network Date: Sun, 9 Aug 2026 19:10:48 +0800 Subject: [PATCH 13/30] Update app.js --- js/app.js | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 96 insertions(+), 1 deletion(-) diff --git a/js/app.js b/js/app.js index bc0aeb3..8f62731 100644 --- a/js/app.js +++ b/js/app.js @@ -511,4 +511,99 @@ async function init() { const captchaInput = document.getElementById('postCaptchaInput').value.trim(); const captchaAnswer = parseInt(document.getElementById('postCaptchaInput').dataset.answer); if (!content) { alert('请填写内容'); return; } - if (parseInt(captchaInput) !== captchaAnswer) { alert(' + 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); + API.logEvent('create_post').catch(() => {}); + alert('发布成功!'); + switchPage('feed'); + renderFeed(); + renderStats(); + } catch (err) { + alert('发布失败:' + err.message); + } + }); + + // 9. 验证码刷新 + document.getElementById('postCaptchaQuestion').addEventListener('click', function() { + refreshCaptcha('post'); + }); + + // 10. 图片上传预览 + document.getElementById('imageUpload').addEventListener('change', function() { + const preview = document.getElementById('imagePreview'); + preview.innerHTML = ''; + for (let file of this.files) { + 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 #e2e8f0;'; + preview.appendChild(img); + }; + reader.readAsDataURL(file); + } + }); + + // 11. 举报提交 + 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); + } + }); + + // 12. 模态框关闭 + 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'); + }); + }); + + // 13. 浏览器前进后退 + window.addEventListener('popstate', function(e) { + const state = e.state || {}; + const page = state.page || 'feed'; + const postId = state.postId || null; + const username = state.username || null; + if (postId) { + showPostPage(postId); + } else if (username) { + showUserPage(username); + } else { + switchPage(page); + } + }); + + // 14. 私信按钮(已在上面的 DOMContentLoaded 中绑定) + // 但如果用户登录后动态加载,需要重新绑定 + // 这里再确保一次 + 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(); + }); + } +} + +document.addEventListener('DOMContentLoaded', init); From b86ea0269eb0be032250f4a232779e52f9d62001 Mon Sep 17 00:00:00 2001 From: Furrium Network Date: Sun, 9 Aug 2026 19:11:35 +0800 Subject: [PATCH 14/30] Update config.js --- config.js | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/config.js b/config.js index e989da7..e70744c 100644 --- a/config.js +++ b/config.js @@ -1,25 +1,23 @@ // ============================================================ // ⚙️ Forumlify 配置文件 -// 自托管版本 +// 自托管版本 —— 修改下面 API 地址即可 // ============================================================ const CONFIG = { - // ===== 必填 ===== - - + // 后端 API 地址 + // - Docker 部署:http://backend:3000/api + // - 本地开发:http://localhost:3000/api + // - 生产环境:https://你的域名/api API_BASE_URL: '/api', - // ===== 可选 ===== - // 论坛名称(显示在左上角) FORUM_NAME: 'Forumlify', // 是否开启发帖/注册的人机验证(10以内加减法) ENABLE_CAPTCHA: true, - // 可选:服务端监听端口(仅服务端读取,浏览器端忽略) - // 优先顺序:环境变量 PORT > 此处配置 > 默认 3000 - // 设为 null 则使用环境变量或默认值 + // 服务端监听端口(仅服务端读取,浏览器端忽略) + // 优先级:环境变量 PORT > SERVER_PORT > 默认 3000 SERVER_PORT: null, }; From f0cf4b173816ea5d856cefbc7d31f930c03787c1 Mon Sep 17 00:00:00 2001 From: Furrium Network Date: Sun, 9 Aug 2026 19:12:21 +0800 Subject: [PATCH 15/30] Update config.js --- config.js | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/config.js b/config.js index e70744c..3a4e139 100644 --- a/config.js +++ b/config.js @@ -4,16 +4,13 @@ // ============================================================ const CONFIG = { - // 后端 API 地址 - // - Docker 部署:http://backend:3000/api - // - 本地开发:http://localhost:3000/api - // - 生产环境:https://你的域名/api + API_BASE_URL: '/api', - // 论坛名称(显示在左上角) + // 论坛名称(显示在界面左上角和网站title) FORUM_NAME: 'Forumlify', - // 是否开启发帖/注册的人机验证(10以内加减法) + // 是否开启发帖/注册的人机验证(简单的加减法) ENABLE_CAPTCHA: true, // 服务端监听端口(仅服务端读取,浏览器端忽略) From e223407b556b6bcd23cdb8b6305f53f4337271ec Mon Sep 17 00:00:00 2001 From: Furrium Network Date: Sun, 9 Aug 2026 19:13:38 +0800 Subject: [PATCH 16/30] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86=E7=A7=81?= =?UTF-8?q?=E4=BF=A1=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.html | 53 ++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 46 insertions(+), 7 deletions(-) diff --git a/index.html b/index.html index 9bd37e0..b942693 100644 --- a/index.html +++ b/index.html @@ -20,6 +20,16 @@