2025-07-12 15:56:36 +08:00
|
|
|
package admin
|
2025-05-27 19:01:05 +08:00
|
|
|
|
|
|
|
|
import (
|
2025-06-18 18:34:08 +08:00
|
|
|
"html/template"
|
2025-06-09 17:59:19 +08:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
|
|
"go_blog/config"
|
2025-06-19 22:27:06 +08:00
|
|
|
"go_blog/models"
|
2025-07-12 15:56:36 +08:00
|
|
|
"go_blog/utils"
|
2025-05-27 19:01:05 +08:00
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
)
|
|
|
|
|
|
2025-06-09 17:59:19 +08:00
|
|
|
// ListThemes 显示主题管理页面(返回主题列表和当前主题)
|
2025-05-27 19:01:05 +08:00
|
|
|
func ListThemes(c *gin.Context) {
|
2025-06-09 17:59:19 +08:00
|
|
|
// 从上下文中获取主题管理器
|
2025-05-27 19:01:05 +08:00
|
|
|
tm, exists := c.Get("ThemeManager")
|
|
|
|
|
if !exists {
|
2025-06-09 17:59:19 +08:00
|
|
|
c.String(http.StatusInternalServerError, "主题管理器未找到")
|
|
|
|
|
return
|
|
|
|
|
}
|
2025-07-12 15:56:36 +08:00
|
|
|
themeManager, ok := tm.(*utils.ThemeManager)
|
2025-06-09 17:59:19 +08:00
|
|
|
if !ok {
|
|
|
|
|
c.String(http.StatusInternalServerError, "主题管理器类型错误")
|
2025-05-27 19:01:05 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-09 17:59:19 +08:00
|
|
|
// 获取可用主题列表(读取 web/themes 目录下的所有子目录)
|
|
|
|
|
entries, err := themeManager.GetAvailableThemes() // 假设 ThemeManager 新增获取主题列表方法
|
2025-05-27 19:01:05 +08:00
|
|
|
if err != nil {
|
2025-06-09 17:59:19 +08:00
|
|
|
c.String(http.StatusInternalServerError, "读取主题目录失败: "+err.Error())
|
2025-05-27 19:01:05 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-09 17:59:19 +08:00
|
|
|
// 渲染管理页面模板
|
2025-06-18 18:34:08 +08:00
|
|
|
// 直接加载 web/admin/themes.tmpl 文件(路径相对于项目根目录)
|
|
|
|
|
const themesTemplatePath = "web/admin/themes.tmpl" // 定义模板路径常量
|
|
|
|
|
|
|
|
|
|
tpl, err := template.ParseFiles(themesTemplatePath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.String(http.StatusInternalServerError, "模板加载失败: "+err.Error())
|
2025-06-09 17:59:19 +08:00
|
|
|
return
|
2025-05-27 19:01:05 +08:00
|
|
|
}
|
2025-06-09 17:59:19 +08:00
|
|
|
c.Header("Content-Type", "text/html; charset=utf-8")
|
|
|
|
|
err = tpl.Execute(c.Writer, gin.H{
|
|
|
|
|
"CurrentTheme": themeManager.CurrentTheme(),
|
|
|
|
|
"Themes": entries,
|
2025-05-27 19:01:05 +08:00
|
|
|
})
|
2025-06-09 17:59:19 +08:00
|
|
|
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
|
|
|
|
|
}
|
2025-07-12 15:56:36 +08:00
|
|
|
themeManager, ok := tm.(*utils.ThemeManager)
|
2025-06-09 17:59:19 +08:00
|
|
|
if !ok {
|
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "主题管理器类型错误"})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
newTheme := c.PostForm("theme")
|
|
|
|
|
if newTheme == "" {
|
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "主题名称不能为空"})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-19 22:27:06 +08:00
|
|
|
// 校验主题是否存在(保持原有逻辑)
|
|
|
|
|
availableThemes, _ := themeManager.GetAvailableThemes()
|
|
|
|
|
themeExists := false
|
|
|
|
|
for _, t := range availableThemes {
|
|
|
|
|
if t == newTheme {
|
|
|
|
|
themeExists = true
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if !themeExists {
|
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "无效的主题名称"})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 加载新主题(保持原有逻辑)
|
2025-06-09 17:59:19 +08:00
|
|
|
if err := themeManager.LoadTheme(newTheme); err != nil {
|
2025-06-19 22:27:06 +08:00
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "主题加载失败,请检查主题文件"})
|
2025-06-09 17:59:19 +08:00
|
|
|
return
|
|
|
|
|
}
|
2025-06-19 22:27:06 +08:00
|
|
|
// 更新全局配置
|
|
|
|
|
globalConfig := config.GetGlobalConfig()
|
|
|
|
|
globalConfig.Theme.Current = newTheme
|
2025-06-09 17:59:19 +08:00
|
|
|
|
2025-06-19 22:27:06 +08:00
|
|
|
// 调用options.go的SetOptionValue持久化配置
|
|
|
|
|
if err := models.SetOptionValue(models.DB, "current_theme", 0, newTheme); err != nil {
|
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "主题配置保存失败: " + err.Error()})
|
2025-06-09 17:59:19 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"status": "主题切换成功", "current_theme": newTheme})
|
2025-05-27 19:01:05 +08:00
|
|
|
}
|