增加admin相关页面

This commit is contained in:
张超
2025-06-09 17:59:19 +08:00
parent c273584189
commit 7913a2b381
19 changed files with 345 additions and 532 deletions

View File

@@ -200,17 +200,31 @@ func readFile(path string) ([]byte, error) {
return os.ReadFile(path)
}
// 获取可用主题列表
// ListThemes 获取所有可用主题(目录名)
func (m *ThemeManager) ListThemes() ([]string, error) {
dirs, err := os.ReadDir(m.themesDir)
entries, err := os.ReadDir(m.themesDir)
if err != nil {
return nil, err
return nil, fmt.Errorf("读取主题目录失败: %w", err)
}
var themes []string
for _, d := range dirs {
if d.IsDir() && isValidTheme(filepath.Join(m.themesDir, d.Name())) {
themes = append(themes, d.Name())
for _, entry := range entries {
if entry.IsDir() {
themes = append(themes, entry.Name())
}
}
return themes, nil
}
// GetAvailableThemes 获取所有可用主题(读取 themesDir 目录下的子目录)
func (m *ThemeManager) GetAvailableThemes() ([]string, error) {
entries, err := os.ReadDir(m.themesDir)
if err != nil {
return nil, fmt.Errorf("读取主题目录失败: %w", err)
}
var themes []string
for _, entry := range entries {
if entry.IsDir() {
themes = append(themes, entry.Name())
}
}
return themes, nil

View File

@@ -23,23 +23,23 @@ 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) {
// 从上下文中获取主题管理器(通过 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
}
// 从上下文中获取主题管理器(通过 main.go 的 themeMiddleware 注入)
tm, exists := c.Get("ThemeManager")
if !exists {
c.JSON(http.StatusInternalServerError, gin.H{"error": "主题管理器未找到"})
return
}
themeManager, ok := tm.(*ThemeManager)
if !ok {
c.JSON(http.StatusInternalServerError, gin.H{"error": "主题管理器类型错误"})
return
}
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": "主题切换成功"})
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": "主题切换成功"})
}