2024-02-24 09:28:07 -05:00
|
|
|
package mygin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/naiba/nezha/model"
|
2024-02-25 01:15:19 -05:00
|
|
|
"github.com/naiba/nezha/pkg/utils"
|
2024-02-24 09:28:07 -05:00
|
|
|
"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 {
|
2024-02-25 01:15:19 -05:00
|
|
|
// 检验自定义主题
|
2024-02-25 20:31:16 -05:00
|
|
|
if theme == "custom" && singleton.Conf.Site.Theme != "custom" && !utils.IsFileExists("resource/template/theme-custom/home.html") {
|
2024-02-25 01:15:19 -05:00
|
|
|
return
|
|
|
|
}
|
2024-02-24 09:28:07 -05:00
|
|
|
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)
|
|
|
|
}
|