diff --git a/js/app.js b/js/app.js index f896980..b41679e 100644 --- a/js/app.js +++ b/js/app.js @@ -123,9 +123,11 @@ function switchPage(page, param) { }); renderAdminReports(); } - if (page === 'settings' && currentUser) { - document.getElementById('settingsUsername').value = currentUser.username || ''; - document.getElementById('settingsBio').value = currentUser.bio || ''; + if (page === 'settings') { + renderSettingsPage(); + } + if (page === 'messages') { + renderMessagesPage(); } if (page === 'new') { document.getElementById('postTitle').value = ''; @@ -455,6 +457,132 @@ function showCustomPage(pageName) { }); } +// ============================================================ +// 📩 消息页面 +// ============================================================ + +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: '💬', + post_deleted: '🗑️', + report_handled: '🛡️', + system: '📢' + }; + notifications.forEach(n => { + const icon = typeMap[n.type] || '📌'; + const time = n.created_at ? new Date(n.created_at).toLocaleString('zh-CN') : ''; + html += ` +
+ ${icon} +
+
${n.title}
+
${n.content}
+ ${n.link ? `查看详情 →` : ''} +
${time}
+
+
+ `; + }); + container.innerHTML = html; + } catch (err) { + container.innerHTML = '
加载失败
'; + } +} + +// ============================================================ +// ⚙️ 设置页面 +// ============================================================ + +async function renderSettingsPage() { + const container = document.getElementById('settingsContent'); + if (!container) return; + + 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; + + container.innerHTML = ` +
+
+ +

${user.username}

+

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

+
+ 📅 加入 ${user.created_at ? new Date(user.created_at).toLocaleDateString('zh-CN') : '未知'} + 📝 ${postCount} 帖 + ${user.role === 'admin' ? '🛡️ 管理员' : ''} +
+
+ +
+
+ + +
+
+ + +
+ +
+
+ `; + + document.getElementById('settingsSaveBtn').addEventListener('click', async function() { + const username = document.getElementById('settingsUsername').value.trim(); + const bio = document.getElementById('settingsBio').value.trim(); + if (!username) { alert('用户名不能为空'); return; } + try { + const data = await apiFetch('/users/' + user.id, { + method: 'PUT', + body: JSON.stringify({ username, bio }) + }); + if (data.error) throw new Error(data.error); + currentUser.username = username; + currentUser.bio = bio; + alert('保存成功!'); + renderNav(); + renderSettingsPage(); + } catch (err) { + alert('保存失败:' + err.message); + } + }); + } catch (err) { + container.innerHTML = '
加载失败
'; + } +} + // ============================================================ // 🚀 初始化 // ============================================================ @@ -551,29 +679,6 @@ async function init() { }); }); - document.getElementById('settingsSave').addEventListener('click', async () => { - if (!currentUser) { alert('请先登录'); return; } - const username = document.getElementById('settingsUsername').value.trim(); - const bio = document.getElementById('settingsBio').value.trim(); - if (!username) { alert('用户名不能为空'); return; } - try { - const data = await apiFetch('/users/' + currentUser.id, { - method: 'PUT', - body: JSON.stringify({ username, bio }) - }); - if (data.error) throw new Error(data.error); - currentUser.username = username; - currentUser.bio = bio; - alert('保存成功!'); - renderNav(); - if (currentPage === 'feed') { - renderFeed(); - } - } catch (err) { - alert('保存失败:' + err.message); - } - }); - document.getElementById('forumName').addEventListener('click', function() { switchPage('feed'); });