This commit is contained in:
张超
2025-05-27 19:01:05 +08:00
parent f02495cc7a
commit 5ffc64e1dd
16 changed files with 512 additions and 123 deletions

View File

@@ -21,14 +21,25 @@ func RenderTemplate(c *gin.Context, tmpl string, data gin.H) {
}
// SwitchTheme handles POST requests to switch the current theme
// SwitchTheme 处理主题切换请求(修正后)
func SwitchTheme(c *gin.Context) {
newTheme := c.PostForm("theme")
manager := &ThemeManager{} //themes.GetManager()
// 从上下文中获取主题管理器(通过 main.go 的 themeMiddleware 注入)
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
}
if err := manager.LoadTheme(newTheme); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
} else {
config.SetCurrentTheme(newTheme) // Update the configuration
c.JSON(http.StatusOK, gin.H{"status": "success"})
}
newTheme := c.PostForm("theme")
if err := themeManager.LoadTheme(newTheme); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "加载主题失败: " + err.Error()})
return
}
config.SetCurrentTheme(newTheme) // 持久化主题配置(需确保 config 包支持)
c.JSON(http.StatusOK, gin.H{"status": "主题切换成功"})
}