3x-ui/web/controller/base.go

38 lines
801 B
Go
Raw Normal View History

2023-02-09 22:18:06 +03:00
package controller
import (
"net/http"
2023-05-20 18:19:39 +03:00
"x-ui/logger"
"x-ui/web/locale"
2023-02-09 22:18:06 +03:00
"x-ui/web/session"
"github.com/gin-gonic/gin"
2023-02-09 22:18:06 +03:00
)
type BaseController struct {
}
func (a *BaseController) checkLogin(c *gin.Context) {
if !session.IsLogin(c) {
if isAjax(c) {
2023-05-21 01:59:27 +03:00
pureJsonMsg(c, false, I18nWeb(c, "pages.login.loginAgain"))
2023-02-09 22:18:06 +03:00
} else {
c.Redirect(http.StatusTemporaryRedirect, c.GetString("base_path"))
}
c.Abort()
} else {
c.Next()
}
}
2023-05-21 01:59:27 +03:00
func I18nWeb(c *gin.Context, name string, params ...string) string {
2023-05-20 18:19:39 +03:00
anyfunc, funcExists := c.Get("I18n")
if !funcExists {
logger.Warning("I18n function not exists in gin context!")
return ""
}
i18nFunc, _ := anyfunc.(func(i18nType locale.I18nType, key string, keyParams ...string) string)
msg := i18nFunc(locale.Web, name, params...)
return msg
2023-02-09 22:18:06 +03:00
}