base_common.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_forum_admin/services/alarm_msg"
  5. "net/http"
  6. "net/url"
  7. "github.com/beego/beego/v2/server/web"
  8. "eta/eta_forum_admin/models"
  9. "eta/eta_forum_admin/utils"
  10. )
  11. type BaseCommonController struct {
  12. web.Controller
  13. }
  14. func (c *BaseCommonController) ServeJSON(encoding ...bool) {
  15. // 方法处理完后,需要后置处理的业务逻辑
  16. //if handlerList, ok := AfterHandlerUrlMap[c.Ctx.Request.URL.Path]; ok {
  17. // for _, handler := range handlerList {
  18. // handler(c.Ctx.Input.RequestBody)
  19. // }
  20. //}
  21. var (
  22. hasIndent = false
  23. hasEncoding = false
  24. )
  25. if web.BConfig.RunMode == web.PROD {
  26. hasIndent = false
  27. }
  28. if len(encoding) > 0 && encoding[0] == true {
  29. hasEncoding = true
  30. }
  31. if c.Data["json"] == nil {
  32. go alarm_msg.SendAlarmMsg("接口:URI:"+c.Ctx.Input.URI()+";无返回值", 3)
  33. return
  34. }
  35. baseRes := c.Data["json"].(*models.BaseResponse)
  36. if baseRes != nil && baseRes.Ret != 408 {
  37. body, _ := json.Marshal(baseRes)
  38. var requestBody string
  39. method := c.Ctx.Input.Method()
  40. if method == "GET" {
  41. requestBody = c.Ctx.Request.RequestURI
  42. } else {
  43. requestBody, _ = url.QueryUnescape(string(c.Ctx.Input.RequestBody))
  44. }
  45. if baseRes.Ret != 200 && baseRes.IsSendEmail {
  46. //go utils.SendEmail(utils.APP_NAME_CN+"【"+utils.RunMode+"】"+"失败提醒", "URI:"+c.Ctx.Input.URI()+"<br/> "+"Params"+requestBody+" <br/>"+"ErrMsg:"+baseRes.ErrMsg+";<br/>Msg:"+baseRes.Msg+";<br/> Body:"+string(body)+"<br/>", utils.EmailSendToUsers)
  47. go alarm_msg.SendAlarmMsg("失败提醒:URI:"+c.Ctx.Input.URI()+";请求入参:Params"+requestBody+" ErrMsg:"+baseRes.ErrMsg+";Msg"+baseRes.Msg+"; Body:"+string(body), 3)
  48. }
  49. }
  50. c.JSON(c.Data["json"], hasIndent, hasEncoding)
  51. }
  52. func (c *BaseCommonController) JSON(data interface{}, hasIndent bool, coding bool) error {
  53. c.Ctx.Output.Header("Content-Type", "application/json; charset=utf-8")
  54. var content []byte
  55. var err error
  56. if hasIndent {
  57. content, err = json.MarshalIndent(data, "", " ")
  58. } else {
  59. content, err = json.Marshal(data)
  60. }
  61. if err != nil {
  62. http.Error(c.Ctx.Output.Context.ResponseWriter, err.Error(), http.StatusInternalServerError)
  63. return err
  64. }
  65. ip := c.Ctx.Input.IP()
  66. requestBody, err := url.QueryUnescape(string(c.Ctx.Input.RequestBody))
  67. if err != nil {
  68. requestBody = string(c.Ctx.Input.RequestBody)
  69. }
  70. if requestBody == "" {
  71. requestBody = c.Ctx.Input.URI()
  72. }
  73. authorization := c.Ctx.Input.Header("authorization")
  74. if authorization == "" {
  75. authorization = c.Ctx.Input.Header("Authorization")
  76. }
  77. utils.ApiLog.Info("uri:%s, authorization:%s, requestBody:%s, responseBody:%s, ip:%s", c.Ctx.Input.URI(), authorization, requestBody, content, ip)
  78. if utils.RunMode != "debug" {
  79. content = utils.DesBase64Encrypt(content, utils.DesKey)
  80. // get请求时,不加双引号就获取不到数据,不知道什么原因,所以还是在前后加上双引号吧
  81. content = []byte(`"` + string(content) + `"`)
  82. }
  83. if coding {
  84. content = []byte(utils.StringsToJSON(string(content)))
  85. }
  86. return c.Ctx.Output.Body(content)
  87. }