base.go 801 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. }
  11. func (a *BaseController) checkLogin(c *gin.Context) {
  12. if !session.IsLogin(c) {
  13. if isAjax(c) {
  14. pureJsonMsg(c, false, I18nWeb(c, "pages.login.loginAgain"))
  15. } else {
  16. c.Redirect(http.StatusTemporaryRedirect, c.GetString("base_path"))
  17. }
  18. c.Abort()
  19. } else {
  20. c.Next()
  21. }
  22. }
  23. func I18nWeb(c *gin.Context, name string, params ...string) string {
  24. anyfunc, funcExists := c.Get("I18n")
  25. if !funcExists {
  26. logger.Warning("I18n function not exists in gin context!")
  27. return ""
  28. }
  29. i18nFunc, _ := anyfunc.(func(i18nType locale.I18nType, key string, keyParams ...string) string)
  30. msg := i18nFunc(locale.Web, name, params...)
  31. return msg
  32. }