From cfe5cf53aab828d2aef3074fd18519f68312d8cd Mon Sep 17 00:00:00 2001 From: Zhang Chao Date: Wed, 26 Aug 2026 17:59:03 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=90=8E=E5=8F=B0=E8=AF=84?= =?UTF-8?q?=E8=AE=BA=E5=8A=A0=E8=BD=BD=E9=97=AE=E9=A2=98=EF=BC=8C=E9=9D=9E?= =?UTF-8?q?admin=E4=B9=9F=E5=8F=AF=E5=8A=A0=E8=BD=BD=E5=AF=B9=E8=87=AA?= =?UTF-8?q?=E5=B7=B1=E6=96=87=E7=AB=A0=E7=9A=84=E6=9C=AA=E5=AE=A1=E6=A0=B8?= =?UTF-8?q?=E8=AF=84=E8=AE=BA=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- handlers/backend/comment.go | 16 ++++++++-------- main.go | 1 + routers/router.go | 2 +- static/js/admin.js | 26 +++++++++++++------------- templates/admin/index.html | 1 - templates/default/sidebar.html | 2 +- 6 files changed, 24 insertions(+), 24 deletions(-) diff --git a/handlers/backend/comment.go b/handlers/backend/comment.go index dfee1ad..683a905 100644 --- a/handlers/backend/comment.go +++ b/handlers/backend/comment.go @@ -35,9 +35,13 @@ func GetComments(c *gin.Context) { }) // 非管理员只能查看自己文章上的评论(后台)或已审核的评论(前台) - if !isAdmin(c) { - // 前台访问:只显示已审核的评论 - db = db.Where("status = ?", "approved") + if isAdmin(c) { + // 管理员可以查看所有评论 + if status != "" { + db = db.Where("status = ?", status) + } + } else { + if userID, exists := c.Get("userID"); exists { // 如果是后台接口调用,限制为自己的文章 if postID == "" && postSlug == "" { @@ -45,11 +49,7 @@ func GetComments(c *gin.Context) { database.DB.Model(&models.Post{}).Select("id").Where("author_id = ?", userID)) } } - } else { - // 管理员可以查看所有状态的评论(通过 status 参数过滤) - if status != "" { - db = db.Where("status = ?", status) - } + } // 按文章 ID 或 slug 过滤 diff --git a/main.go b/main.go index 0ddc517..08d7128 100644 --- a/main.go +++ b/main.go @@ -59,6 +59,7 @@ func main() { r := gin.New() r.Use(gin.Recovery()) r.Use(slogMiddleware(logger)) + r.Use(gin.Logger()) // 配置模板渲染器(现在 LoadTemplates 返回 error) htmlRenderer, err := utils.LoadTemplates(theme) diff --git a/routers/router.go b/routers/router.go index 6eeb235..abceffc 100644 --- a/routers/router.go +++ b/routers/router.go @@ -110,12 +110,12 @@ func setupAdminAPIRoutes(api *gin.RouterGroup) { admin.DELETE("/categories/:id", backend.DeleteCategory) // 标签管理(写入) - admin.POST("/tags", backend.CreateTag) admin.GET("/tags/:id", backend.GetTag) admin.PUT("/tags/:id", backend.UpdateTag) admin.DELETE("/tags/:id", backend.DeleteTag) // 页面管理(写入) + admin.GET("/pages", backend.GetPages) admin.POST("/pages", backend.CreatePage) admin.PUT("/pages/:id", backend.UpdatePage) admin.DELETE("/pages/:id", backend.DeletePage) diff --git a/static/js/admin.js b/static/js/admin.js index 7d841e6..fed95bf 100644 --- a/static/js/admin.js +++ b/static/js/admin.js @@ -725,7 +725,7 @@ async function loadTags() { // 加载页面 async function loadPages() { try { - const res = await fetch(`${API_BASE}/pages`); + const res = await fetch(`${API_BASE}/admin/pages`); const data = await res.json(); const tbody = document.getElementById('pages-list'); @@ -900,7 +900,7 @@ function editTag(id, name, slug) { // 编辑页面 async function editPage(id) { try { - const res = await fetch(`${API_BASE}/pages/${id}`, { headers: getHeaders() }); + const res = await fetch(`${API_BASE}/admin/pages/${id}`, { headers: getHeaders() }); const data = await res.json(); const page = data.data; @@ -1266,14 +1266,14 @@ let currentTheme = null; async function loadThemes() { try { // 获取当前主题 - const currentRes = await fetch(`${API_BASE}/theme`, { + const currentRes = await fetch(`${API_BASE}/admin/theme`, { headers: getHeaders() }); const currentData = await currentRes.json(); currentTheme = currentData.theme; // 获取所有可用主题 - const themesRes = await fetch(`${API_BASE}/themes`, { + const themesRes = await fetch(`${API_BASE}/admin/themes`, { headers: getHeaders() }); const themesData = await themesRes.json(); @@ -1345,7 +1345,7 @@ async function switchTheme() { } try { - const response = await fetch(`${API_BASE}/theme`, { + const response = await fetch(`${API_BASE}/admin/theme`, { method: 'POST', headers: { ...getHeaders(), @@ -1392,7 +1392,7 @@ async function showCreateThemeModal() { const themeSelect = document.getElementById('theme-template'); try { // 获取所有可用主题 - const res = await fetch(`${API_BASE}/themes`, { + const res = await fetch(`${API_BASE}/admin/themes`, { headers: getHeaders() }); const themesData = await res.json(); @@ -1443,7 +1443,7 @@ async function handleCreateTheme(e) { } try { - const response = await fetch(`${API_BASE}/themes/create`, { + const response = await fetch(`${API_BASE}/admin/themes/create`, { method: 'POST', headers: { ...getHeaders(), @@ -1477,7 +1477,7 @@ async function handleCreateTheme(e) { // 快速切换主题 async function quickSwitch(theme) { try { - const response = await fetch(`${API_BASE}/theme`, { + const response = await fetch(`${API_BASE}/admin/theme`, { method: 'POST', headers: { ...getHeaders(), @@ -1525,7 +1525,7 @@ async function editTheme(themeName) { // 加载主题文件列表 async function loadThemeFiles(themeName) { try { - const response = await fetch(`${API_BASE}/themes/${themeName}/files`, { + const response = await fetch(`${API_BASE}/admin/themes/${themeName}/files`, { headers: getHeaders() }); const data = await response.json(); @@ -1577,7 +1577,7 @@ async function loadFileContent(filename) { try { currentEditingFile = filename; - const response = await fetch(`${API_BASE}/themes/${currentEditingTheme}/files/${filename}`, { + const response = await fetch(`${API_BASE}/admin/themes/${currentEditingTheme}/files/${filename}`, { headers: getHeaders() }); const data = await response.json(); @@ -1610,7 +1610,7 @@ async function saveCurrentFile() { const content = document.getElementById('theme-file-editor').value; try { - const response = await fetch(`${API_BASE}/themes/${currentEditingTheme}/files/${currentEditingFile}`, { + const response = await fetch(`${API_BASE}/admin/themes/${currentEditingTheme}/files/${currentEditingFile}`, { method: 'PUT', headers: { ...getHeaders(), @@ -1626,11 +1626,11 @@ async function saveCurrentFile() { // 如果是模板文件,重新加载主题 if (currentEditingFile.endsWith('.html')) { - const renderer = await fetch(`${API_BASE}/theme`, { headers: getHeaders() }); + const renderer = await fetch(`${API_BASE}/admin/theme`, { headers: getHeaders() }); const rendererData = await renderer.json(); if (rendererData.theme === currentEditingTheme) { // 重新加载当前主题以应用更改 - await fetch(`${API_BASE}/theme`, { + await fetch(`${API_BASE}/admin/theme`, { method: 'POST', headers: { ...getHeaders(), diff --git a/templates/admin/index.html b/templates/admin/index.html index 9f5f84a..81c3613 100644 --- a/templates/admin/index.html +++ b/templates/admin/index.html @@ -304,7 +304,6 @@