base_common.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta_gn/eta_chart_lib/utils"
  5. "fmt"
  6. "github.com/beego/beego/v2/server/web"
  7. "net/http"
  8. "net/url"
  9. "strings"
  10. )
  11. type BaseCommonController struct {
  12. web.Controller
  13. Lang string `description:"当前语言类型,中文:zh;英文:en;默认:zh"`
  14. }
  15. func (c *BaseCommonController) Prepare() {
  16. var requestBody string
  17. method := c.Ctx.Input.Method()
  18. if method == "GET" {
  19. requestBody = c.Ctx.Request.RequestURI
  20. } else {
  21. requestBody, _ = url.QueryUnescape(string(c.Ctx.Input.RequestBody))
  22. }
  23. {
  24. lang := c.Ctx.Input.Header("Lang")
  25. if lang == "" {
  26. lang = utils.ZhLangVersion
  27. }
  28. c.Lang = lang
  29. }
  30. ip := c.Ctx.Input.IP()
  31. utils.ApiLog.Info("uri:%s, requestBody:%s, responseBody:%s, ip:%s", c.Ctx.Input.URI(), requestBody, ip)
  32. }
  33. func (c *BaseCommonController) ServeJSON(encoding ...bool) {
  34. var (
  35. hasIndent = false
  36. hasEncoding = false
  37. )
  38. if web.BConfig.RunMode == web.PROD {
  39. hasIndent = false
  40. }
  41. if len(encoding) > 0 && encoding[0] == true {
  42. hasEncoding = true
  43. }
  44. if c.Data["json"] == nil {
  45. go utils.SendEmail("异常提醒:"+utils.RunMode, "接口:"+"URI:"+c.Ctx.Input.URI()+";无返回值", utils.EmailSendToUsers)
  46. return
  47. }
  48. c.JSON(c.Data["json"], hasIndent, hasEncoding)
  49. }
  50. func (c *BaseCommonController) JSON(data interface{}, hasIndent bool, coding bool) error {
  51. c.Ctx.Output.Header("Content-Type", "application/json; charset=utf-8")
  52. var content []byte
  53. var err error
  54. if hasIndent {
  55. content, err = json.MarshalIndent(data, "", " ")
  56. } else {
  57. content, err = json.Marshal(data)
  58. }
  59. if err != nil {
  60. http.Error(c.Ctx.Output.Context.ResponseWriter, err.Error(), http.StatusInternalServerError)
  61. return err
  62. }
  63. ip := c.Ctx.Input.IP()
  64. params := c.Ctx.Input.Params()
  65. fmt.Println("params")
  66. fmt.Println(params)
  67. requestBody, _ := url.QueryUnescape(string(c.Ctx.Input.RequestBody))
  68. c.logUri(content, requestBody, ip)
  69. if coding {
  70. content = []byte(utils.StringsToJSON(string(content)))
  71. }
  72. return c.Ctx.Output.Body(content)
  73. }
  74. func (c *BaseCommonController) logUri(content []byte, requestBody, ip string) {
  75. authorization := ""
  76. method := c.Ctx.Input.Method()
  77. uri := c.Ctx.Input.URI()
  78. fmt.Println("Url:", uri)
  79. if method != "HEAD" {
  80. if method == "POST" || method == "GET" {
  81. authorization = c.Ctx.Input.Header("authorization")
  82. if authorization == "" {
  83. authorization = c.Ctx.Input.Header("Authorization")
  84. }
  85. if authorization == "" {
  86. newAuthorization := c.GetString("authorization")
  87. if newAuthorization != `` {
  88. authorization = "authorization=" + newAuthorization
  89. } else {
  90. newAuthorization = c.GetString("Authorization")
  91. authorization = "authorization=" + newAuthorization
  92. }
  93. } else {
  94. if strings.Contains(authorization, ";") {
  95. authorization = strings.Replace(authorization, ";", "$", 1)
  96. }
  97. }
  98. if authorization == "" {
  99. strArr := strings.Split(uri, "?")
  100. for k, v := range strArr {
  101. fmt.Println(k, v)
  102. }
  103. if len(strArr) > 1 {
  104. authorization = strArr[1]
  105. authorization = strings.Replace(authorization, "Authorization", "authorization", -1)
  106. fmt.Println(authorization)
  107. }
  108. }
  109. }
  110. }
  111. utils.ApiLog.Info("uri:%s, authorization:%s, requestBody:%s, responseBody:%s, ip:%s", c.Ctx.Input.URI(), authorization, requestBody, content, ip)
  112. return
  113. }