add admin router

This commit is contained in:
张超
2025-06-18 18:34:08 +08:00
parent 67c85327ee
commit 4c9f6c140f
4 changed files with 32 additions and 86 deletions

View File

@@ -24,26 +24,3 @@ func ShowAdminIndexPage(c *gin.Context) {
c.String(http.StatusInternalServerError, "渲染模板失败: "+err.Error())
}
}
// ShowThemeSwitchPage 渲染主题切换页面
func ShowThemeSwitchPage(c *gin.Context) {
// 假设主题列表存储在固定路径或需要手动维护(示例数据,需根据实际情况修改)
themes := []string{"default", "dark", "light"} // 示例主题列表
currentTheme := "default" // 示例当前主题(需根据实际存储方式获取)
// 直接加载 web/admin 目录下的 themes.tmpl 模板(需确保文件存在)
tpl, err := template.ParseFiles("web/admin/themes.tmpl")
if err != nil {
c.String(http.StatusInternalServerError, "加载模板失败: "+err.Error())
return
}
c.Header("Content-Type", "text/html; charset=utf-8")
err = tpl.Execute(c.Writer, gin.H{
"CurrentTheme": currentTheme,
"Themes": themes,
})
if err != nil {
c.String(http.StatusInternalServerError, "渲染模板失败: "+err.Error())
}
}

View File

@@ -1,6 +1,7 @@
package controllers
import (
"html/template"
"net/http"
"go_blog/config"
@@ -31,12 +32,14 @@ func ListThemes(c *gin.Context) {
}
// 渲染管理页面模板
tpl := themeManager.GetTemplate("admin/themes") // 对应 templates/admin/themes.tmpl
if tpl == nil {
c.String(http.StatusInternalServerError, "模板 'admin/themes' 未找到")
// 直接加载 web/admin/themes.tmpl 文件(路径相对于项目根目录)
const themesTemplatePath = "web/admin/themes.tmpl" // 定义模板路径常量
tpl, err := template.ParseFiles(themesTemplatePath)
if err != nil {
c.String(http.StatusInternalServerError, "模板加载失败: "+err.Error())
return
}
c.Header("Content-Type", "text/html; charset=utf-8")
err = tpl.Execute(c.Writer, gin.H{
"CurrentTheme": themeManager.CurrentTheme(),