base_common.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_mini_api/models"
  5. "eta/eta_mini_api/services"
  6. "eta/eta_mini_api/utils"
  7. "fmt"
  8. "net/http"
  9. "net/url"
  10. "strings"
  11. "github.com/beego/beego/v2/server/web"
  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. utils.ApiLog.Info("uri:%s, requestBody:%s, ip:%s", c.Ctx.Input.URI(), requestBody, ip)
  26. }
  27. func (c *BaseCommonController) ServeJSON(encoding ...bool) {
  28. var (
  29. hasIndent = false
  30. hasEncoding = false
  31. )
  32. if web.BConfig.RunMode == web.PROD {
  33. hasIndent = false
  34. }
  35. if len(encoding) > 0 && encoding[0] == true {
  36. hasEncoding = true
  37. }
  38. if c.Data["json"] == nil {
  39. utils.ApiLog.Notice(utils.APPNAME+" "+utils.RunMode+"异常提醒", "接口:"+"URI:"+c.Ctx.Input.URI()+";无返回值")
  40. return
  41. }
  42. baseRes := c.Data["json"].(*models.BaseResponse)
  43. if baseRes != nil && !baseRes.Success {
  44. utils.ApiLog.Notice("接口:" + "URI:" + c.Ctx.Input.URI() + ";ErrMsg:" + baseRes.ErrMsg + ";Msg" + baseRes.Msg)
  45. }
  46. c.JSON(c.Data["json"], hasIndent, hasEncoding)
  47. }
  48. func (c *BaseCommonController) JSON(data interface{}, hasIndent bool, coding bool) error {
  49. c.Ctx.Output.Header("Content-Type", "application/json; charset=utf-8")
  50. var content []byte
  51. var err error
  52. if hasIndent {
  53. content, err = json.MarshalIndent(data, "", " ")
  54. } else {
  55. content, err = json.Marshal(data)
  56. }
  57. if err != nil {
  58. http.Error(c.Ctx.Output.Context.ResponseWriter, err.Error(), http.StatusInternalServerError)
  59. return err
  60. }
  61. ip := c.Ctx.Input.IP()
  62. requestBody, _ := url.QueryUnescape(string(c.Ctx.Input.RequestBody))
  63. c.logUri(content, requestBody, ip)
  64. if coding {
  65. content = []byte(utils.StringsToJSON(string(content)))
  66. }
  67. // 数据加密
  68. if services.CheckEncryption() {
  69. content = utils.DesBase64Encrypt(content, utils.DesKey)
  70. // get请求时,不加双引号就获取不到数据,不知道什么原因,所以还是在前后加上双引号吧
  71. content = []byte(`"` + string(content) + `"`)
  72. }
  73. return c.Ctx.Output.Body(content)
  74. }
  75. func (c *BaseCommonController) logUri(respContent []byte, requestBody, ip string) {
  76. authorization := ""
  77. method := c.Ctx.Input.Method()
  78. uri := c.Ctx.Input.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, respContent, ip)
  112. return
  113. }