base_common.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_mobile/models"
  5. "eta/eta_mobile/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. go utils.SendEmail("异常提醒:"+utils.RunMode, "接口:"+"URI:"+c.Ctx.Input.URI()+";无返回值", utils.EmailSendToUsers)
  54. return
  55. }
  56. baseRes := c.Data["json"].(*models.BaseResponse)
  57. if baseRes != nil && !baseRes.Success && baseRes.IsSendEmail {
  58. go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "URI:"+c.Ctx.Input.URI()+" ErrMsg:"+baseRes.ErrMsg+";Msg"+baseRes.Msg, utils.EmailSendToUsers)
  59. }
  60. c.JSON(c.Data["json"], hasIndent, hasEncoding)
  61. }
  62. func (c *BaseCommonController) JSON(data interface{}, hasIndent bool, coding bool) error {
  63. c.Ctx.Output.Header("Content-Type", "application/json; charset=utf-8")
  64. var content []byte
  65. var err error
  66. if hasIndent {
  67. content, err = json.MarshalIndent(data, "", " ")
  68. } else {
  69. content, err = json.Marshal(data)
  70. }
  71. if err != nil {
  72. http.Error(c.Ctx.Output.Context.ResponseWriter, err.Error(), http.StatusInternalServerError)
  73. return err
  74. }
  75. ip := c.Ctx.Input.IP()
  76. params := c.Ctx.Input.Params()
  77. fmt.Println("params")
  78. fmt.Println(params)
  79. requestBody, _ := url.QueryUnescape(string(c.Ctx.Input.RequestBody))
  80. c.logUri(data, requestBody, ip)
  81. if coding {
  82. content = []byte(utils.StringsToJSON(string(content)))
  83. }
  84. // 如果不是debug分支的话,那么需要加密返回
  85. if utils.RunMode != "debug" {
  86. content = utils.DesBase64Encrypt(content)
  87. // get请求时,不加双引号就获取不到数据,不知道什么原因,所以还是在前后加上双引号吧
  88. content = []byte(`"` + string(content) + `"`)
  89. }
  90. if coding {
  91. content = []byte(utils.StringsToJSON(string(content)))
  92. }
  93. return c.Ctx.Output.Body(content)
  94. }
  95. func (c *BaseCommonController) logUri(data interface{}, requestBody, ip string) {
  96. authorization := ""
  97. method := c.Ctx.Input.Method()
  98. uri := c.Ctx.Input.URI()
  99. fmt.Println("Url:", uri)
  100. if method != "HEAD" {
  101. if method == "POST" || method == "GET" {
  102. authorization = c.Ctx.Input.Header("authorization")
  103. if authorization == "" {
  104. authorization = c.Ctx.Input.Header("Authorization")
  105. }
  106. if authorization == "" {
  107. newAuthorization := c.GetString("authorization")
  108. if newAuthorization != `` {
  109. authorization = "authorization=" + newAuthorization
  110. } else {
  111. newAuthorization = c.GetString("Authorization")
  112. authorization = "authorization=" + newAuthorization
  113. }
  114. } else {
  115. if strings.Contains(authorization, ";") {
  116. authorization = strings.Replace(authorization, ";", "$", 1)
  117. }
  118. }
  119. if authorization == "" {
  120. strArr := strings.Split(uri, "?")
  121. for k, v := range strArr {
  122. fmt.Println(k, v)
  123. }
  124. if len(strArr) > 1 {
  125. authorization = strArr[1]
  126. authorization = strings.Replace(authorization, "Authorization", "authorization", -1)
  127. fmt.Println(authorization)
  128. }
  129. }
  130. }
  131. }
  132. var reqData interface{}
  133. err := json.Unmarshal([]byte(requestBody), &reqData)
  134. if err != nil {
  135. utils.ApiLog.WithFields(logrus.Fields{
  136. "uri": c.Ctx.Input.URI(),
  137. "authorization": authorization,
  138. "requestBody": requestBody,
  139. "responseBody": data,
  140. "ip": ip,
  141. }).Info("请求详情")
  142. } else {
  143. utils.ApiLog.WithFields(logrus.Fields{
  144. "uri": c.Ctx.Input.URI(),
  145. "authorization": authorization,
  146. "requestBody": reqData,
  147. "responseBody": data,
  148. "ip": ip,
  149. }).Info("请求详情")
  150. }
  151. return
  152. }