base_common.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/cache"
  5. "eta/eta_api/models"
  6. "eta/eta_api/services/alarm_msg"
  7. "eta/eta_api/utils"
  8. "fmt"
  9. "github.com/beego/beego/v2/server/web"
  10. "net/http"
  11. "net/url"
  12. "strings"
  13. )
  14. type BaseCommonController struct {
  15. web.Controller
  16. }
  17. func (c *BaseCommonController) Prepare() {
  18. var requestBody string
  19. method := c.Ctx.Input.Method()
  20. if method == "GET" {
  21. requestBody = c.Ctx.Request.RequestURI
  22. } else {
  23. requestBody, _ = url.QueryUnescape(string(c.Ctx.Input.RequestBody))
  24. }
  25. ip := c.Ctx.Input.IP()
  26. utils.ApiLog.Info("uri:%s, requestBody:%s, responseBody:%s, ip:%s", c.Ctx.Input.URI(), requestBody, ip)
  27. }
  28. func (c *BaseCommonController) ServeJSON(encoding ...bool) {
  29. var (
  30. hasIndent = false
  31. hasEncoding = false
  32. )
  33. if web.BConfig.RunMode == web.PROD {
  34. hasIndent = false
  35. }
  36. if len(encoding) > 0 && encoding[0] == true {
  37. hasEncoding = true
  38. }
  39. if c.Data["json"] == nil {
  40. //go utils.SendEmail("异常提醒:"+utils.RunMode, "接口:"+"URI:"+c.Ctx.Input.URI()+";无返回值", utils.EmailSendToUsers)
  41. go alarm_msg.SendAlarmMsg("接口:URI:"+c.Ctx.Input.URI()+";无返回值", 3)
  42. return
  43. }
  44. baseRes := c.Data["json"].(*models.BaseResponse)
  45. if baseRes != nil && !baseRes.Success && baseRes.IsSendEmail {
  46. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "URI:"+c.Ctx.Input.URI()+" ErrMsg:"+baseRes.ErrMsg+";Msg"+baseRes.Msg, utils.EmailSendToUsers)
  47. go alarm_msg.SendAlarmMsg("失败提醒:URI:"+c.Ctx.Input.URI()+" ErrMsg:"+baseRes.ErrMsg+";Msg"+baseRes.Msg, 3)
  48. }
  49. //新增uuid记录
  50. {
  51. urlPath := c.Ctx.Request.URL.Path
  52. if _, ok := AdminOperateRecordMap[urlPath]; !ok {
  53. var adminId int
  54. var realName, params, ip, uriStr string
  55. ip = c.Ctx.Input.IP()
  56. uuid := c.Ctx.Input.Header("Uuid")
  57. uri := c.Ctx.Input.URI()
  58. userAgent := c.Ctx.Input.Header("User-Agent")
  59. uriStr, _ = url.PathUnescape(uri)
  60. if uriStr != "" {
  61. uri = uriStr
  62. }
  63. if c.Ctx.Input.Method() == "GET" {
  64. paramsJson, _ := json.Marshal(c.Ctx.Input.Params())
  65. params = string(paramsJson)
  66. } else {
  67. params = string(c.Ctx.Input.RequestBody)
  68. }
  69. header := c.Ctx.Request.Header
  70. headerJson, _ := json.Marshal(header)
  71. headerStr := string(headerJson)
  72. go cache.AdminOperateRecord(adminId, realName, uuid, uri, params, ip, userAgent, headerStr)
  73. }
  74. }
  75. c.JSON(c.Data["json"], hasIndent, hasEncoding)
  76. }
  77. func (c *BaseCommonController) JSON(data interface{}, hasIndent bool, coding bool) error {
  78. c.Ctx.Output.Header("Content-Type", "application/json; charset=utf-8")
  79. desEncrypt := utils.DesBase64Encrypt([]byte(utils.DesKey), utils.DesKeySalt)
  80. c.Ctx.Output.Header("Dk", string(desEncrypt)) // des3加解密key
  81. var content []byte
  82. var err error
  83. if hasIndent {
  84. content, err = json.MarshalIndent(data, "", " ")
  85. } else {
  86. content, err = json.Marshal(data)
  87. }
  88. if err != nil {
  89. http.Error(c.Ctx.Output.Context.ResponseWriter, err.Error(), http.StatusInternalServerError)
  90. return err
  91. }
  92. ip := c.Ctx.Input.IP()
  93. params := c.Ctx.Input.Params()
  94. fmt.Println("params")
  95. fmt.Println(params)
  96. requestBody, _ := url.QueryUnescape(string(c.Ctx.Input.RequestBody))
  97. c.logUri(content, requestBody, ip)
  98. // 如果不是debug分支的话,那么需要加密返回
  99. if utils.RunMode != "debug" {
  100. content = utils.DesBase64Encrypt(content, utils.DesKey)
  101. // get请求时,不加双引号就获取不到数据,不知道什么原因,所以还是在前后加上双引号吧
  102. content = []byte(`"` + string(content) + `"`)
  103. }
  104. if coding {
  105. content = []byte(utils.StringsToJSON(string(content)))
  106. }
  107. return c.Ctx.Output.Body(content)
  108. }
  109. func (c *BaseCommonController) logUri(respContent []byte, requestBody, ip string) {
  110. authorization := ""
  111. method := c.Ctx.Input.Method()
  112. uri := c.Ctx.Input.URI()
  113. fmt.Println("Url:", uri)
  114. if method != "HEAD" {
  115. if method == "POST" || method == "GET" {
  116. authorization = c.Ctx.Input.Header("authorization")
  117. if authorization == "" {
  118. authorization = c.Ctx.Input.Header("Authorization")
  119. }
  120. if authorization == "" {
  121. newAuthorization := c.GetString("authorization")
  122. if newAuthorization != `` {
  123. authorization = "authorization=" + newAuthorization
  124. } else {
  125. newAuthorization = c.GetString("Authorization")
  126. authorization = "authorization=" + newAuthorization
  127. }
  128. } else {
  129. if strings.Contains(authorization, ";") {
  130. authorization = strings.Replace(authorization, ";", "$", 1)
  131. }
  132. }
  133. if authorization == "" {
  134. strArr := strings.Split(uri, "?")
  135. for k, v := range strArr {
  136. fmt.Println(k, v)
  137. }
  138. if len(strArr) > 1 {
  139. authorization = strArr[1]
  140. authorization = strings.Replace(authorization, "Authorization", "authorization", -1)
  141. fmt.Println(authorization)
  142. }
  143. }
  144. }
  145. }
  146. utils.ApiLog.Info("uri:%s, authorization:%s, requestBody:%s, responseBody:%s, ip:%s", c.Ctx.Input.URI(), authorization, requestBody, respContent, ip)
  147. return
  148. }