base.go 825 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package controller
  2. import (
  3. "net/http"
  4. "x-ui/logger"
  5. "x-ui/web/locale"
  6. "x-ui/web/session"
  7. "github.com/gin-gonic/gin"
  8. )
  9. type BaseController struct{}
  10. func (a *BaseController) checkLogin(c *gin.Context) {
  11. if !session.IsLogin(c) {
  12. if isAjax(c) {
  13. pureJsonMsg(c, http.StatusUnauthorized, false, I18nWeb(c, "pages.login.loginAgain"))
  14. } else {
  15. c.Redirect(http.StatusTemporaryRedirect, c.GetString("base_path"))
  16. }
  17. c.Abort()
  18. } else {
  19. c.Next()
  20. }
  21. }
  22. func I18nWeb(c *gin.Context, name string, params ...string) string {
  23. anyfunc, funcExists := c.Get("I18n")
  24. if !funcExists {
  25. logger.Warning("I18n function not exists in gin context!")
  26. return ""
  27. }
  28. i18nFunc, _ := anyfunc.(func(i18nType locale.I18nType, key string, keyParams ...string) string)
  29. msg := i18nFunc(locale.Web, name, params...)
  30. return msg
  31. }