base_common.go 3.1 KB

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