// ============================================================ // 🛡️ 管理后台 // ============================================================ 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 = `

⚙️ 论坛设置


🎨 自定义 CSS

上传 style.css 覆盖默认样式,自定义论坛外观。

点击或拖拽上传 style.css

只能上传 style.css 文件

`; // 加载当前论坛名称 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'; } }); // 自定义 CSS 上传 const dropZone = document.getElementById('customCssDropZone'); const fileInput = document.getElementById('customCssInput'); const dropText = document.getElementById('customCssDropText'); const statusEl = document.getElementById('customCssStatus'); let selectedFile = null; if (dropZone && fileInput) { dropZone.addEventListener('click', function() { fileInput.click(); }); fileInput.addEventListener('change', function() { if (this.files.length > 0) { handleCssFile(this.files[0]); } }); dropZone.addEventListener('dragover', function(e) { e.preventDefault(); this.style.borderColor = 'var(--primary)'; this.style.background = 'var(--primary-bg)'; dropText.textContent = '松开上传'; }); dropZone.addEventListener('dragleave', function(e) { e.preventDefault(); this.style.borderColor = 'var(--border)'; this.style.background = 'var(--bg)'; dropText.textContent = '点击或拖拽上传 style.css'; }); dropZone.addEventListener('drop', function(e) { e.preventDefault(); this.style.borderColor = 'var(--border)'; this.style.background = 'var(--bg)'; dropText.textContent = '点击或拖拽上传 style.css'; if (e.dataTransfer.files.length > 0) { handleCssFile(e.dataTransfer.files[0]); } }); } function handleCssFile(file) { if (file.name !== 'style.css') { statusEl.textContent = '❌ 文件名必须是 style.css'; statusEl.style.color = '#ef4444'; selectedFile = null; fileInput.value = ''; return; } if (!file.type.includes('text/css') && !file.name.endsWith('.css')) { statusEl.textContent = '❌ 请上传 CSS 文件'; statusEl.style.color = '#ef4444'; selectedFile = null; fileInput.value = ''; return; } selectedFile = file; statusEl.textContent = '✅ 已选择: ' + file.name + ' (' + (file.size / 1024).toFixed(1) + ' KB)'; statusEl.style.color = '#22c55e'; } // 保存 CSS document.getElementById('customCssSaveBtn').addEventListener('click', function() { if (!selectedFile) { statusEl.textContent = '⚠️ 请先选择 style.css 文件'; statusEl.style.color = '#f59e0b'; return; } showCustomCssWarningModal(async function() { try { await API.uploadCustomCSS(selectedFile); statusEl.textContent = '✅ CSS 上传成功!刷新页面查看效果'; statusEl.style.color = '#22c55e'; selectedFile = null; fileInput.value = ''; dropText.textContent = '点击或拖拽上传 style.css'; loadCustomCSS(); } catch (err) { statusEl.textContent = '❌ 上传失败:' + err.message; statusEl.style.color = '#ef4444'; } }); }); // 删除 CSS document.getElementById('customCssDeleteBtn').addEventListener('click', async function() { if (!confirm('确定要删除自定义 CSS 吗?将恢复默认样式。')) return; try { await API.deleteCustomCSS(); statusEl.textContent = '✅ 已删除自定义 CSS'; statusEl.style.color = '#22c55e'; const link = document.getElementById('customCssLink'); if (link) link.remove(); } catch (err) { statusEl.textContent = '❌ 删除失败:' + err.message; statusEl.style.color = '#ef4444'; } }); } // ============================================================ // 自定义 CSS 警告模态框 // ============================================================ function showCustomCssWarningModal(onConfirm) { const modal = document.createElement('div'); modal.className = 'modal active'; modal.style.display = 'flex'; modal.innerHTML = ` `; document.body.appendChild(modal); modal.querySelector('#cssWarningConfirm').addEventListener('click', function() { modal.remove(); if (onConfirm) onConfirm(); }); modal.querySelector('#cssWarningCancel').addEventListener('click', function() { modal.remove(); }); modal.addEventListener('click', function(e) { if (e.target === this) modal.remove(); }); } // ============================================================ // 加载自定义 CSS // ============================================================ function loadCustomCSS() { const existing = document.getElementById('customCssLink'); if (existing) existing.remove(); const link = document.createElement('link'); link.id = 'customCssLink'; link.rel = 'stylesheet'; link.href = CONFIG.API_BASE_URL + '/custom-css?v=' + Date.now(); link.onerror = function() { // 如果自定义 CSS 不存在,静默失败 }; document.head.appendChild(link); } // ============================================================ // 管理后台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](); }); }); // 页面加载时尝试加载自定义 CSS document.addEventListener('DOMContentLoaded', function() { setTimeout(loadCustomCSS, 500); });