增加admin相关页面
This commit is contained in:
@@ -1,39 +1,84 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"go_blog/config"
|
||||
"go_blog/themes"
|
||||
"os"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// ListThemes 获取可用主题列表
|
||||
// ListThemes 显示主题管理页面(返回主题列表和当前主题)
|
||||
func ListThemes(c *gin.Context) {
|
||||
// 从主题管理器获取当前主题
|
||||
// 从上下文中获取主题管理器
|
||||
tm, exists := c.Get("ThemeManager")
|
||||
if !exists {
|
||||
c.JSON(500, gin.H{"error": "主题管理器未找到"})
|
||||
c.String(http.StatusInternalServerError, "主题管理器未找到")
|
||||
return
|
||||
}
|
||||
themeManager, ok := tm.(*themes.ThemeManager)
|
||||
if !ok {
|
||||
c.String(http.StatusInternalServerError, "主题管理器类型错误")
|
||||
return
|
||||
}
|
||||
themeManager := tm.(*themes.ThemeManager)
|
||||
|
||||
// 读取 web/themes 目录下的所有主题文件夹
|
||||
themesDir := "web/themes"
|
||||
entries, err := os.ReadDir(themesDir)
|
||||
// 获取可用主题列表(读取 web/themes 目录下的所有子目录)
|
||||
entries, err := themeManager.GetAvailableThemes() // 假设 ThemeManager 新增获取主题列表方法
|
||||
if err != nil {
|
||||
c.JSON(500, gin.H{"error": "读取主题目录失败: " + err.Error()})
|
||||
c.String(http.StatusInternalServerError, "读取主题目录失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
var themeList []string
|
||||
for _, entry := range entries {
|
||||
if entry.IsDir() {
|
||||
themeList = append(themeList, entry.Name())
|
||||
}
|
||||
// 渲染管理页面模板
|
||||
tpl := themeManager.GetTemplate("admin/themes") // 对应 templates/admin/themes.tmpl
|
||||
if tpl == nil {
|
||||
c.String(http.StatusInternalServerError, "模板 'admin/themes' 未找到")
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, gin.H{
|
||||
"current": themeManager.CurrentTheme(),
|
||||
"themes": themeList,
|
||||
c.Header("Content-Type", "text/html; charset=utf-8")
|
||||
err = tpl.Execute(c.Writer, gin.H{
|
||||
"CurrentTheme": themeManager.CurrentTheme(),
|
||||
"Themes": entries,
|
||||
})
|
||||
if err != nil {
|
||||
c.String(http.StatusInternalServerError, "渲染模板失败: "+err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// SwitchTheme 处理主题切换请求
|
||||
func SwitchTheme(c *gin.Context) {
|
||||
// 从上下文中获取主题管理器
|
||||
tm, exists := c.Get("ThemeManager")
|
||||
if !exists {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "主题管理器未找到"})
|
||||
return
|
||||
}
|
||||
themeManager, ok := tm.(*themes.ThemeManager)
|
||||
if !ok {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "主题管理器类型错误"})
|
||||
return
|
||||
}
|
||||
|
||||
// 获取前端提交的主题名称
|
||||
newTheme := c.PostForm("theme")
|
||||
if newTheme == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "主题名称不能为空"})
|
||||
return
|
||||
}
|
||||
|
||||
// 加载新主题
|
||||
if err := themeManager.LoadTheme(newTheme); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "加载主题失败: " + err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
// 持久化主题配置(假设 config 包支持保存到配置文件)
|
||||
if err := config.SetCurrentTheme(newTheme); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "保存主题配置失败: " + err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"status": "主题切换成功", "current_theme": newTheme})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user