From 7170add9111a676ac8dfe9a1b8d26bf8cdfd23ed Mon Sep 17 00:00:00 2001 From: Furrium Network Date: Sun, 9 Aug 2026 18:21:08 +0800 Subject: [PATCH 1/2] Update server.js --- server.js | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/server.js b/server.js index 0759a0c..5993734 100644 --- a/server.js +++ b/server.js @@ -69,7 +69,7 @@ const admin = async (req, res, next) => { }; // ============================================================ -// 论坛设置接口(新增) +// 论坛设置接口 // ============================================================ // 获取论坛设置(公开) @@ -207,12 +207,19 @@ app.get('/api/auth/me', auth, async (req, res) => { // 用户管理 // ============================================================ -// 获取所有用户(管理员) +// 获取用户信息(支持按用户名查询) app.get('/api/users', auth, admin, async (req, res) => { try { - const r = await pool.query( - 'SELECT id, username, avatar_url, bio, role, created_at FROM users ORDER BY created_at DESC' - ); + let query = 'SELECT id, username, avatar_url, bio, role, created_at FROM users'; + const params = []; + + if (req.query.username) { + query += ' WHERE username = $1'; + params.push(req.query.username); + } + + query += ' ORDER BY created_at DESC'; + const r = await pool.query(query, params); res.json(r.rows); } catch (err) { res.status(500).json({ error: '服务器错误' }); @@ -265,12 +272,12 @@ app.put('/api/users/:id', auth, async (req, res) => { // 帖子接口 // ============================================================ -// 获取帖子列表 +// 获取帖子列表(支持按用户筛选) app.get('/api/posts', async (req, res) => { const sort = req.query.sort === 'hot' ? 'updated_at' : 'created_at'; try { - const r = await pool.query(` + let query = ` SELECT p.*, u.username, @@ -278,8 +285,17 @@ app.get('/api/posts', async (req, res) => { (SELECT COUNT(*) FROM replies WHERE post_id = p.id) as reply_count FROM posts p JOIN users u ON p.user_id = u.id - ORDER BY ${sort} DESC - `); + `; + const params = []; + + // 如果传了 user_id,筛选该用户的帖子 + if (req.query.user_id) { + query += ' WHERE p.user_id = $1'; + params.push(req.query.user_id); + } + + query += ` ORDER BY ${sort} DESC`; + const r = await pool.query(query, params); res.json(r.rows); } catch (err) { res.status(500).json({ error: '服务器错误' }); From d0df10df7f5a8c339e0e602e3e7fabb71169f6cf Mon Sep 17 00:00:00 2001 From: Furrium Network Date: Sun, 9 Aug 2026 18:22:10 +0800 Subject: [PATCH 2/2] Update user.js --- js/user.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/js/user.js b/js/user.js index 1b7ea5e..5a54008 100644 --- a/js/user.js +++ b/js/user.js @@ -7,6 +7,7 @@ async function renderUserProfile(username) { container.innerHTML = '
加载中...
'; try { + // 1. 获取用户信息(通过 username 参数) const users = await apiFetch('/users?username=' + encodeURIComponent(username)); const user = users && users.length > 0 ? users[0] : null; @@ -17,6 +18,7 @@ async function renderUserProfile(username) { document.getElementById('userPageTitle').textContent = '👤 ' + user.username; + // 2. 获取该用户的帖子(通过 user_id 筛选) 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'; @@ -69,4 +71,4 @@ function showUserPage(username) { el.style.animation = ''; currentPage = 'user'; renderUserProfile(username); -} \ No newline at end of file +}