// ============================================================
// 🛡️ 管理后台
// ============================================================
function renderAdminReports() {
const container = document.getElementById('adminContent');
container.innerHTML = '
加载中...
';
API.getReports().then(reports => {
if (!reports || reports.length === 0) {
container.innerHTML = '
${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 = '加载中...
';
API.getUsers().then(users => {
let html = `
加载中...
';
API.getEventLogs().then(logs => {
if (!logs || logs.length === 0) {
container.innerHTML = '加载中...
';
API.getAdminCustomPages().then(pages => {
if (pages.length === 0) {
container.innerHTML = '
×
${isEdit ? '✏️ 编辑页面' : '📄 添加页面'}
${isEdit ? '
⚠️ 名称不可修改
' : '
只允许字母、数字、短横线和下划线
'}
支持 HTML、CSS(<style>)、JS(<script>),内容会在独立的沙盒中渲染
`;
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]();
});
});