2023-02-09 11:18:06 -08:00
|
|
|
package controller
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net/http"
|
2024-03-10 14:31:24 -07:00
|
|
|
|
2025-09-19 01:05:43 -07:00
|
|
|
"github.com/mhsanaei/3x-ui/v2/logger"
|
|
|
|
|
"github.com/mhsanaei/3x-ui/v2/web/locale"
|
|
|
|
|
"github.com/mhsanaei/3x-ui/v2/web/session"
|
2023-04-29 08:17:44 -07:00
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2023-02-09 11:18:06 -08:00
|
|
|
)
|
|
|
|
|
|
2024-03-10 14:31:24 -07:00
|
|
|
type BaseController struct{}
|
2023-02-09 11:18:06 -08:00
|
|
|
|
|
|
|
|
func (a *BaseController) checkLogin(c *gin.Context) {
|
|
|
|
|
if !session.IsLogin(c) {
|
|
|
|
|
if isAjax(c) {
|
2024-03-10 14:31:24 -07:00
|
|
|
pureJsonMsg(c, http.StatusUnauthorized, false, I18nWeb(c, "pages.login.loginAgain"))
|
2023-02-09 11:18:06 -08:00
|
|
|
} else {
|
|
|
|
|
c.Redirect(http.StatusTemporaryRedirect, c.GetString("base_path"))
|
|
|
|
|
}
|
|
|
|
|
c.Abort()
|
|
|
|
|
} else {
|
|
|
|
|
c.Next()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-20 15:59:27 -07:00
|
|
|
func I18nWeb(c *gin.Context, name string, params ...string) string {
|
2023-05-20 08:19:39 -07: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 11:18:06 -08:00
|
|
|
}
|