模板可切换,模板引擎模块化有问题

This commit is contained in:
张超
2025-06-19 22:27:06 +08:00
parent 4c9f6c140f
commit 8241b8c61f
16 changed files with 401 additions and 122 deletions

View File

@@ -5,6 +5,7 @@ import (
"net/http"
"go_blog/config"
"go_blog/models"
"go_blog/themes"
"github.com/gin-gonic/gin"
@@ -63,23 +64,38 @@ func SwitchTheme(c *gin.Context) {
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()})
// 校验主题是否存在(保持原有逻辑)
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
}
// 持久化主题配置(假设 config 包支持保存到配置文件
if err := config.SetCurrentTheme(newTheme); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "保存主题配置失败: " + err.Error()})
// 加载新主题(保持原有逻辑
if err := themeManager.LoadTheme(newTheme); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "主题加载失败,请检查主题文件"})
return
}
// 更新全局配置
globalConfig := config.GetGlobalConfig()
globalConfig.Theme.Current = newTheme
// 调用options.go的SetOptionValue持久化配置
if err := models.SetOptionValue(models.DB, "current_theme", 0, newTheme); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "主题配置保存失败: " + err.Error()})
return
}