2019-12-08 03:59:58 -05:00
|
|
|
package mygin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2022-04-29 21:32:57 -04:00
|
|
|
"github.com/nicksnyder/go-i18n/v2/i18n"
|
2019-12-08 03:59:58 -05:00
|
|
|
|
2020-11-10 21:07:45 -05:00
|
|
|
"github.com/naiba/nezha/model"
|
2022-01-08 22:54:14 -05:00
|
|
|
"github.com/naiba/nezha/service/singleton"
|
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,
|
2024-07-14 07:41:50 -04:00
|
|
|
"/nat": true,
|
2021-01-18 20:59:04 -05:00
|
|
|
"/cron": true,
|
2022-05-18 08:52:18 -04:00
|
|
|
"/api": 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")
|
2022-01-08 22:54:14 -05:00
|
|
|
data["Version"] = singleton.Version
|
|
|
|
data["Conf"] = singleton.Conf
|
2024-02-25 01:15:19 -05:00
|
|
|
data["Themes"] = model.Themes
|
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 {
|
2022-01-08 22:54:14 -05:00
|
|
|
data["Title"] = singleton.Conf.Site.Brand
|
2019-12-08 03:59:58 -05:00
|
|
|
} else {
|
2022-01-08 22:54:14 -05:00
|
|
|
data["Title"] = fmt.Sprintf("%s - %s", t, singleton.Conf.Site.Brand)
|
2019-12-08 03:59:58 -05:00
|
|
|
}
|
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
|
|
|
}
|
2022-04-29 21:32:57 -04:00
|
|
|
data["LANG"] = map[string]string{
|
|
|
|
"Add": singleton.Localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "Add"}),
|
|
|
|
"Edit": singleton.Localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "Edit"}),
|
|
|
|
"AlarmRule": singleton.Localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "AlarmRule"}),
|
|
|
|
"Notification": singleton.Localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "NotificationMethod"}),
|
|
|
|
"Server": singleton.Localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "Server"}),
|
|
|
|
"Monitor": singleton.Localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "ServicesManagement"}),
|
|
|
|
"Cron": singleton.Localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "ScheduledTasks"}),
|
|
|
|
}
|
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)
|
|
|
|
}
|