// ============================================================ // 🛡️ 管理后台 // ============================================================ function renderAdminReports() { const container = document.getElementById('adminContent'); container.innerHTML = '
加载中...
'; API.getReports().then(reports => { if (!reports || reports.length === 0) { container.innerHTML = '
✅ 暂无举报
'; return; } let html = ''; const statusMap = { pending: '⏳ 待处理', approved: '✅ 已删除', rejected: '❌ 已驳回' }; reports.forEach(r => { const postTitle = r.post_title || '无标题'; html += `
${r.reporter_name || '匿名'} 举报了帖子
原因:${r.reason}
帖子:${postTitle} — ${(r.post_content || '').substring(0, 30)}${(r.post_content || '').length > 30 ? '...' : ''}
状态:${statusMap[r.status] || r.status}
${r.handler_name ? `
处理人:${r.handler_name}${r.handler_note ? ' (' + r.handler_note + ')' : ''}
` : ''} ${r.status === 'pending' ? `
` : ''}
`; }); container.innerHTML = html; container.querySelectorAll('[data-action]').forEach(btn => { btn.addEventListener('click', function() { const id = this.dataset.reportid; const action = this.dataset.action; const status = action === 'approve' ? 'approved' : 'rejected'; const note = action === 'approve' ? '已删除违规帖子' : '举报不成立'; if (action === 'approve' && !confirm('确定要删除该帖子并标记举报为已处理吗?')) return; API.updateReport(id, status, note).then(() => { if (action === 'approve') { const report = reports.find(r => r.id === id); if (report && report.post_id) { API.deletePost(report.post_id).catch(() => {}); } } renderAdminReports(); }).catch(err => alert('操作失败:' + err.message)); }); }); }).catch(() => { container.innerHTML = '
加载失败
'; }); } function renderAdminUsers() { const container = document.getElementById('adminContent'); container.innerHTML = '
加载中...
'; API.getUsers().then(users => { let html = `
共 ${users.length} 位用户
`; users.forEach(u => { const isAdmin = u.role === 'admin'; const isCurrentUser = currentUser && currentUser.id === u.id; html += ` `; }); html += '
用户 角色 注册时间 操作
${u.username} ${isCurrentUser ? '你' : ''}
${isAdmin ? '管理员' : '普通用户'} ${u.created_at ? new Date(u.created_at).toLocaleDateString('zh-CN') : '—'} ${isCurrentUser ? '不可操作自己' : (isAdmin ? `` : `` ) }
'; container.innerHTML = html; container.querySelectorAll('[data-role]').forEach(btn => { btn.addEventListener('click', function() { const userId = this.dataset.userid; const role = this.dataset.role; const roleName = role === 'admin' ? '管理员' : '普通用户'; if (!confirm(`确定要将该用户设为「${roleName}」吗?`)) return; API.updateUserRole(userId, role).then(() => { renderAdminUsers(); }).catch(err => alert('操作失败:' + err.message)); }); }); }).catch(() => { container.innerHTML = '
加载失败
'; }); } function renderAdminLogs() { const container = document.getElementById('adminContent'); container.innerHTML = '
加载中...
'; API.getEventLogs().then(logs => { if (!logs || logs.length === 0) { container.innerHTML = '
暂无日志
'; return; } let html = ''; logs.forEach(l => { html += ` `; }); html += '
时间用户操作
${new Date(l.created_at).toLocaleString('zh-CN')} ${l.username || '系统'} ${l.action}
'; container.innerHTML = html; }).catch(() => { container.innerHTML = '
加载失败
'; }); } function renderAdminLinks() { const container = document.getElementById('adminContent'); container.innerHTML = `
`; function loadLinks() { API.getLinks().then(links => { const ul = document.getElementById('linkList'); if (!links || links.length === 0) { ul.innerHTML = '
暂无友情链接
'; return; } let html = ''; links.forEach(l => { html += `
${l.title}
`; }); ul.innerHTML = html; ul.querySelectorAll('[data-linkid]').forEach(btn => { btn.addEventListener('click', function() { if (!confirm('确定删除该链接吗?')) return; API.deleteLink(this.dataset.linkid).then(() => { loadLinks(); }).catch(err => alert('删除失败:' + err.message)); }); }); }).catch(() => {}); } loadLinks(); document.getElementById('addLinkBtn').addEventListener('click', function() { const title = document.getElementById('newLinkTitle').value.trim(); const url = document.getElementById('newLinkUrl').value.trim(); if (!title || !url) { alert('请填写完整信息'); return; } API.addLink(title, url).then(() => { document.getElementById('newLinkTitle').value = ''; document.getElementById('newLinkUrl').value = ''; loadLinks(); }).catch(err => alert('添加失败:' + err.message)); }); } function renderAdminSettings() { const container = document.getElementById('adminContent'); container.innerHTML = `

