base_common.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_mobile/models"
  5. "eta/eta_mobile/utils"
  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. baseRes := c.Data["json"].(*models.BaseResponse)
  41. if baseRes != nil && !baseRes.Success && baseRes.IsSendEmail {
  42. go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "URI:"+c.Ctx.Input.URI()+" ErrMsg:"+baseRes.ErrMsg+";Msg"+baseRes.Msg, utils.EmailSendToUsers)
  43. }
  44. c.JSON(c.Data["json"], hasIndent, hasEncoding)
  45. }
  46. func (c *BaseCommonController) JSON(data interface{}, hasIndent bool, coding bool) error {
  47. c.Ctx.Output.Header("Content-Type", "application/json; charset=utf-8")
  48. desEncrypt := utils.DesBase64Encrypt([]byte(utils.DesKey), utils.DesKeySalt)
  49. c.Ctx.Output.Header("Dk", string(desEncrypt)) // des3加解密key
  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. //params := c.Ctx.Input.Params()
  63. //fmt.Println("params")
  64. //fmt.Println(params)
  65. requestBody, _ := url.QueryUnescape(string(c.Ctx.Input.RequestBody))
  66. c.logUri(content, requestBody, ip)
  67. if coding {
  68. content = []byte(utils.StringsToJSON(string(content)))
  69. }
  70. // 如果不是debug分支的话,那么需要加密返回
  71. if utils.RunMode != "debug" {
  72. content = utils.DesBase64Encrypt(content, utils.DesKey)
  73. // get请求时,不加双引号就获取不到数据,不知道什么原因,所以还是在前后加上双引号吧
  74. content = []byte(`"` + string(content) + `"`)
  75. }
  76. if coding {
  77. content = []byte(utils.StringsToJSON(string(content)))
  78. }
  79. return c.Ctx.Output.Body(content)
  80. }
  81. func (c *BaseCommonController) logUri(respContent []byte, 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. utils.ApiLog.Info("uri:%s, authorization:%s, requestBody:%s, responseBody:%s, ip:%s", c.Ctx.Input.URI(), authorization, requestBody, respContent, ip)
  119. return
  120. }