From 99ac12c9fdc701df07a4e6269aef82702c637366 Mon Sep 17 00:00:00 2001 From: naiba Date: Sat, 24 Feb 2024 22:28:07 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20=E5=89=8D=E7=AB=AF=E5=88=87?= =?UTF-8?q?=E6=8D=A2=E6=A8=A1=E6=9D=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/dashboard/controller/common_page.go | 9 +++++---- model/common.go | 1 + pkg/mygin/preferred_theme.go | 25 +++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 4 deletions(-) create mode 100644 pkg/mygin/preferred_theme.go diff --git a/cmd/dashboard/controller/common_page.go b/cmd/dashboard/controller/common_page.go index 5e8b042..26b4119 100644 --- a/cmd/dashboard/controller/common_page.go +++ b/cmd/dashboard/controller/common_page.go @@ -44,6 +44,7 @@ type commonPage struct { func (cp *commonPage) serve() { cr := cp.r.Group("") cr.Use(mygin.Authorize(mygin.AuthorizeOption{})) + cr.Use(mygin.PreferredTheme) cr.GET("/terminal/:id", cp.terminal) cr.POST("/view-password", cp.issueViewPassword) cr.Use(cp.checkViewPassword) // 前端查看密码鉴权 @@ -98,7 +99,7 @@ func (p *commonPage) checkViewPassword(c *gin.Context) { // 验证查看密码 viewPassword, _ := c.Cookie(singleton.Conf.Site.CookieName + "-vp") if err := bcrypt.CompareHashAndPassword([]byte(viewPassword), []byte(singleton.Conf.Site.ViewPassword)); err != nil { - c.HTML(http.StatusOK, "theme-"+singleton.Conf.Site.Theme+"/viewpassword", mygin.CommonEnvironment(c, gin.H{ + c.HTML(http.StatusOK, mygin.GetPreferredTheme(c, "/viewpassword"), mygin.CommonEnvironment(c, gin.H{ "Title": singleton.Localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "VerifyPassword"}), "CustomCode": singleton.Conf.Site.CustomCode, })) @@ -128,7 +129,7 @@ func (p *commonPage) service(c *gin.Context) { stats, statsStore, }, nil }) - c.HTML(http.StatusOK, "theme-"+singleton.Conf.Site.Theme+"/service", mygin.CommonEnvironment(c, gin.H{ + c.HTML(http.StatusOK, mygin.GetPreferredTheme(c, "/service"), mygin.CommonEnvironment(c, gin.H{ "Title": singleton.Localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "ServicesStatus"}), "Services": res.([]interface{})[0], "CycleTransferStats": res.([]interface{})[1], @@ -234,7 +235,7 @@ func (cp *commonPage) network(c *gin.Context) { Servers: servers, }) - c.HTML(http.StatusOK, "theme-"+singleton.Conf.Site.Theme+"/network", mygin.CommonEnvironment(c, gin.H{ + c.HTML(http.StatusOK, mygin.GetPreferredTheme(c, "/network"), mygin.CommonEnvironment(c, gin.H{ "Servers": string(serversBytes), "MonitorInfos": string(monitorInfos), "CustomCode": singleton.Conf.Site.CustomCode, @@ -280,7 +281,7 @@ func (cp *commonPage) home(c *gin.Context) { }, true) return } - c.HTML(http.StatusOK, "theme-"+singleton.Conf.Site.Theme+"/home", mygin.CommonEnvironment(c, gin.H{ + c.HTML(http.StatusOK, mygin.GetPreferredTheme(c, "/home"), mygin.CommonEnvironment(c, gin.H{ "Servers": string(stat), "CustomCode": singleton.Conf.Site.CustomCode, })) diff --git a/model/common.go b/model/common.go index 3341e54..a14c264 100644 --- a/model/common.go +++ b/model/common.go @@ -8,6 +8,7 @@ import ( const CtxKeyAuthorizedUser = "ckau" const CtxKeyViewPasswordVerified = "ckvpv" +const CtxKeyPreferredTheme = "ckpt" const CacheKeyOauth2State = "p:a:state" type Common struct { diff --git a/pkg/mygin/preferred_theme.go b/pkg/mygin/preferred_theme.go new file mode 100644 index 0000000..be10a61 --- /dev/null +++ b/pkg/mygin/preferred_theme.go @@ -0,0 +1,25 @@ +package mygin + +import ( + "fmt" + + "github.com/gin-gonic/gin" + "github.com/naiba/nezha/model" + "github.com/naiba/nezha/service/singleton" +) + +func PreferredTheme(c *gin.Context) { + // 采用前端传入的主题 + if theme, err := c.Cookie("preferred_theme"); err == nil { + if _, has := model.Themes[theme]; has { + c.Set(model.CtxKeyPreferredTheme, theme) + } + } +} + +func GetPreferredTheme(c *gin.Context, path string) string { + if theme, has := c.Get(model.CtxKeyPreferredTheme); has { + return fmt.Sprintf("theme-%s%s", theme, path) + } + return fmt.Sprintf("theme-%s%s", singleton.Conf.Site.Theme, path) +}