base_common.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/beego/beego/v2/server/web"
  6. "hongze/hz_crm_api/cache"
  7. "hongze/hz_crm_api/models"
  8. "hongze/hz_crm_api/utils"
  9. "net/http"
  10. "net/url"
  11. )
  12. type BaseCommonController struct {
  13. web.Controller
  14. }
  15. func (this *BaseCommonController) Prepare() {
  16. var requestBody string
  17. method := this.Ctx.Input.Method()
  18. if method == "GET" {
  19. requestBody = this.Ctx.Request.RequestURI
  20. } else {
  21. requestBody, _ = url.QueryUnescape(string(this.Ctx.Input.RequestBody))
  22. }
  23. ip := this.Ctx.Input.IP()
  24. apiLog.Println("请求地址:", this.Ctx.Input.URI(), "RequestBody:", requestBody, "IP:", ip)
  25. }
  26. func (c *BaseCommonController) ServeJSON(encoding ...bool) {
  27. var (
  28. hasIndent = false
  29. hasEncoding = false
  30. )
  31. if web.BConfig.RunMode == web.PROD {
  32. hasIndent = false
  33. }
  34. if len(encoding) > 0 && encoding[0] == true {
  35. hasEncoding = true
  36. }
  37. if c.Data["json"] == nil {
  38. go utils.SendEmail("异常提醒:"+utils.RunMode, "接口:"+"URI:"+c.Ctx.Input.URI()+";无返回值", utils.EmailSendToUsers)
  39. return
  40. }
  41. baseRes := c.Data["json"].(*models.BaseResponse)
  42. if baseRes != nil && !baseRes.Success && baseRes.IsSendEmail {
  43. go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "URI:"+c.Ctx.Input.URI()+" ErrMsg:"+baseRes.ErrMsg+";Msg"+baseRes.Msg, utils.EmailSendToUsers)
  44. }
  45. //新增uuid记录
  46. {
  47. urlPath := c.Ctx.Request.URL.Path
  48. if _, ok := AdminOperateRecordMap[urlPath]; !ok {
  49. var adminId int
  50. var realName, params, ip, uriStr string
  51. ip = c.Ctx.Input.IP()
  52. uuid := c.Ctx.Input.Header("Uuid")
  53. uri := c.Ctx.Input.URI()
  54. userAgent := c.Ctx.Input.Header("User-Agent")
  55. uriStr, _ = url.PathUnescape(uri)
  56. if uriStr != "" {
  57. uri = uriStr
  58. }
  59. if c.Ctx.Input.Method() == "GET" {
  60. paramsJson, _ := json.Marshal(c.Ctx.Input.Params())
  61. params = string(paramsJson)
  62. } else {
  63. params = string(c.Ctx.Input.RequestBody)
  64. }
  65. header := c.Ctx.Request.Header
  66. headerJson, _ := json.Marshal(header)
  67. headerStr := string(headerJson)
  68. go cache.AdminOperateRecord(adminId, realName, uuid, uri, params, ip, userAgent, headerStr)
  69. }
  70. }
  71. c.JSON(c.Data["json"], hasIndent, hasEncoding)
  72. }
  73. func (c *BaseCommonController) JSON(data interface{}, hasIndent bool, coding bool) error {
  74. c.Ctx.Output.Header("Content-Type", "application/json; charset=utf-8")
  75. var content []byte
  76. var err error
  77. if hasIndent {
  78. content, err = json.MarshalIndent(data, "", " ")
  79. } else {
  80. content, err = json.Marshal(data)
  81. }
  82. if err != nil {
  83. http.Error(c.Ctx.Output.Context.ResponseWriter, err.Error(), http.StatusInternalServerError)
  84. return err
  85. }
  86. ip := c.Ctx.Input.IP()
  87. params := c.Ctx.Input.Params()
  88. fmt.Println("params")
  89. fmt.Println(params)
  90. requestBody, _ := url.QueryUnescape(string(c.Ctx.Input.RequestBody))
  91. apiLog.Println("请求地址:", c.Ctx.Input.URI(), "Authorization:", c.Ctx.Input.Header("Authorization"), "RequestBody:", requestBody, "ResponseBody", string(content), "IP:", ip)
  92. // 如果不是debug分支的话,那么需要加密返回
  93. if utils.RunMode != "debug" {
  94. content = utils.DesBase64Encrypt(content)
  95. // get请求时,不加双引号就获取不到数据,不知道什么原因,所以还是在前后加上双引号吧
  96. content = []byte(`"` + string(content) + `"`)
  97. }
  98. if coding {
  99. content = []byte(utils.StringsToJSON(string(content)))
  100. }
  101. return c.Ctx.Output.Body(content)
  102. }