⚙️ 论坛设置

`; API.getSettings().then(data => { document.getElementById('settingsForumName').value = data.forum_name || 'Forumlify'; }).catch(() => {}); document.getElementById('settingsForumSave').addEventListener('click', async () => { const name = document.getElementById('settingsForumName').value.trim(); if (!name) { alert('请输入论坛名称'); return; } try { await API.updateSettings(name); document.getElementById('settingsResult').textContent = '✅ 保存成功!'; document.getElementById('settingsResult').style.color = '#22c55e'; document.getElementById('forumName').textContent = name; document.title = name; const titleEl = document.getElementById('pageTitle'); if (titleEl) titleEl.textContent = name; } catch (err) { document.getElementById('settingsResult').textContent = '❌ 保存失败'; document.getElementById('settingsResult').style.color = '#ef4444'; } }); } // ============================================================ // 📄 自定义页面管理 // ============================================================ let editingPageId = null; function renderAdminCustomPages() { const container = document.getElementById('adminContent'); container.innerHTML = `

📄 自定义页面

`; loadCustomPageList(); document.getElementById('addCustomPageBtn').addEventListener('click', function() { editingPageId = null; openCustomPageEditor(); }); } function loadCustomPageList() { const container = document.getElementById('customPageList'); container.innerHTML = '
加载中...
'; API.getAdminCustomPages().then(pages => { if (pages.length === 0) { container.innerHTML = '
暂无自定义页面
'; return; } let html = `
`; pages.forEach(p => { html += ` `; }); html += '
名称 标题 URL 排序 状态 操作
${p.name} ${p.title} ?custom=${p.name} ${p.sort_order} ${p.enabled ? '✅ 启用' : '❌ 禁用'}
'; container.innerHTML = html; container.querySelectorAll('[data-action="edit"]').forEach(btn => { btn.addEventListener('click', function() { const page = pages.find(p => p.id === this.dataset.id); if (page) { editingPageId = page.id; openCustomPageEditor(page); } }); }); container.querySelectorAll('[data-action="delete"]').forEach(btn => { btn.addEventListener('click', function() { if (!confirm('确定要删除这个页面吗?')) return; API.deleteCustomPage(this.dataset.id).then(() => { loadCustomPageList(); loadCustomPagesNav(); }).catch(err => alert('删除失败:' + err.message)); }); }); }).catch(err => { container.innerHTML = '
加载失败
'; }); } function openCustomPageEditor(page) { const isEdit = !!page; 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('#editorSaveBtn').addEventListener('click', async function() { const name = document.getElementById('editorPageName').value.trim(); const title = document.getElementById('editorPageTitle').value.trim(); const content = document.getElementById('editorContent').value.trim(); const sort_order = parseInt(document.getElementById('editorSortOrder').value) || 0; const enabled = document.getElementById('editorEnabled').value === 'true'; if (!name) { alert('请输入页面名称'); return; } if (!title) { alert('请输入导航栏显示名称'); return; } if (!content) { alert('请输入页面内容'); return; } if (!/^[a-zA-Z0-9\-_]+$/.test(name)) { alert('页面名称只允许字母、数字、短横线和下划线'); return; } try { if (isEdit) { await API.updateCustomPage(page.id, title, content, sort_order, enabled); } else { await API.createCustomPage(name, title, content, sort_order); } modal.remove(); loadCustomPageList(); loadCustomPagesNav(); } catch (err) { alert('保存失败:' + err.message); } }); } // ============================================================ // 管理后台Tab切换 // ============================================================ document.querySelectorAll('.admin-tab').forEach(tab => { tab.addEventListener('click', function() { document.querySelectorAll('.admin-tab').forEach(t => t.classList.remove('active')); this.classList.add('active'); const tabMap = { reports: renderAdminReports, users: renderAdminUsers, logs: renderAdminLogs, links: renderAdminLinks, settings: renderAdminSettings, custom: renderAdminCustomPages }; if (tabMap[this.dataset.tab]) tabMap[this.dataset.tab](); }); });