base_common.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "net/url"
  6. "github.com/beego/beego/v2/server/web"
  7. "eta_gn/eta_index_lib/models"
  8. "eta_gn/eta_index_lib/utils"
  9. )
  10. type BaseCommonController struct {
  11. web.Controller
  12. Lang string `description:"当前语言类型,中文:zh;英文:en;默认:zh"`
  13. }
  14. func (c *BaseCommonController) Prepare() {
  15. //var requestBody string
  16. //method := c.Ctx.Input.Method()
  17. //if method == "GET" {
  18. // requestBody = c.Ctx.Request.RequestURI
  19. //} else {
  20. // requestBody, _ = url.QueryUnescape(string(c.Ctx.Input.RequestBody))
  21. //}
  22. // 当前语言
  23. {
  24. lang := c.Ctx.Input.Header("Lang")
  25. if lang == "" {
  26. lang = utils.ZhLangVersion
  27. }
  28. c.Lang = lang
  29. }
  30. //ip := c.Ctx.Input.IP()
  31. //utils.ApiLog.Info("uri:%s, requestBody:%s, responseBody:%s, ip:%s", c.Ctx.Input.URI(), requestBody, ip)
  32. }
  33. func (c *BaseCommonController) ServeJSON(encoding ...bool) {
  34. // 方法处理完后,需要后置处理的业务逻辑
  35. //if handlerList, ok := AfterHandlerUrlMap[c.Ctx.Request.URL.Path]; ok {
  36. // for _, handler := range handlerList {
  37. // handler(c.Ctx.Input.RequestBody)
  38. // }
  39. //}
  40. var (
  41. hasIndent = false
  42. hasEncoding = false
  43. )
  44. if web.BConfig.RunMode == web.PROD {
  45. hasIndent = false
  46. }
  47. if len(encoding) > 0 && encoding[0] == true {
  48. hasEncoding = true
  49. }
  50. if c.Data["json"] == nil {
  51. go utils.SendEmail("异常提醒:", "接口:"+"URI:"+c.Ctx.Input.URI()+";无返回值", utils.EmailSendToUsers)
  52. return
  53. }
  54. baseRes := c.Data["json"].(*models.BaseResponse)
  55. if baseRes != nil && baseRes.Ret != 408 {
  56. body, _ := json.Marshal(baseRes)
  57. var requestBody string
  58. method := c.Ctx.Input.Method()
  59. if method == "GET" {
  60. requestBody = c.Ctx.Request.RequestURI
  61. } else {
  62. requestBody, _ = url.QueryUnescape(string(c.Ctx.Input.RequestBody))
  63. }
  64. if baseRes.Ret != 200 && baseRes.IsSendEmail {
  65. 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)
  66. }
  67. }
  68. c.JSON(c.Data["json"], hasIndent, hasEncoding)
  69. }
  70. func (c *BaseCommonController) JSON(data interface{}, hasIndent bool, coding bool) error {
  71. c.Ctx.Output.Header("Content-Type", "application/json; charset=utf-8")
  72. var content []byte
  73. var err error
  74. if hasIndent {
  75. content, err = json.MarshalIndent(data, "", " ")
  76. } else {
  77. content, err = json.Marshal(data)
  78. }
  79. if err != nil {
  80. http.Error(c.Ctx.Output.Context.ResponseWriter, err.Error(), http.StatusInternalServerError)
  81. return err
  82. }
  83. ip := c.Ctx.Input.IP()
  84. requestBody, err := url.QueryUnescape(string(c.Ctx.Input.RequestBody))
  85. if err != nil {
  86. requestBody = string(c.Ctx.Input.RequestBody)
  87. }
  88. if requestBody == "" {
  89. requestBody = c.Ctx.Input.URI()
  90. }
  91. authorization := c.Ctx.Input.Header("authorization")
  92. if authorization == "" {
  93. authorization = c.Ctx.Input.Header("Authorization")
  94. }
  95. utils.ApiLog.Info("uri:%s, authorization:%s, requestBody:%s, responseBody:%s, ip:%s", c.Ctx.Input.URI(), authorization, requestBody, content, ip)
  96. if coding {
  97. content = []byte(utils.StringsToJSON(string(content)))
  98. }
  99. return c.Ctx.Output.Body(content)
  100. }