base_common.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. "github.com/sirupsen/logrus"
  9. "net/http"
  10. "net/url"
  11. "strings"
  12. )
  13. type BaseCommonController struct {
  14. web.Controller
  15. }
  16. func (c *BaseCommonController) Prepare() {
  17. var requestBody string
  18. method := c.Ctx.Input.Method()
  19. if method == "GET" {
  20. requestBody = c.Ctx.Request.RequestURI
  21. } else {
  22. requestBody, _ = url.QueryUnescape(string(c.Ctx.Input.RequestBody))
  23. }
  24. ip := c.Ctx.Input.IP()
  25. var reqData interface{}
  26. err := json.Unmarshal([]byte(requestBody), &reqData)
  27. if err != nil {
  28. utils.ApiLog.WithFields(logrus.Fields{
  29. "uri": c.Ctx.Input.URI(),
  30. "requestBody": requestBody,
  31. "ip": ip,
  32. }).Info("Prepare 请求详情")
  33. } else {
  34. utils.ApiLog.WithFields(logrus.Fields{
  35. "uri": c.Ctx.Input.URI(),
  36. "requestBody": reqData,
  37. "ip": ip,
  38. }).Info("Prepare 请求详情")
  39. }
  40. }
  41. func (c *BaseCommonController) ServeJSON(encoding ...bool) {
  42. var (
  43. hasIndent = false
  44. hasEncoding = false
  45. )
  46. if web.BConfig.RunMode == web.PROD {
  47. hasIndent = false
  48. }
  49. if len(encoding) > 0 && encoding[0] == true {
  50. hasEncoding = true
  51. }
  52. if c.Data["json"] == nil {
  53. msg := "接口:" + "URI:" + c.Ctx.Input.URI() + ";无返回值"
  54. go alarm_msg.SendAlarmMsg(msg, 3)
  55. return
  56. }
  57. c.JSON(c.Data["json"], hasIndent, hasEncoding)
  58. }
  59. func (c *BaseCommonController) JSON(data interface{}, hasIndent bool, coding bool) error {
  60. c.Ctx.Output.Header("Content-Type", "application/json; charset=utf-8")
  61. var content []byte
  62. var err error
  63. if hasIndent {
  64. content, err = json.MarshalIndent(data, "", " ")
  65. } else {
  66. content, err = json.Marshal(data)
  67. }
  68. if err != nil {
  69. http.Error(c.Ctx.Output.Context.ResponseWriter, err.Error(), http.StatusInternalServerError)
  70. return err
  71. }
  72. ip := c.Ctx.Input.IP()
  73. params := c.Ctx.Input.Params()
  74. fmt.Println("params")
  75. fmt.Println(params)
  76. requestBody, _ := url.QueryUnescape(string(c.Ctx.Input.RequestBody))
  77. c.logUri(data, requestBody, ip)
  78. if coding {
  79. content = []byte(utils.StringsToJSON(string(content)))
  80. }
  81. return c.Ctx.Output.Body(content)
  82. }
  83. func (c *BaseCommonController) logUri(data interface{}, requestBody, ip string) {
  84. authorization := ""
  85. method := c.Ctx.Input.Method()
  86. uri := c.Ctx.Input.URI()
  87. fmt.Println("Url:", uri)
  88. if method != "HEAD" {
  89. if method == "POST" || method == "GET" {
  90. authorization = c.Ctx.Input.Header("authorization")
  91. if authorization == "" {
  92. authorization = c.Ctx.Input.Header("Authorization")
  93. }
  94. if authorization == "" {
  95. newAuthorization := c.GetString("authorization")
  96. if newAuthorization != `` {
  97. authorization = "authorization=" + newAuthorization
  98. } else {
  99. newAuthorization = c.GetString("Authorization")
  100. authorization = "authorization=" + newAuthorization
  101. }
  102. } else {
  103. if strings.Contains(authorization, ";") {
  104. authorization = strings.Replace(authorization, ";", "$", 1)
  105. }
  106. }
  107. if authorization == "" {
  108. strArr := strings.Split(uri, "?")
  109. for k, v := range strArr {
  110. fmt.Println(k, v)
  111. }
  112. if len(strArr) > 1 {
  113. authorization = strArr[1]
  114. authorization = strings.Replace(authorization, "Authorization", "authorization", -1)
  115. fmt.Println(authorization)
  116. }
  117. }
  118. }
  119. }
  120. var reqData interface{}
  121. err := json.Unmarshal([]byte(requestBody), &reqData)
  122. if err != nil {
  123. utils.ApiLog.WithFields(logrus.Fields{
  124. "uri": c.Ctx.Input.URI(),
  125. "authorization": authorization,
  126. "requestBody": requestBody,
  127. "responseBody": data,
  128. "ip": ip,
  129. }).Info("请求详情")
  130. } else {
  131. utils.ApiLog.WithFields(logrus.Fields{
  132. "uri": c.Ctx.Input.URI(),
  133. "authorization": authorization,
  134. "requestBody": reqData,
  135. "responseBody": data,
  136. "ip": ip,
  137. }).Info("请求详情")
  138. }
  139. return
  140. }