feat: 后台管理功能增强

1. 前端用户下拉菜单优化 - 改用CSS动画,解决鼠标移动问题
2. 帖子管理增加显示/隐藏开关
3. 帖子详情页可直接删除回复
4. 新增回复管理页面(列表、删除)
5. 用户管理增加编辑功能(用户名、邮箱、手机、简介)
6. 用户管理增加查看帖子按钮
7. 所有页面侧边栏添加回复管理入口
8. 数据库增加 is_hidden 字段
This commit is contained in:
2026-04-12 18:16:36 +08:00
parent e7ebc3a4d6
commit cd087e9931
9 changed files with 458 additions and 48 deletions

View File

@@ -31,6 +31,9 @@
<a href="/admin/posts" class="flex items-center gap-3 px-6 py-3 text-slate-300 hover:bg-slate-700 hover:text-white">
<i class="ri-file-text-line"></i><span>帖子管理</span>
</a>
<a href="/admin/replies" class="flex items-center gap-3 px-6 py-3 text-slate-300 hover:bg-slate-700 hover:text-white">
<i class="ri-chat-3-line"></i><span>回复管理</span>
</a>
<a href="/admin/topics" class="flex items-center gap-3 px-6 py-3 text-slate-300 hover:bg-slate-700 hover:text-white">
<i class="ri-tools-line"></i><span>主题管理</span>
</a>

View File

@@ -19,7 +19,7 @@
<i class="ri-shield-keyhole-line text-3xl text-white"></i>
</div>
<h1 class="text-2xl font-bold text-gray-800">后台管理系统</h1>
<p class="text-gray-500 text-sm mt-2">管理员账号: admin / admin123</p>
<p class="text-gray-500 text-sm mt-2">用户管理员账号: admin / admin123</p>
</div>
<form id="loginForm" class="space-y-6">

View File

