新增后台管理系统
功能模块: - 仪表盘: 用户统计、帖子统计、帖子类型分布、热门标签、最新帖子 - 用户管理: 查看用户列表、删除用户 - 帖子管理: 查看/删除帖子、置顶功能、帖子详情预览 - 主题管理: 查看/删除主题、主题详情预览 端口: 19005
This commit is contained in:
329
admin/app.py
Normal file
329
admin/app.py
Normal file
@@ -0,0 +1,329 @@
|
||||
"""
|
||||
技术论坛 - 后台管理系统
|
||||
"""
|
||||
|
||||
from flask import Flask, render_template, jsonify, request
|
||||
from flask_cors import CORS
|
||||
import json
|
||||
from pathlib import Path
|
||||
from datetime import datetime
|
||||
|
||||
app = Flask(__name__)
|
||||
CORS(app)
|
||||
|
||||
# 数据目录
|
||||
DATA_DIR = Path(__file__).parent.parent / 'data'
|
||||
USERS_FILE = DATA_DIR / 'users.json'
|
||||
POSTS_FILE = DATA_DIR / 'posts.json'
|
||||
TOPICS_FILE = DATA_DIR / 'topics.json'
|
||||
|
||||
def load_users():
|
||||
if USERS_FILE.exists():
|
||||
return json.loads(USERS_FILE.read_text(encoding='utf-8'))
|
||||
return {}
|
||||
|
||||
def load_posts():
|
||||
if POSTS_FILE.exists():
|
||||
return json.loads(POSTS_FILE.read_text(encoding='utf-8'))
|
||||
return {}
|
||||
|
||||
def load_topics():
|
||||
if TOPICS_FILE.exists():
|
||||
return json.loads(TOPICS_FILE.read_text(encoding='utf-8'))
|
||||
return {}
|
||||
|
||||
def save_users(users):
|
||||
USERS_FILE.write_text(json.dumps(users, ensure_ascii=False, indent=2), encoding='utf-8')
|
||||
|
||||
def save_posts(posts):
|
||||
POSTS_FILE.write_text(json.dumps(posts, ensure_ascii=False, indent=2), encoding='utf-8')
|
||||
|
||||
def save_topics(topics):
|
||||
TOPICS_FILE.write_text(json.dumps(topics, ensure_ascii=False, indent=2), encoding='utf-8')
|
||||
|
||||
# ============ 页面路由 ============
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
return render_template('index.html')
|
||||
|
||||
@app.route('/users')
|
||||
def users_page():
|
||||
return render_template('users.html')
|
||||
|
||||
@app.route('/posts')
|
||||
def posts_page():
|
||||
return render_template('posts.html')
|
||||
|
||||
@app.route('/topics')
|
||||
def topics_page():
|
||||
return render_template('topics.html')
|
||||
|
||||
# ============ API路由 ============
|
||||
|
||||
@app.route('/api/stats')
|
||||
def api_stats():
|
||||
users = load_users()
|
||||
posts = load_posts()
|
||||
topics = load_topics()
|
||||
|
||||
# 统计
|
||||
total_messages = 0
|
||||
for post in posts.values():
|
||||
total_messages += len(post.get('replies', []))
|
||||
|
||||
today = datetime.now().strftime('%Y-%m-%d')
|
||||
today_posts = sum(1 for p in posts.values() if p.get('created_at', '').startswith(today))
|
||||
today_users = sum(1 for u in users.values() if u.get('created_at', '').startswith(today))
|
||||
|
||||
# 帖子类型统计
|
||||
discussion_count = sum(1 for p in posts.values() if p.get('type') == 'discussion')
|
||||
share_count = sum(1 for p in posts.values() if p.get('type') == 'share')
|
||||
|
||||
return jsonify({
|
||||
'users_count': len(users),
|
||||
'posts_count': len(posts),
|
||||
'topics_count': len(topics),
|
||||
'messages_count': total_messages,
|
||||
'today_posts': today_posts,
|
||||
'today_users': today_users,
|
||||
'discussion_count': discussion_count,
|
||||
'share_count': share_count,
|
||||
})
|
||||
|
||||
@app.route('/api/users')
|
||||
def api_users():
|
||||
users = load_users()
|
||||
posts = load_posts()
|
||||
|
||||
user_list = []
|
||||
for uid, user in users.items():
|
||||
# 统计用户帖子和回复数
|
||||
posts_count = len(user.get('posts', []))
|
||||
replies_count = 0
|
||||
for post in posts.values():
|
||||
for reply in post.get('replies', []):
|
||||
if reply.get('author_id') == uid:
|
||||
replies_count += 1
|
||||
|
||||
user_list.append({
|
||||
'id': uid,
|
||||
'username': user.get('username', ''),
|
||||
'email': user.get('email', ''),
|
||||
'phone': user.get('phone', ''),
|
||||
'posts_count': posts_count,
|
||||
'replies_count': replies_count,
|
||||
'created_at': user.get('created_at', ''),
|
||||
})
|
||||
|
||||
user_list.sort(key=lambda x: x['created_at'], reverse=True)
|
||||
return jsonify(user_list)
|
||||
|
||||
@app.route('/api/users/<user_id>', methods=['DELETE'])
|
||||
def api_delete_user(user_id):
|
||||
users = load_users()
|
||||
posts = load_posts()
|
||||
topics = load_topics()
|
||||
|
||||
if user_id not in users:
|
||||
return jsonify({'error': '用户不存在'}), 404
|
||||
|
||||
# 删除用户的帖子
|
||||
for post_id in users[user_id].get('posts', []):
|
||||
if post_id in posts:
|
||||
del posts[post_id]
|
||||
|
||||
# 从主题关注中移除
|
||||
for topic in topics.values():
|
||||
if user_id in topic.get('followers', []):
|
||||
topic['followers'].remove(user_id)
|
||||
|
||||
# 删除用户
|
||||
del users[user_id]
|
||||
|
||||
save_users(users)
|
||||
save_posts(posts)
|
||||
save_topics(topics)
|
||||
|
||||
return jsonify({'success': True})
|
||||
|
||||
@app.route('/api/posts')
|
||||
def api_posts():
|
||||
posts = load_posts()
|
||||
users = load_users()
|
||||
|
||||
post_type = request.args.get('type')
|
||||
|
||||
post_list = []
|
||||
for pid, post in posts.items():
|
||||
if post_type and post['type'] != post_type:
|
||||
continue
|
||||
|
||||
author = users.get(post['author_id'], {})
|
||||
post_list.append({
|
||||
'id': pid,
|
||||
'title': post['title'],
|
||||
'type': post['type'],
|
||||
'author': author.get('username', '未知'),
|
||||
'author_id': post['author_id'],
|
||||
'likes': len(post.get('likes', [])),
|
||||
'replies': len(post.get('replies', [])),
|
||||
'views': post.get('views', 0),
|
||||
'is_pinned': post.get('is_pinned', False),
|
||||
'created_at': post['created_at'],
|
||||
})
|
||||
|
||||
post_list.sort(key=lambda x: x['created_at'], reverse=True)
|
||||
return jsonify(post_list)
|
||||
|
||||
@app.route('/api/posts/<post_id>')
|
||||
def api_post_detail(post_id):
|
||||
posts = load_posts()
|
||||
users = load_users()
|
||||
|
||||
post = posts.get(post_id)
|
||||
if not post:
|
||||
return jsonify({'error': '帖子不存在'}), 404
|
||||
|
||||
author = users.get(post['author_id'], {})
|
||||
|
||||
# 获取回复
|
||||
replies = []
|
||||
for reply in post.get('replies', []):
|
||||
reply_author = users.get(reply['author_id'], {})
|
||||
replies.append({
|
||||
'id': reply['id'],
|
||||
'content': reply['content'][:100] + '...' if len(reply['content']) > 100 else reply['content'],
|
||||
'author': reply_author.get('username', '未知'),
|
||||
'likes': len(reply.get('likes', [])),
|
||||
'created_at': reply['created_at'],
|
||||
})
|
||||
|
||||
return jsonify({
|
||||
'id': post_id,
|
||||
'title': post['title'],
|
||||
'content': post['content'],
|
||||
'type': post['type'],
|
||||
'author': author.get('username', '未知'),
|
||||
'tags': post.get('tags', []),
|
||||
'likes': len(post.get('likes', [])),
|
||||
'replies': replies,
|
||||
'views': post.get('views', 0),
|
||||
'is_pinned': post.get('is_pinned', False),
|
||||
'created_at': post['created_at'],
|
||||
})
|
||||
|
||||
@app.route('/api/posts/<post_id>', methods=['DELETE'])
|
||||
def api_delete_post(post_id):
|
||||
posts = load_posts()
|
||||
users = load_users()
|
||||
|
||||
if post_id not in posts:
|
||||
return jsonify({'error': '帖子不存在'}), 404
|
||||
|
||||
author_id = posts[post_id].get('author_id')
|
||||
|
||||
del posts[post_id]
|
||||
|
||||
# 从用户列表中移除
|
||||
if author_id and author_id in users:
|
||||
if post_id in users[author_id].get('posts', []):
|
||||
users[author_id]['posts'].remove(post_id)
|
||||
|
||||
save_posts(posts)
|
||||
save_users(users)
|
||||
|
||||
return jsonify({'success': True})
|
||||
|
||||
@app.route('/api/posts/<post_id>/pin', methods=['POST'])
|
||||
def api_pin_post(post_id):
|
||||
posts = load_posts()
|
||||
|
||||
if post_id not in posts:
|
||||
return jsonify({'error': '帖子不存在'}), 404
|
||||
|
||||
posts[post_id]['is_pinned'] = not posts[post_id].get('is_pinned', False)
|
||||
save_posts(posts)
|
||||
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'is_pinned': posts[post_id]['is_pinned']
|
||||
})
|
||||
|
||||
@app.route('/api/topics')
|
||||
def api_topics():
|
||||
topics = load_topics()
|
||||
users = load_users()
|
||||
|
||||
topic_list = []
|
||||
for tid, topic in topics.items():
|
||||
author = users.get(topic['author_id'], {})
|
||||
topic_list.append({
|
||||
'id': tid,
|
||||
'name': topic['name'],
|
||||
'icon': topic.get('icon', '🔧'),
|
||||
'author': author.get('username', '未知'),
|
||||
'sub_topics_count': len(topic.get('sub_topics', [])),
|
||||
'questions_count': len(topic.get('questions', [])),
|
||||
'followers_count': len(topic.get('followers', [])),
|
||||
'created_at': topic['created_at'],
|
||||
})
|
||||
|
||||
topic_list.sort(key=lambda x: x['followers_count'], reverse=True)
|
||||
return jsonify(topic_list)
|
||||
|
||||
@app.route('/api/topics/<topic_id>')
|
||||
def api_topic_detail(topic_id):
|
||||
topics = load_topics()
|
||||
users = load_users()
|
||||
|
||||
topic = topics.get(topic_id)
|
||||
if not topic:
|
||||
return jsonify({'error': '主题不存在'}), 404
|
||||
|
||||
author = users.get(topic['author_id'], {})
|
||||
|
||||
return jsonify({
|
||||
'id': topic_id,
|
||||
'name': topic['name'],
|
||||
'description': topic.get('description', ''),
|
||||
'icon': topic.get('icon', '🔧'),
|
||||
'author': author.get('username', '未知'),
|
||||
'sub_topics': topic.get('sub_topics', []),
|
||||
'questions': topic.get('questions', []),
|
||||
'followers_count': len(topic.get('followers', [])),
|
||||
'created_at': topic['created_at'],
|
||||
})
|
||||
|
||||
@app.route('/api/topics/<topic_id>', methods=['DELETE'])
|
||||
def api_delete_topic(topic_id):
|
||||
topics = load_topics()
|
||||
|
||||
if topic_id not in topics:
|
||||
return jsonify({'error': '主题不存在'}), 404
|
||||
|
||||
del topics[topic_id]
|
||||
save_topics(topics)
|
||||
|
||||
return jsonify({'success': True})
|
||||
|
||||
@app.route('/api/tags')
|
||||
def api_tags():
|
||||
posts = load_posts()
|
||||
|
||||
tag_counts = {}
|
||||
for post in posts.values():
|
||||
for tag in post.get('tags', []):
|
||||
tag_counts[tag] = tag_counts.get(tag, 0) + 1
|
||||
|
||||
tags = sorted(tag_counts.items(), key=lambda x: x[1], reverse=True)
|
||||
return jsonify([{'name': t[0], 'count': t[1]} for t in tags[:20]])
|
||||
|
||||
if __name__ == '__main__':
|
||||
print("=" * 50)
|
||||
print("技术论坛 - 后台管理系统")
|
||||
print("=" * 50)
|
||||
print(f"访问地址: http://localhost:19005")
|
||||
print("=" * 50)
|
||||
|
||||
app.run(host='0.0.0.0', port=19005, debug=True)
|
||||
206
admin/templates/index.html
Normal file
206
admin/templates/index.html
Normal file
@@ -0,0 +1,206 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<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">
|
||||
<style>
|
||||
.gradient-bg { background: linear-gradient(135deg, #3b82f6 0%, #8b5cf6 100%); }
|
||||
</style>
|
||||
</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="/" class="flex items-center gap-3 px-6 py-3 bg-slate-700 text-white">
|
||||
<i class="ri-dashboard-line"></i><span>仪表盘</span>
|
||||
</a>
|
||||
<a href="/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="/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="/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="http://localhost:19004" target="_blank" class="text-slate-400 hover:text-white text-sm flex items-center gap-2">
|
||||
<i class="ri-external-link-line"></i> 访问前台
|
||||
</a>
|
||||
</div>
|
||||
</aside>
|
||||
|
||||
<!-- 主内容 -->
|
||||
<main class="ml-64 flex-1 p-8">
|
||||
<!-- 统计卡片 -->
|
||||
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6 mb-8">
|
||||
<div class="bg-white rounded-xl p-6 shadow-sm border border-gray-100">
|
||||
<div class="flex items-center justify-between">
|
||||
<div>
|
||||
<p class="text-gray-500 text-sm">用户总数</p>
|
||||
<p class="text-3xl font-bold text-gray-800 mt-2" id="stat-users">-</p>
|
||||
</div>
|
||||
<div class="w-12 h-12 bg-blue-100 rounded-lg flex items-center justify-center">
|
||||
<i class="ri-user-line text-2xl text-blue-600"></i>
|
||||
</div>
|
||||
</div>
|
||||
<p class="text-xs text-gray-400 mt-2">今日新增: <span id="stat-today-users">0</span></p>
|
||||
</div>
|
||||
|
||||
<div class="bg-white rounded-xl p-6 shadow-sm border border-gray-100">
|
||||
<div class="flex items-center justify-between">
|
||||
<div>
|
||||
<p class="text-gray-500 text-sm">帖子总数</p>
|
||||
<p class="text-3xl font-bold text-gray-800 mt-2" id="stat-posts">-</p>
|
||||
</div>
|
||||
<div class="w-12 h-12 bg-green-100 rounded-lg flex items-center justify-center">
|
||||
<i class="ri-file-text-line text-2xl text-green-600"></i>
|
||||
</div>
|
||||
</div>
|
||||
<p class="text-xs text-gray-400 mt-2">今日发布: <span id="stat-today-posts">0</span></p>
|
||||
</div>
|
||||
|
||||
<div class="bg-white rounded-xl p-6 shadow-sm border border-gray-100">
|
||||
<div class="flex items-center justify-between">
|
||||
<div>
|
||||
<p class="text-gray-500 text-sm">主题总数</p>
|
||||
<p class="text-3xl font-bold text-gray-800 mt-2" id="stat-topics">-</p>
|
||||
</div>
|
||||
<div class="w-12 h-12 bg-purple-100 rounded-lg flex items-center justify-center">
|
||||
<i class="ri-tools-line text-2xl text-purple-600"></i>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="bg-white rounded-xl p-6 shadow-sm border border-gray-100">
|
||||
<div class="flex items-center justify-between">
|
||||
<div>
|
||||
<p class="text-gray-500 text-sm">消息总数</p>
|
||||
<p class="text-3xl font-bold text-gray-800 mt-2" id="stat-messages">-</p>
|
||||
</div>
|
||||
<div class="w-12 h-12 bg-orange-100 rounded-lg flex items-center justify-center">
|
||||
<i class="ri-message-3-line text-2xl text-orange-600"></i>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 帖子类型分布 & 热门标签 -->
|
||||
<div class="grid grid-cols-1 lg:grid-cols-2 gap-6">
|
||||
<div class="bg-white rounded-xl shadow-sm border border-gray-100 p-6">
|
||||
<h2 class="text-lg font-semibold text-gray-800 mb-4 flex items-center gap-2">
|
||||
<i class="ri-pie-chart-line text-blue-500"></i>
|
||||
帖子类型分布
|
||||
</h2>
|
||||
<div id="typeChart" class="flex items-center justify-center gap-8 py-8">
|
||||
<div class="text-center">
|
||||
<div class="text-3xl font-bold text-blue-600" id="discussion-count">0</div>
|
||||
<div class="text-sm text-gray-500">技术交流</div>
|
||||
</div>
|
||||
<div class="text-center">
|
||||
<div class="text-3xl font-bold text-purple-600" id="share-count">0</div>
|
||||
<div class="text-sm text-gray-500">工具分享</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="bg-white rounded-xl shadow-sm border border-gray-100 p-6">
|
||||
<h2 class="text-lg font-semibold text-gray-800 mb-4 flex items-center gap-2">
|
||||
<i class="ri-price-tag-3-line text-green-500"></i>
|
||||
热门标签
|
||||
</h2>
|
||||
<div id="tagsList" class="flex flex-wrap gap-2">
|
||||
<p class="text-gray-500">加载中...</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 最新帖子 -->
|
||||
<div class="mt-6 bg-white rounded-xl shadow-sm border border-gray-100 p-6">
|
||||
<h2 class="text-lg font-semibold text-gray-800 mb-4 flex items-center gap-2">
|
||||
<i class="ri-time-line text-purple-500"></i>
|
||||
最新帖子
|
||||
</h2>
|
||||
<div id="recentPosts" class="space-y-3">
|
||||
<p class="text-gray-500">加载中...</p>
|
||||
</div>
|
||||
</div>
|
||||
</main>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
async function loadStats() {
|
||||
const res = await fetch('/api/stats');
|
||||
const data = await res.json();
|
||||
|
||||
document.getElementById('stat-users').textContent = data.users_count;
|
||||
document.getElementById('stat-posts').textContent = data.posts_count;
|
||||
document.getElementById('stat-topics').textContent = data.topics_count;
|
||||
document.getElementById('stat-messages').textContent = data.messages_count;
|
||||
document.getElementById('stat-today-users').textContent = data.today_users;
|
||||
document.getElementById('stat-today-posts').textContent = data.today_posts;
|
||||
document.getElementById('discussion-count').textContent = data.discussion_count;
|
||||
document.getElementById('share-count').textContent = data.share_count;
|
||||
}
|
||||
|
||||
async function loadTags() {
|
||||
const res = await fetch('/api/tags');
|
||||
const tags = await res.json();
|
||||
|
||||
const container = document.getElementById('tagsList');
|
||||
if (tags.length === 0) {
|
||||
container.innerHTML = '<p class="text-gray-500">暂无标签</p>';
|
||||
return;
|
||||
}
|
||||
|
||||
container.innerHTML = tags.map(tag => `
|
||||
<span class="px-3 py-1 bg-gray-100 text-gray-700 rounded-full text-sm">
|
||||
${tag.name} (${tag.count})
|
||||
</span>
|
||||
`).join('');
|
||||
}
|
||||
|
||||
async function loadRecentPosts() {
|
||||
const res = await fetch('/api/posts');
|
||||
const posts = await res.json();
|
||||
|
||||
const container = document.getElementById('recentPosts');
|
||||
if (posts.length === 0) {
|
||||
container.innerHTML = '<p class="text-gray-500">暂无帖子</p>';
|
||||
return;
|
||||
}
|
||||
|
||||
container.innerHTML = posts.slice(0, 5).map(p => `
|
||||
<div class="flex items-center justify-between p-3 bg-gray-50 rounded-lg">
|
||||
<div class="flex items-center gap-3">
|
||||
<span class="px-2 py-0.5 rounded text-xs ${p.type === 'discussion' ? 'bg-blue-100 text-blue-600' : 'bg-purple-100 text-purple-600'}">
|
||||
${p.type === 'discussion' ? '交流' : '分享'}
|
||||
</span>
|
||||
<span class="font-medium text-gray-800">${p.title}</span>
|
||||
</div>
|
||||
<div class="flex items-center gap-4 text-sm text-gray-500">
|
||||
<span>${p.author}</span>
|
||||
<span>${new Date(p.created_at).toLocaleDateString()}</span>
|
||||
<span><i class="ri-eye-line"></i> ${p.views}</span>
|
||||
</div>
|
||||
</div>
|
||||
`).join('');
|
||||
}
|
||||
|
||||
loadStats();
|
||||
loadTags();
|
||||
loadRecentPosts();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
203
admin/templates/posts.html
Normal file
203
admin/templates/posts.html
Normal file
@@ -0,0 +1,203 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<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="/" 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="/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="/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="/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>
|
||||
</aside>
|
||||
|
||||
<main class="ml-64 flex-1 p-8">
|
||||
<div class="flex items-center justify-between mb-6">
|
||||
<h1 class="text-2xl font-bold text-gray-800">帖子管理</h1>
|
||||
<select id="typeFilter" onchange="loadPosts()" class="px-4 py-2 border border-gray-200 rounded-lg">
|
||||
<option value="">全部类型</option>
|
||||
<option value="discussion">技术交流</option>
|
||||
<option value="share">工具分享</option>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<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-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>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody id="postTable">
|
||||
<tr><td colspan="7" class="px-6 py-8 text-center text-gray-500">加载中...</td></tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<!-- 详情弹窗 -->
|
||||
<div id="detailModal" 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-2xl max-h-[80vh] overflow-hidden flex flex-col">
|
||||
<div class="p-6 border-b flex justify-between items-center">
|
||||
<h2 class="text-xl font-bold text-gray-800" id="modalTitle">帖子详情</h2>
|
||||
<button onclick="closeModal()" class="text-gray-400"><i class="ri-close-line text-2xl"></i></button>
|
||||
</div>
|
||||
<div class="p-6 overflow-auto flex-1" id="modalContent"></div>
|
||||
</div>
|
||||
</div>
|
||||
</main>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
async function loadPosts() {
|
||||
const type = document.getElementById('typeFilter').value;
|
||||
const url = type ? `/api/posts?type=${type}` : '/api/posts';
|
||||
|
||||
const res = await fetch(url);
|
||||
const posts = await res.json();
|
||||
|
||||
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>';
|
||||
return;
|
||||
}
|
||||
|
||||
tbody.innerHTML = posts.map(p => `
|
||||
<tr class="border-b border-gray-50 hover:bg-gray-50">
|
||||
<td class="px-6 py-4">
|
||||
<p class="font-medium text-gray-800 truncate max-w-xs">${p.title}</p>
|
||||
</td>
|
||||
<td class="px-6 py-4">
|
||||
<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' ? '技术交流' : '工具分享'}
|
||||
</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">
|
||||
${p.views} / ${p.likes} / ${p.replies}
|
||||
</td>
|
||||
<td class="px-6 py-4">
|
||||
${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">
|
||||
${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">
|
||||
<i class="ri-eye-line"></i>
|
||||
</button>
|
||||
<button onclick="pinPost('${p.id}')" class="text-yellow-500 hover:text-yellow-700 mr-3">
|
||||
<i class="ri-pushpin-line"></i>
|
||||
</button>
|
||||
<button onclick="deletePost('${p.id}')" class="text-red-500 hover:text-red-700">
|
||||
<i class="ri-delete-bin-line"></i>
|
||||
</button>
|
||||
</td>
|
||||
</tr>
|
||||
`).join('');
|
||||
}
|
||||
|
||||
async function viewPost(postId) {
|
||||
const res = await fetch(`/api/posts/${postId}`);
|
||||
const post = await res.json();
|
||||
|
||||
document.getElementById('modalTitle').textContent = post.title;
|
||||
document.getElementById('modalContent').innerHTML = `
|
||||
<div class="space-y-4">
|
||||
<div class="grid grid-cols-3 gap-4 p-4 bg-gray-50 rounded-lg">
|
||||
<div>
|
||||
<p class="text-sm text-gray-500">作者</p>
|
||||
<p class="font-medium">${post.author}</p>
|
||||
</div>
|
||||
<div>
|
||||
<p class="text-sm text-gray-500">类型</p>
|
||||
<p class="font-medium">${post.type === 'discussion' ? '技术交流' : '工具分享'}</p>
|
||||
</div>
|
||||
<div>
|
||||
<p class="text-sm text-gray-500">浏览/赞</p>
|
||||
<p class="font-medium">${post.views} / ${post.likes}</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<p class="text-sm text-gray-500 mb-2">内容</p>
|
||||
<div class="p-4 bg-gray-50 rounded-lg text-sm text-gray-700 whitespace-pre-wrap max-h-60 overflow-auto">
|
||||
${post.content}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
${post.replies.length > 0 ? `
|
||||
<div>
|
||||
<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>
|
||||
`).join('')}
|
||||
</div>
|
||||
</div>
|
||||
` : ''}
|
||||
</div>
|
||||
`;
|
||||
|
||||
document.getElementById('detailModal').classList.remove('hidden');
|
||||
document.getElementById('detailModal').classList.add('flex');
|
||||
}
|
||||
|
||||
function closeModal() {
|
||||
document.getElementById('detailModal').classList.add('hidden');
|
||||
document.getElementById('detailModal').classList.remove('flex');
|
||||
}
|
||||
|
||||
async function pinPost(postId) {
|
||||
const res = await fetch(`/api/posts/${postId}/pin`, { method: 'POST' });
|
||||
const data = await res.json();
|
||||
|
||||
if (data.success) {
|
||||
loadPosts();
|
||||
}
|
||||
}
|
||||
|
||||
async function deletePost(postId) {
|
||||
if (!confirm('确定要删除这个帖子吗?')) return;
|
||||
|
||||
const res = await fetch(`/api/posts/${postId}`, { method: 'DELETE' });
|
||||
const data = await res.json();
|
||||
|
||||
if (data.success) {
|
||||
loadPosts();
|
||||
}
|
||||
}
|
||||
|
||||
loadPosts();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
181
admin/templates/topics.html
Normal file
181
admin/templates/topics.html
Normal file
@@ -0,0 +1,181 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<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="/" 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="/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="/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="/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>
|
||||
</nav>
|
||||
</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-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>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody id="topicTable">
|
||||
<tr><td colspan="7" class="px-6 py-8 text-center text-gray-500">加载中...</td></tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<!-- 详情弹窗 -->
|
||||
<div id="detailModal" 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-2xl max-h-[80vh] overflow-hidden flex flex-col">
|
||||
<div class="p-6 border-b flex justify-between items-center">
|
||||
<h2 class="text-xl font-bold text-gray-800" id="modalTitle">主题详情</h2>
|
||||
<button onclick="closeModal()" class="text-gray-400"><i class="ri-close-line text-2xl"></i></button>
|
||||
</div>
|
||||
<div class="p-6 overflow-auto flex-1" id="modalContent"></div>
|
||||
</div>
|
||||
</div>
|
||||
</main>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
async function loadTopics() {
|
||||
const res = await fetch('/api/topics');
|
||||
const topics = await res.json();
|
||||
|
||||
const tbody = document.getElementById('topicTable');
|
||||
|
||||
if (topics.length === 0) {
|
||||
tbody.innerHTML = '<tr><td colspan="7" class="px-6 py-8 text-center text-gray-500">暂无主题</td></tr>';
|
||||
return;
|
||||
}
|
||||
|
||||
tbody.innerHTML = topics.map(t => `
|
||||
<tr class="border-b border-gray-50 hover:bg-gray-50">
|
||||
<td class="px-6 py-4">
|
||||
<div class="flex items-center gap-2">
|
||||
<span class="text-2xl">${t.icon}</span>
|
||||
<span class="font-medium text-gray-800">${t.name}</span>
|
||||
</div>
|
||||
</td>
|
||||
<td class="px-6 py-4 text-gray-600">${t.author}</td>
|
||||
<td class="px-6 py-4">
|
||||
<span class="px-2 py-1 bg-blue-100 text-blue-700 rounded text-xs">${t.sub_topics_count}</span>
|
||||
</td>
|
||||
<td class="px-6 py-4">
|
||||
<span class="px-2 py-1 bg-green-100 text-green-700 rounded text-xs">${t.questions_count}</span>
|
||||
</td>
|
||||
<td class="px-6 py-4">
|
||||
<span class="px-2 py-1 bg-purple-100 text-purple-700 rounded text-xs">${t.followers_count}</span>
|
||||
</td>
|
||||
<td class="px-6 py-4 text-sm text-gray-500">
|
||||
${new Date(t.created_at).toLocaleDateString()}
|
||||
</td>
|
||||
<td class="px-6 py-4">
|
||||
<button onclick="viewTopic('${t.id}')" class="text-blue-500 hover:text-blue-700 mr-3">
|
||||
<i class="ri-eye-line"></i>
|
||||
</button>
|
||||
<button onclick="deleteTopic('${t.id}')" class="text-red-500 hover:text-red-700">
|
||||
<i class="ri-delete-bin-line"></i>
|
||||
</button>
|
||||
</td>
|
||||
</tr>
|
||||
`).join('');
|
||||
}
|
||||
|
||||
async function viewTopic(topicId) {
|
||||
const res = await fetch(`/api/topics/${topicId}`);
|
||||
const topic = await res.json();
|
||||
|
||||
document.getElementById('modalTitle').textContent = topic.name;
|
||||
document.getElementById('modalContent').innerHTML = `
|
||||
<div class="space-y-4">
|
||||
<div class="p-4 bg-gray-50 rounded-lg">
|
||||
<p class="text-sm text-gray-500">描述</p>
|
||||
<p class="text-gray-800">${topic.description || '暂无描述'}</p>
|
||||
</div>
|
||||
|
||||
<div class="grid grid-cols-3 gap-4">
|
||||
<div class="p-4 bg-blue-50 rounded-lg text-center">
|
||||
<div class="text-2xl font-bold text-blue-600">${topic.sub_topics.length}</div>
|
||||
<div class="text-sm text-gray-500">子主题</div>
|
||||
</div>
|
||||
<div class="p-4 bg-green-50 rounded-lg text-center">
|
||||
<div class="text-2xl font-bold text-green-600">${topic.questions.length}</div>
|
||||
<div class="text-sm text-gray-500">问题</div>
|
||||
</div>
|
||||
<div class="p-4 bg-purple-50 rounded-lg text-center">
|
||||
<div class="text-2xl font-bold text-purple-600">${topic.followers_count}</div>
|
||||
<div class="text-sm text-gray-500">关注</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
${topic.questions.length > 0 ? `
|
||||
<div>
|
||||
<p class="text-sm text-gray-500 mb-2">最新问题</p>
|
||||
<div class="space-y-2 max-h-40 overflow-auto">
|
||||
${topic.questions.slice(0, 5).map(q => `
|
||||
<div class="p-2 bg-gray-50 rounded text-sm">
|
||||
<p class="font-medium">${q.title}</p>
|
||||
<p class="text-gray-500 text-xs">${q.answers.length} 回答</p>
|
||||
</div>
|
||||
`).join('')}
|
||||
</div>
|
||||
</div>
|
||||
` : ''}
|
||||
</div>
|
||||
`;
|
||||
|
||||
document.getElementById('detailModal').classList.remove('hidden');
|
||||
document.getElementById('detailModal').classList.add('flex');
|
||||
}
|
||||
|
||||
function closeModal() {
|
||||
document.getElementById('detailModal').classList.add('hidden');
|
||||
document.getElementById('detailModal').classList.remove('flex');
|
||||
}
|
||||
|
||||
async function deleteTopic(topicId) {
|
||||
if (!confirm('确定要删除这个主题吗?')) return;
|
||||
|
||||
const res = await fetch(`/api/topics/${topicId}`, { method: 'DELETE' });
|
||||
const data = await res.json();
|
||||
|
||||
if (data.success) {
|
||||
loadTopics();
|
||||
}
|
||||
}
|
||||
|
||||
loadTopics();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
110
admin/templates/users.html
Normal file
110
admin/templates/users.html
Normal file
@@ -0,0 +1,110 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<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="/" 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="/users" class="flex items-center gap-3 px-6 py-3 bg-slate-700 text-white">
|
||||
<i class="ri-user-line"></i><span>用户管理</span>
|
||||
</a>
|
||||
<a href="/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="/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>
|
||||
</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-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>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody id="userTable">
|
||||
<tr><td colspan="7" class="px-6 py-8 text-center text-gray-500">加载中...</td></tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</main>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
async function loadUsers() {
|
||||
const res = await fetch('/api/users');
|
||||
const users = await res.json();
|
||||
|
||||
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>';
|
||||
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">
|
||||
<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">
|
||||
<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>
|
||||
<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> 删除
|
||||
</button>
|
||||
</td>
|
||||
</tr>
|
||||
`).join('');
|
||||
}
|
||||
|
||||
async function deleteUser(userId) {
|
||||
if (!confirm('确定要删除这个用户吗?这将同时删除该用户的所有帖子!')) return;
|
||||
|
||||
const res = await fetch(`/api/users/${userId}`, { method: 'DELETE' });
|
||||
const data = await res.json();
|
||||
|
||||
if (data.success) {
|
||||
loadUsers();
|
||||
} else {
|
||||
alert('删除失败');
|
||||
}
|
||||
}
|
||||
|
||||
loadUsers();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user