feat(theme): 自定义主题

This commit is contained in:
naiba 2020-12-19 10:57:10 +08:00
parent a9b1ab9e95
commit 9dbbfac551
9 changed files with 48 additions and 5 deletions

View File

@ -22,6 +22,24 @@
sudo ./nezha.sh sudo ./nezha.sh
``` ```
## 主题自定义
- 默认主题更改进度条颜色示例
```
.ui.green.progress> .bar {
background-color: pink !important;
}
```
- hotaru 主题更改背景图片示例
```
.hotaru-cover {
background: url(https://s3.ax1x.com/2020/12/08/DzHv6A.jpg) center;
}
```
## 常见问题 ## 常见问题
### 启用 HTTPS ### 启用 HTTPS

View File

@ -25,7 +25,7 @@ var (
server string server string
clientSecret string clientSecret string
debug bool debug bool
version string version = "v9.9.9"
rootCmd = &cobra.Command{ rootCmd = &cobra.Command{
Use: "nezha-agent", Use: "nezha-agent",

View File

@ -29,7 +29,8 @@ func (cp *commonPage) home(c *gin.Context) {
dao.ServerLock.RLock() dao.ServerLock.RLock()
defer dao.ServerLock.RUnlock() defer dao.ServerLock.RUnlock()
data := gin.H{ data := gin.H{
"Servers": dao.ServerList, "Servers": dao.ServerList,
"CustomCSS": dao.Conf.Site.CustomCSS,
} }
u, ok := c.Get(model.CtxKeyAuthorizedUser) u, ok := c.Get(model.CtxKeyAuthorizedUser)
if ok { if ok {

View File

@ -3,6 +3,7 @@ package controller
import ( import (
"fmt" "fmt"
"html/template" "html/template"
"strings"
"time" "time"
"code.cloudfoundry.org/bytefmt" "code.cloudfoundry.org/bytefmt"
@ -24,6 +25,9 @@ func ServeWeb(port uint) {
"tf": func(t time.Time) string { "tf": func(t time.Time) string {
return t.Format("2006年1月2号") return t.Format("2006年1月2号")
}, },
"css": func(s string) template.CSS {
return template.CSS(s)
},
"stf": func(s uint64) string { "stf": func(s uint64) string {
return time.Unix(int64(s), 0).Format("2006年1月2号 15:04") return time.Unix(int64(s), 0).Format("2006年1月2号 15:04")
}, },
@ -33,6 +37,9 @@ func ServeWeb(port uint) {
"bf": func(b uint64) string { "bf": func(b uint64) string {
return bytefmt.ByteSize(b) return bytefmt.ByteSize(b)
}, },
"ts": func(s string) string {
return strings.TrimSpace(s)
},
}) })
r.Static("/static", "resource/static") r.Static("/static", "resource/static")
r.LoadHTMLGlob("resource/template/**/*") r.LoadHTMLGlob("resource/template/**/*")

View File

@ -127,9 +127,10 @@ func (ma *memberAPI) logout(c *gin.Context) {
} }
type settingForm struct { type settingForm struct {
Title string Title string
Admin string Admin string
Theme string Theme string
CustomCSS string
} }
func (ma *memberAPI) updateSetting(c *gin.Context) { func (ma *memberAPI) updateSetting(c *gin.Context) {
@ -143,6 +144,7 @@ func (ma *memberAPI) updateSetting(c *gin.Context) {
} }
dao.Conf.Site.Brand = sf.Title dao.Conf.Site.Brand = sf.Title
dao.Conf.Site.Theme = sf.Theme dao.Conf.Site.Theme = sf.Theme
dao.Conf.Site.CustomCSS = sf.CustomCSS
dao.Conf.GitHub.Admin = sf.Admin dao.Conf.GitHub.Admin = sf.Admin
if err := dao.Conf.Save(); err != nil { if err := dao.Conf.Save(); err != nil {
c.JSON(http.StatusOK, model.Response{ c.JSON(http.StatusOK, model.Response{

View File

@ -17,6 +17,7 @@ type Config struct {
Brand string // 站点名称 Brand string // 站点名称
CookieName string // 浏览器 Cookie 名称 CookieName string // 浏览器 Cookie 名称
Theme string Theme string
CustomCSS string
} }
GitHub struct { GitHub struct {
Admin string // 管理员ID列表 Admin string // 管理员ID列表

View File

@ -18,6 +18,10 @@
<option value="hotaru">CakeMine-Hotaru</option> <option value="hotaru">CakeMine-Hotaru</option>
</select> </select>
</div> </div>
<div class="field">
<label>自定义CSS</label>
<textarea name="CustomCSS" value="{{.Conf.Site.CustomCSS}}"></textarea>
</div>
<button class="ui button" type="submit">保存</button> <button class="ui button" type="submit">保存</button>
</form> </form>
</div> </div>

View File

@ -1,5 +1,10 @@
{{define "theme-default/home"}} {{define "theme-default/home"}}
{{template "common/header" .}} {{template "common/header" .}}
{{if ts .CustomCSS}}
<style>
{{.CustomCSS|css}}
</style>
{{end}}
{{template "common/menu" .}} {{template "common/menu" .}}
<div class="nb-container"> <div class="nb-container">
<div class="ui container"> <div class="ui container">

View File

@ -19,6 +19,11 @@
<link rel="stylesheet" href="/static/theme-hotaru/css/core.css?v202012092025" type="text/css"> <link rel="stylesheet" href="/static/theme-hotaru/css/core.css?v202012092025" type="text/css">
<link rel="stylesheet" href="/static/theme-hotaru/css/main.css?v202012092025" type="text/css"> <link rel="stylesheet" href="/static/theme-hotaru/css/main.css?v202012092025" type="text/css">
<link rel="stylesheet" href="/static/theme-hotaru/css/darkmode.css?v202012092025" type="text/css"> <link rel="stylesheet" href="/static/theme-hotaru/css/darkmode.css?v202012092025" type="text/css">
{{if ts .CustomCSS}}
<style>
{{.CustomCSS|css}}
</style>
{{end}}
</head> </head>
<body> <body>