@@ -27,6 +27,9 @@
<a href="/admin/posts" class="flex items-center gap-3 px-6 py-3 bg-slate-700 text-white">
<i class="ri-file-text-line"></i><span>帖子管理</span>
</a>
<a href="/admin/replies" class="flex items-center gap-3 px-6 py-3 text-slate-300 hover:bg-slate-700 hover:text-white">
<i class="ri-chat-3-line"></i><span>回复管理</span>
</a>
<a href="/admin/topics" class="flex items-center gap-3 px-6 py-3 text-slate-300 hover:bg-slate-700 hover:text-white">
<i class="ri-tools-line"></i><span>主题管理</span>
</a>
@@ -55,17 +58,18 @@
<table class="w-full">
<thead class="bg-gray-50 border-b border-gray-100">
<tr>
<th class="px-6 py-3 text-left text-sm font-medium text-gray-500">标题</th>
<th class="px-6 py-3 text-left text-sm font-medium text-gray-500">类型</th>
<th class="px-6 py-3 text-left text-sm font-medium text-gray-500">作者</th>
<th class="px-6 py-3 text-left text-sm font-medium text-gray-500">浏览/赞/回复</th>
<th class="px-6 py-3 text-left text-sm font-medium text-gray-500">状态</th>
<th class="px-6 py-3 text-left text-sm font-medium text-gray-500">发布时间</th>
<th class="px-6 py-3 text-left text-sm font-medium text-gray-500">操作</th>
<th class="px-4 py-3 text-left text-sm font-medium text-gray-500">标题</th>
<th class="px-4 py-3 text-left text-sm font-medium text-gray-500">类型</th>
<th class="px-4 py-3 text-left text-sm font-medium text-gray-500">作者</th>
<th class="px-4 py-3 text-left text-sm font-medium text-gray-500">浏览/赞/回复</th>
<th class="px-4 py-3 text-left text-sm font-medium text-gray-500">状态</th>
<th class="px-4 py-3 text-left text-sm font-medium text-gray-500">显示</th>
<th class="px-4 py-3 text-left text-sm font-medium text-gray-500">发布时间</th>
<th class="px-4 py-3 text-left text-sm font-medium text-gray-500">操作</th>
</tr>
</thead>
<tbody id="postTable">
<tr><td colspan="7" class="px-6 py-8 text-center text-gray-500">加载中...</td></tr>
<tr><td colspan="8" class="px-6 py-8 text-center text-gray-500">加载中...</td></tr>
</tbody>
</table>
</div>
@@ -110,38 +114,44 @@
const tbody = document.getElementById('postTable');
if (posts.length === 0) {
tbody.innerHTML = '<tr><td colspan="7" class="px-6 py-8 text-center text-gray-500">暂无帖子</td></tr>';
tbody.innerHTML = '<tr><td colspan="8" class="px-6 py-8 text-center text-gray-500">暂无帖子</td></tr>';
return;
}
tbody.innerHTML = posts.map(p => `
<tr class="border-b border-gray-50 hover:bg-gray-50">
<td class="px-6 py-4">
<td class="px-4 py-3">
<p class="font-medium text-gray-800 truncate max-w-xs">${p.title}</p>
</td>
<td class="px-6 py-4">
<td class="px-4 py-3">
<span class="px-2 py-1 rounded text-xs ${p.type === 'discussion' ? 'bg-blue-100 text-blue-600' : 'bg-purple-100 text-purple-600'}">
${p.type === 'discussion' ? '技术交流' : '工具分享'}
${p.type === 'discussion' ? '交流' : '分享'}
</span>
</td>
<td class="px-6 py-4 text-gray-600">${p.author}</td>
<td class="px-6 py-4 text-sm text-gray-500">
<td class="px-4 py-3 text-gray-600">${p.author}</td>
<td class="px-4 py-3 text-sm text-gray-500">
${p.views} / ${p.likes} / ${p.replies}
</td>
<td class="px-6 py-4">
<td class="px-4 py-3">
${p.is_pinned ? '<span class="px-2 py-1 bg-red-100 text-red-600 rounded text-xs">置顶</span>' : '-'}
</td>
<td class="px-6 py-4 text-sm text-gray-500">
<td class="px-4 py-3">
<button onclick="toggleHide('${p.id}', ${p.is_hidden || 0})"
class="px-2 py-1 rounded text-xs ${p.is_hidden ? 'bg-gray-100 text-gray-600' : 'bg-green-100 text-green-600'}">
${p.is_hidden ? '已隐藏' : '显示'}
</button>
</td>
<td class="px-4 py-3 text-sm text-gray-500">
${new Date(p.created_at).toLocaleDateString()}
</td>
<td class="px-6 py-4">
<button onclick="viewPost('${p.id}')" class="text-blue-500 hover:text-blue-700 mr-3">
<td class="px-4 py-3 whitespace-nowrap">
<button onclick="viewPost('${p.id}')" class="text-blue-500 hover:text-blue-700 mr-2" title="查看">
<i class="ri-eye-line"></i>
</button>
<button onclick="pinPost('${p.id}')" class="text-yellow-500 hover:text-yellow-700 mr-3">
<button onclick="pinPost('${p.id}')" class="text-yellow-500 hover:text-yellow-700 mr-2" title="置顶">
<i class="ri-pushpin-line"></i>
</button>
<button onclick="deletePost('${p.id}')" class="text-red-500 hover:text-red-700">
<button onclick="deletePost('${p.id}')" class="text-red-500 hover:text-red-700" title="删除">
<i class="ri-delete-bin-line"></i>
</button>
</td>
@@ -149,6 +159,15 @@
`).join('');
}
async function toggleHide(postId, currentHidden) {
const res = await fetch(`/admin/api/posts/${postId}/hide`, { method: 'POST' });
const data = await res.json();
if (data.success) {
loadPosts();
}
}
async function viewPost(postId) {
const res = await fetch(`/admin/api/posts/${postId}`);
const post = await res.json();
@@ -183,13 +202,18 @@
<p class="text-sm text-gray-500 mb-2">回复 (${post.replies.length})</p>
<div class="space-y-2 max-h-40 overflow-auto">
${post.replies.map(r => `
<div class="p-2 bg-gray-50 rounded text-sm">
<span class="font-medium">${r.author}</span>: ${r.content}
<div class="p-2 bg-gray-50 rounded text-sm flex justify-between items-start">
<div class="flex-1">
<span class="font-medium">${r.author}</span>: ${r.content}
</div>
<button onclick="deleteReply('${r.id}', '${postId}')" class="text-red-500 hover:text-red-700 ml-2">
<i class="ri-delete-bin-line"></i>
</button>
</div>
`).join('')}
</div>
</div>
` : ''}
` : '<p class="text-gray-500 text-sm">暂无回复</p>'}
</div>
`;
@@ -222,6 +246,17 @@
}
}
async function deleteReply(replyId, postId) {
if (!confirm('确定要删除这条回复吗?')) return;
const res = await fetch(`/admin/api/replies/${replyId}`, { method: 'DELETE' });
const data = await res.json();
if (data.success) {
viewPost(postId); // 重新加载帖子详情
}
}
loadPosts();
</script>
</body>

View File

@@ -0,0 +1,140 @@
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="icon" href="/favicon.svg" type="image/svg+xml">
<title>回复管理 - 论坛后台</title>
<script src="https://cdn.tailwindcss.com"></script>
<link href="https://cdn.jsdelivr.net/npm/remixicon@3.5.0/fonts/remixicon.css" rel="stylesheet">
</head>
<body class="bg-gray-50 min-h-screen">
<div class="flex">
<aside class="w-64 bg-slate-800 min-h-screen fixed left-0 top-0">
<div class="p-6">
<h1 class="text-white text-xl font-bold flex items-center gap-2">
<i class="ri-code-s-slash-line text-2xl text-blue-400"></i>
论坛后台
</h1>
</div>
<nav class="mt-6">
<a href="/admin" class="flex items-center gap-3 px-6 py-3 text-slate-300 hover:bg-slate-700 hover:text-white">
<i class="ri-dashboard-line"></i><span>仪表盘</span>
</a>
<a href="/admin/users" class="flex items-center gap-3 px-6 py-3 text-slate-300 hover:bg-slate-700 hover:text-white">
<i class="ri-user-line"></i><span>用户管理</span>
</a>
<a href="/admin/posts" class="flex items-center gap-3 px-6 py-3 text-slate-300 hover:bg-slate-700 hover:text-white">
<i class="ri-file-text-line"></i><span>帖子管理</span>
</a>
<a href="/admin/replies" class="flex items-center gap-3 px-6 py-3 bg-slate-700 text-white">
<i class="ri-chat-3-line"></i><span>回复管理</span>
</a>
<a href="/admin/topics" class="flex items-center gap-3 px-6 py-3 text-slate-300 hover:bg-slate-700 hover:text-white">
<i class="ri-tools-line"></i><span>主题管理</span>
</a>
</nav>
<div class="absolute bottom-0 left-0 right-0 p-4 border-t border-slate-700">
<a href="/" target="_blank" class="text-slate-400 hover:text-white text-sm flex items-center gap-2">
<i class="ri-external-link-line"></i> 访问前台
</a>
<button onclick="logout()" class="mt-2 text-slate-400 hover:text-red-400 text-sm flex items-center gap-2">
<i class="ri-logout-box-line"></i> 退出登录
</button>
</div>
</aside>
<main class="ml-64 flex-1 p-8">
<h1 class="text-2xl font-bold text-gray-800 mb-6">回复管理</h1>
<div class="bg-white rounded-xl shadow-sm border border-gray-100 overflow-hidden">
<table class="w-full">
<thead class="bg-gray-50 border-b border-gray-100">
<tr>
<th class="px-4 py-3 text-left text-sm font-medium text-gray-500">回复内容</th>
<th class="px-4 py-3 text-left text-sm font-medium text-gray-500">作者</th>
<th class="px-4 py-3 text-left text-sm font-medium text-gray-500">帖子</th>
<th class="px-4 py-3 text-left text-sm font-medium text-gray-500"></th>
<th class="px-4 py-3 text-left text-sm font-medium text-gray-500">回复时间</th>
<th class="px-4 py-3 text-left text-sm font-medium text-gray-500">操作</th>
</tr>
</thead>
<tbody id="replyTable">
<tr><td colspan="6" class="px-6 py-8 text-center text-gray-500">加载中...</td></tr>
</tbody>
</table>
</div>
</main>
</div>
<script>
// 检查登录状态
async function checkAuth() {
const res = await fetch('/admin/api/check-auth');
const data = await res.json();
if (!data.logged_in) {
window.location.href = '/admin/login';
}
}
checkAuth();
// 退出登录
async function logout() {
await fetch('/admin/api/logout', { method: 'POST' });
window.location.href = '/admin/login';
}
async function loadReplies() {
const res = await fetch('/admin/api/replies');
const replies = await res.json();
const tbody = document.getElementById('replyTable');
if (replies.length === 0) {
tbody.innerHTML = '<tr><td colspan="6" class="px-6 py-8 text-center text-gray-500">暂无回复</td></tr>';
return;
}
tbody.innerHTML = replies.map(r => `
<tr class="border-b border-gray-50 hover:bg-gray-50">
<td class="px-4 py-3">
<p class="text-gray-800 max-w-md">${r.content}</p>
</td>
<td class="px-4 py-3 text-gray-600">${r.author}</td>
<td class="px-4 py-3">
<a href="/post/${r.post_id}" target="_blank" class="text-blue-600 hover:text-blue-800">
${r.post_title}
</a>
</td>
<td class="px-4 py-3">
<span class="px-2 py-1 bg-pink-100 text-pink-700 rounded text-xs">${r.likes}</span>
</td>
<td class="px-4 py-3 text-sm text-gray-500">
${new Date(r.created_at).toLocaleString()}
</td>
<td class="px-4 py-3">
<button onclick="deleteReply('${r.id}')" class="text-red-500 hover:text-red-700">
<i class="ri-delete-bin-line"></i> 删除
</button>
</td>
</tr>
`).join('');
}
async function deleteReply(replyId) {
if (!confirm('确定要删除这条回复吗?')) return;
const res = await fetch(`/admin/api/replies/${replyId}`, { method: 'DELETE' });
const data = await res.json();
if (data.success) {
loadReplies();
} else {
alert('删除失败: ' + (data.error || '未知错误'));
}
}
loadReplies();
</script>
</body>
</html>

View File

@@ -27,6 +27,9 @@
<a href="/admin/posts" class="flex items-center gap-3 px-6 py-3 text-slate-300 hover:bg-slate-700 hover:text-white">
<i class="ri-file-text-line"></i><span>帖子管理</span>
</a>
<a href="/admin/replies" class="flex items-center gap-3 px-6 py-3 text-slate-300 hover:bg-slate-700 hover:text-white">
<i class="ri-chat-3-line"></i><span>回复管理</span>
</a>
<a href="/admin/topics" class="flex items-center gap-3 px-6 py-3 bg-slate-700 text-white">
<i class="ri-tools-line"></i><span>主题管理</span>
</a>

View File

@@ -27,6 +27,9 @@
<a href="/admin/posts" class="flex items-center gap-3 px-6 py-3 text-slate-300 hover:bg-slate-700 hover:text-white">
<i class="ri-file-text-line"></i><span>帖子管理</span>
</a>
<a href="/admin/replies" class="flex items-center gap-3 px-6 py-3 text-slate-300 hover:bg-slate-700 hover:text-white">
<i class="ri-chat-3-line"></i><span>回复管理</span>
</a>
<a href="/admin/topics" class="flex items-center gap-3 px-6 py-3 text-slate-300 hover:bg-slate-700 hover:text-white">
<i class="ri-tools-line"></i><span>主题管理</span>
</a>
@@ -48,23 +51,63 @@
<table class="w-full">
<thead class="bg-gray-50 border-b border-gray-100">
<tr>
<th class="px-6 py-3 text-left text-sm font-medium text-gray-500">用户名</th>
<th class="px-6 py-3 text-left text-sm font-medium text-gray-500">邮箱</th>
<th class="px-6 py-3 text-left text-sm font-medium text-gray-500">手机</th>
<th class="px-6 py-3 text-left text-sm font-medium text-gray-500">帖子数</th>
<th class="px-6 py-3 text-left text-sm font-medium text-gray-500">回复</th>
<th class="px-6 py-3 text-left text-sm font-medium text-gray-500">注册时间</th>
<th class="px-6 py-3 text-left text-sm font-medium text-gray-500">操作</th>
<th class="px-4 py-3 text-left text-sm font-medium text-gray-500">用户名</th>
<th class="px-4 py-3 text-left text-sm font-medium text-gray-500">邮箱</th>
<th class="px-4 py-3 text-left text-sm font-medium text-gray-500">手机</th>
<th class="px-4 py-3 text-left text-sm font-medium text-gray-500">简介</th>
<th class="px-4 py-3 text-left text-sm font-medium text-gray-500">帖子</th>
<th class="px-4 py-3 text-left text-sm font-medium text-gray-500">回复数</th>
<th class="px-4 py-3 text-left text-sm font-medium text-gray-500">注册时间</th>
<th class="px-4 py-3 text-left text-sm font-medium text-gray-500">操作</th>
</tr>
</thead>
<tbody id="userTable">
<tr><td colspan="7" class="px-6 py-8 text-center text-gray-500">加载中...</td></tr>
<tr><td colspan="8" class="px-6 py-8 text-center text-gray-500">加载中...</td></tr>
</tbody>
</table>
</div>
</main>
</div>
<!-- 编辑用户弹窗 -->
<div id="editModal" class="fixed inset-0 bg-black/50 hidden items-center justify-center z-50 p-4">
<div class="bg-white rounded-xl w-full max-w-md overflow-hidden">
<div class="p-6 border-b flex justify-between items-center">
<h2 class="text-xl font-bold text-gray-800">编辑用户</h2>
<button onclick="closeEditModal()" class="text-gray-400"><i class="ri-close-line text-2xl"></i></button>
</div>
<div class="p-6">
<form id="editForm" class="space-y-4">
<input type="hidden" id="editUserId">
<div>
<label class="block text-sm font-medium text-gray-700 mb-1">用户名</label>
<input type="text" id="editUsername" class="w-full px-3 py-2 border border-gray-200 rounded-lg focus:ring-2 focus:ring-blue-500">
</div>
<div>
<label class="block text-sm font-medium text-gray-700 mb-1">邮箱</label>
<input type="email" id="editEmail" class="w-full px-3 py-2 border border-gray-200 rounded-lg focus:ring-2 focus:ring-blue-500">
</div>
<div>
<label class="block text-sm font-medium text-gray-700 mb-1">手机</label>
<input type="text" id="editPhone" class="w-full px-3 py-2 border border-gray-200 rounded-lg focus:ring-2 focus:ring-blue-500">
</div>
<div>
<label class="block text-sm font-medium text-gray-700 mb-1">简介</label>
<textarea id="editBio" rows="3" class="w-full px-3 py-2 border border-gray-200 rounded-lg focus:ring-2 focus:ring-blue-500"></textarea>
</div>
<div class="flex gap-3 pt-4">
<button type="submit" class="flex-1 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700">
保存修改
</button>
<button type="button" onclick="closeEditModal()" class="flex-1 py-2 border border-gray-300 text-gray-700 rounded-lg hover:bg-gray-50">
取消
</button>
</div>
</form>
</div>
</div>
</div>
<script>
// 检查登录状态
async function checkAuth() {
@@ -89,33 +132,93 @@
const tbody = document.getElementById('userTable');
if (users.length === 0) {
tbody.innerHTML = '<tr><td colspan="7" class="px-6 py-8 text-center text-gray-500">暂无用户</td></tr>';
tbody.innerHTML = '<tr><td colspan="8" class="px-6 py-8 text-center text-gray-500">暂无用户</td></tr>';
return;
}
tbody.innerHTML = users.map(u => `
<tr class="border-b border-gray-50 hover:bg-gray-50">
<td class="px-6 py-4 font-medium text-gray-800">${u.username}</td>
<td class="px-6 py-4 text-gray-600">${u.email || '-'}</td>
<td class="px-6 py-4 text-gray-600">${u.phone || '-'}</td>
<td class="px-6 py-4">
<td class="px-4 py-3">
<div class="flex items-center gap-2">
<img src="https://api.dicebear.com/7.x/avataaars/svg?seed=${u.username}" class="w-8 h-8 rounded-full">
<span class="font-medium text-gray-800">${u.username}</span>
</div>
</td>
<td class="px-4 py-3 text-gray-600">${u.email || '-'}</td>
<td class="px-4 py-3 text-gray-600">${u.phone || '-'}</td>
<td class="px-4 py-3 text-gray-600 truncate max-w-xs">${u.bio || '-'}</td>
<td class="px-4 py-3">
<span class="px-2 py-1 bg-blue-100 text-blue-700 rounded text-xs">${u.posts_count}</span>
</td>
<td class="px-6 py-4">
<td class="px-4 py-3">
<span class="px-2 py-1 bg-green-100 text-green-700 rounded text-xs">${u.replies_count}</span>
</td>
<td class="px-6 py-4 text-sm text-gray-500">
${u.created_at ? new Date(u.created_at).toLocaleString() : '-'}
<td class="px-4 py-3 text-sm text-gray-500">
${u.created_at ? new Date(u.created_at).toLocaleDateString() : '-'}
</td>
<td class="px-6 py-4">
<button onclick="deleteUser('${u.id}')" class="text-red-500 hover:text-red-700">
<i class="ri-delete-bin-line"></i> 删除
<td class="px-4 py-3 whitespace-nowrap">
<button onclick="editUser('${u.id}', '${u.username}', '${u.email || ''}', '${u.phone || ''}', '${u.bio || ''}')"
class="text-blue-500 hover:text-blue-700 mr-2" title="编辑">
<i class="ri-edit-line"></i>
</button>
<button onclick="viewUserPosts('${u.id}')" class="text-green-500 hover:text-green-700 mr-2" title="查看帖子">
<i class="ri-file-list-line"></i>
</button>
<button onclick="deleteUser('${u.id}')" class="text-red-500 hover:text-red-700" title="删除">
<i class="ri-delete-bin-line"></i>
</button>
</td>
</tr>
`).join('');
}
function editUser(id, username, email, phone, bio) {
document.getElementById('editUserId').value = id;
document.getElementById('editUsername').value = username;
document.getElementById('editEmail').value = email;
document.getElementById('editPhone').value = phone;
document.getElementById('editBio').value = bio;
document.getElementById('editModal').classList.remove('hidden');
document.getElementById('editModal').classList.add('flex');
}
function closeEditModal() {
document.getElementById('editModal').classList.add('hidden');
document.getElementById('editModal').classList.remove('flex');
}
document.getElementById('editForm').addEventListener('submit', async (e) => {
e.preventDefault();
const userId = document.getElementById('editUserId').value;
const data = {
username: document.getElementById('editUsername').value,
email: document.getElementById('editEmail').value,
phone: document.getElementById('editPhone').value,
bio: document.getElementById('editBio').value
};
const res = await fetch(`/admin/api/users/${userId}`, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data)
});
const result = await res.json();
if (result.success) {
closeEditModal();
loadUsers();
} else {
alert('保存失败: ' + (result.error || '未知错误'));
}
});
function viewUserPosts(userId) {
window.open(`/user/${userId}`, '_blank');
}
async function deleteUser(userId) {
if (!confirm('确定要删除这个用户吗?这将同时删除该用户的所有帖子!')) return;