2019-12-08 03:59:58 -05:00
|
|
|
package mygin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
2020-11-10 21:07:45 -05:00
|
|
|
"github.com/naiba/nezha/model"
|
|
|
|
"github.com/naiba/nezha/service/dao"
|
2019-12-08 03:59:58 -05:00
|
|
|
)
|
|
|
|
|
2021-01-15 22:42:12 -05:00
|
|
|
var adminPage = map[string]bool{
|
|
|
|
"/server": true,
|
|
|
|
"/monitor": true,
|
|
|
|
"/setting": true,
|
|
|
|
"/notification": true,
|
2021-01-18 20:59:04 -05:00
|
|
|
"/cron": true,
|
2021-01-15 22:42:12 -05:00
|
|
|
}
|
|
|
|
|
2019-12-08 03:59:58 -05:00
|
|
|
func CommonEnvironment(c *gin.Context, data map[string]interface{}) gin.H {
|
|
|
|
data["MatchedPath"] = c.MustGet("MatchedPath")
|
2019-12-09 10:45:23 -05:00
|
|
|
data["Version"] = dao.Version
|
2021-01-24 08:17:37 -05:00
|
|
|
data["Conf"] = dao.Conf
|
2021-01-15 22:42:12 -05:00
|
|
|
// 是否是管理页面
|
|
|
|
data["IsAdminPage"] = adminPage[data["MatchedPath"].(string)]
|
2019-12-08 03:59:58 -05:00
|
|
|
// 站点标题
|
|
|
|
if t, has := data["Title"]; !has {
|
|
|
|
data["Title"] = dao.Conf.Site.Brand
|
|
|
|
} else {
|
|
|
|
data["Title"] = fmt.Sprintf("%s - %s", t, dao.Conf.Site.Brand)
|
|
|
|
}
|
2019-12-20 10:58:09 -05:00
|
|
|
u, ok := c.Get(model.CtxKeyAuthorizedUser)
|
|
|
|
if ok {
|
|
|
|
data["Admin"] = u
|
2019-12-08 03:59:58 -05:00
|
|
|
}
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
|
|
|
func RecordPath(c *gin.Context) {
|
|
|
|
url := c.Request.URL.String()
|
|
|
|
for _, p := range c.Params {
|
|
|
|
url = strings.Replace(url, p.Value, ":"+p.Key, 1)
|
|
|
|
}
|
|
|
|
c.Set("MatchedPath", url)
|
|
|
|
}
|