base_common.go 3.8 KB

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