base_common.go 3.0 KB

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