From 0b7f43b1493df932f2ffdedce0dd6ea4955276c2 Mon Sep 17 00:00:00 2001 From: UUBulb <35923940+uubulb@users.noreply.github.com> Date: Thu, 10 Oct 2024 13:38:09 +0800 Subject: [PATCH] fix dashboard custom theme, expose HideForGuest for api (#434) * fix: dashboard custom theme * api: expose HideForGuest --- cmd/dashboard/controller/controller.go | 29 ++++++++++++++++++-------- cmd/dashboard/controller/member_api.go | 2 +- service/singleton/api.go | 2 ++ 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/cmd/dashboard/controller/controller.go b/cmd/dashboard/controller/controller.go index 518769b..61806f7 100644 --- a/cmd/dashboard/controller/controller.go +++ b/cmd/dashboard/controller/controller.go @@ -84,7 +84,7 @@ func routers(r *gin.Engine) { } func loadThirdPartyTemplates(tmpl *template.Template) *template.Template { - var ret = tmpl + ret := tmpl themes, err := os.ReadDir("resource/template") if err != nil { log.Printf("NEZHA>> Error reading themes folder: %v", err) @@ -96,6 +96,12 @@ func loadThirdPartyTemplates(tmpl *template.Template) *template.Template { } themeDir := theme.Name() + if strings.HasPrefix(themeDir, "dashboard-") { + // load dashboard templates, ignore desc file + ret = loadTemplates(ret, themeDir) + continue + } + if !strings.HasPrefix(themeDir, "theme-") { log.Printf("NEZHA>> Invalid theme name: %s", themeDir) continue @@ -115,22 +121,27 @@ func loadThirdPartyTemplates(tmpl *template.Template) *template.Template { } // load templates - templatePath := filepath.Join("resource", "template", themeDir, "*.html") - t, err := ret.ParseGlob(templatePath) - if err != nil { - log.Printf("NEZHA>> Error parsing templates %s: %v", themeDir, err) - continue - } + ret = loadTemplates(ret, themeDir) themeKey := strings.TrimPrefix(themeDir, "theme-") model.Themes[themeKey] = themeName.String() - - ret = t } return ret } +func loadTemplates(tmpl *template.Template, themeDir string) *template.Template { + // load templates + templatePath := filepath.Join("resource", "template", themeDir, "*.html") + t, err := tmpl.ParseGlob(templatePath) + if err != nil { + log.Printf("NEZHA>> Error parsing templates %s: %v", themeDir, err) + return tmpl + } + + return t +} + var funcMap = template.FuncMap{ "tr": func(id string, dataAndCount ...interface{}) string { conf := i18n.LocalizeConfig{ diff --git a/cmd/dashboard/controller/member_api.go b/cmd/dashboard/controller/member_api.go index 3878e74..fde9866 100644 --- a/cmd/dashboard/controller/member_api.go +++ b/cmd/dashboard/controller/member_api.go @@ -940,7 +940,7 @@ func (ma *memberAPI) updateSetting(c *gin.Context) { return } - if _, yes := model.Themes[sf.DashboardTheme]; !yes { + if _, yes := model.DashboardThemes[sf.DashboardTheme]; !yes { c.JSON(http.StatusOK, model.Response{ Code: http.StatusBadRequest, Message: fmt.Sprintf("后台主题不存在:%s", sf.DashboardTheme), diff --git a/service/singleton/api.go b/service/singleton/api.go index 7e351d2..f86cb72 100644 --- a/service/singleton/api.go +++ b/service/singleton/api.go @@ -34,6 +34,7 @@ type CommonServerInfo struct { IPV6 string `json:"ipv6"` ValidIP string `json:"valid_ip"` DisplayIndex int `json:"display_index"` + HideForGuest bool `json:"hide_for_guest"` } // StatusResponse 服务器状态子结构 包含服务器信息与状态信息 @@ -150,6 +151,7 @@ func (s *ServerAPIService) GetAllStatus() *ServerStatusResponse { IPV6: ipv6, ValidIP: validIP, DisplayIndex: v.DisplayIndex, + HideForGuest: v.HideForGuest, } res.Result = append(res.Result, &StatusResponse{ CommonServerInfo: info,