base_common.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_report/models"
  5. "eta/eta_report/services"
  6. "eta/eta_report/utils"
  7. "github.com/beego/beego/v2/server/web"
  8. "github.com/sirupsen/logrus"
  9. "net/http"
  10. "net/url"
  11. )
  12. type BaseCommonController struct {
  13. web.Controller
  14. Token string
  15. }
  16. func (this *BaseCommonController) Prepare() {
  17. }
  18. func (c *BaseCommonController) ServeJSON(encoding ...bool) {
  19. var (
  20. hasIndent = false
  21. hasEncoding = false
  22. )
  23. if web.BConfig.RunMode == web.PROD {
  24. hasIndent = false
  25. }
  26. if len(encoding) > 0 && encoding[0] == true {
  27. hasEncoding = true
  28. }
  29. if c.Data["json"] == nil {
  30. go utils.SendEmail(utils.APPNAME+" "+utils.RunMode+"异常提醒", "接口:"+"URI:"+c.Ctx.Input.URI()+";无返回值", utils.EmailSendToUsers)
  31. return
  32. }
  33. baseRes := c.Data["json"].(*models.BaseResponse)
  34. if baseRes != nil && !baseRes.Success && baseRes.IsSendEmail {
  35. go utils.SendEmail(utils.APPNAME+" "+utils.RunMode+" 失败提醒", "URI:"+c.Ctx.Input.URI()+" ErrMsg:"+baseRes.ErrMsg+";Msg"+baseRes.Msg, utils.EmailSendToUsers)
  36. }
  37. c.JSON(c.Data["json"], hasIndent, hasEncoding)
  38. }
  39. func (c *BaseCommonController) JSON(data interface{}, hasIndent bool, coding bool) error {
  40. c.Ctx.Output.Header("Content-Type", "application/json; charset=utf-8")
  41. var content []byte
  42. var err error
  43. if hasIndent {
  44. content, err = json.MarshalIndent(data, "", " ")
  45. } else {
  46. content, err = json.Marshal(data)
  47. }
  48. if err != nil {
  49. http.Error(c.Ctx.Output.Context.ResponseWriter, err.Error(), http.StatusInternalServerError)
  50. return err
  51. }
  52. ip := c.Ctx.Input.IP()
  53. requestBody, err := url.QueryUnescape(string(c.Ctx.Input.RequestBody))
  54. if err != nil {
  55. requestBody = string(c.Ctx.Input.RequestBody)
  56. }
  57. if requestBody == "" {
  58. requestBody = c.Ctx.Input.URI()
  59. }
  60. c.logUri(data, requestBody, ip)
  61. if coding {
  62. content = []byte(utils.StringsToJSON(string(content)))
  63. }
  64. // 数据加密
  65. if services.CheckEncryption() {
  66. content = utils.DesBase64Encrypt(content)
  67. // get请求时,不加双引号就获取不到数据,不知道什么原因,所以还是在前后加上双引号吧
  68. content = []byte(`"` + string(content) + `"`)
  69. }
  70. return c.Ctx.Output.Body(content)
  71. }
  72. func (c *BaseCommonController) logUri(data interface{}, requestBody, ip string) {
  73. var reqData interface{}
  74. err := json.Unmarshal([]byte(requestBody), &reqData)
  75. if err != nil {
  76. utils.ApiLog.WithFields(logrus.Fields{
  77. "uri": c.Ctx.Input.URI(),
  78. "requestBody": requestBody,
  79. "responseBody": data,
  80. "ip": ip,
  81. }).Info("请求详情")
  82. } else {
  83. utils.ApiLog.WithFields(logrus.Fields{
  84. "uri": c.Ctx.Input.URI(),
  85. "requestBody": reqData,
  86. "responseBody": data,
  87. "ip": ip,
  88. }).Info("请求详情")
  89. }
  90. return
  91. }