fix: 修复后台管理页面API路径和链接

- users/posts/topics页面API改为/admin/api/xxx
- 页面链接改为/admin/xxx
- 添加登录验证检查和退出登录按钮
This commit is contained in:
2026-04-12 17:43:17 +08:00
parent e6805d0b75
commit b585adefc9
3 changed files with 106 additions and 34 deletions

View File

@@ -17,19 +17,27 @@
</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">
<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="/users" class="flex items-center gap-3 px-6 py-3 text-slate-300 hover:bg-slate-700 hover:text-white">
<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="/posts" class="flex items-center gap-3 px-6 py-3 bg-slate-700 text-white">
<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="/topics" class="flex items-center gap-3 px-6 py-3 text-slate-300 hover:bg-slate-700 hover:text-white">
<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">
@@ -75,9 +83,25 @@
</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 loadPosts() {
const type = document.getElementById('typeFilter').value;
const url = type ? `/api/posts?type=${type}` : '/api/posts';
const url = type ? `/admin/api/posts?type=${type}` : '/admin/api/posts';
const res = await fetch(url);
const posts = await res.json();
@@ -125,7 +149,7 @@
}
async function viewPost(postId) {
const res = await fetch(`/api/posts/${postId}`);
const res = await fetch(`/admin/api/posts/${postId}`);
const post = await res.json();
document.getElementById('modalTitle').textContent = post.title;
@@ -149,11 +173,11 @@
<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}
${post.content || '无内容'}
</div>
</div>
${post.replies.length > 0 ? `
${post.replies && 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">
@@ -178,7 +202,7 @@
}
async function pinPost(postId) {
const res = await fetch(`/api/posts/${postId}/pin`, { method: 'POST' });
const res = await fetch(`/admin/api/posts/${postId}/pin`, { method: 'POST' });
const data = await res.json();
if (data.success) {
@@ -189,7 +213,7 @@
async function deletePost(postId) {
if (!confirm('确定要删除这个帖子吗?')) return;
const res = await fetch(`/api/posts/${postId}`, { method: 'DELETE' });
const res = await fetch(`/admin/api/posts/${postId}`, { method: 'DELETE' });
const data = await res.json();
if (data.success) {