base_common.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. {
  16. lang := c.Ctx.Input.Header("Lang")
  17. if lang == "" {
  18. lang = utils.ZhLangVersion
  19. }
  20. c.Lang = lang
  21. }
  22. }
  23. func (c *BaseCommonController) ServeJSON(encoding ...bool) {
  24. var (
  25. hasIndent = false
  26. hasEncoding = false
  27. )
  28. if web.BConfig.RunMode == web.PROD {
  29. hasIndent = false
  30. }
  31. if len(encoding) > 0 && encoding[0] == true {
  32. hasEncoding = true
  33. }
  34. if c.Data["json"] == nil {
  35. go utils.SendEmail("异常提醒:", "接口:"+"URI:"+c.Ctx.Input.URI()+";无返回值", utils.EmailSendToUsers)
  36. return
  37. }
  38. baseRes := c.Data["json"].(*models.BaseResponse)
  39. if baseRes != nil && baseRes.Ret != 408 {
  40. body, _ := json.Marshal(baseRes)
  41. var requestBody string
  42. method := c.Ctx.Input.Method()
  43. if method == "GET" {
  44. requestBody = c.Ctx.Request.RequestURI
  45. } else {
  46. requestBody, _ = url.QueryUnescape(string(c.Ctx.Input.RequestBody))
  47. }
  48. if baseRes.Ret != 200 && baseRes.IsSendEmail {
  49. 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)
  50. }
  51. }
  52. c.JSON(c.Data["json"], hasIndent, hasEncoding)
  53. }
  54. func (c *BaseCommonController) JSON(data interface{}, hasIndent bool, coding bool) error {
  55. c.Ctx.Output.Header("Content-Type", "application/json; charset=utf-8")
  56. var content []byte
  57. var err error
  58. if hasIndent {
  59. content, err = json.MarshalIndent(data, "", " ")
  60. } else {
  61. content, err = json.Marshal(data)
  62. }
  63. if err != nil {
  64. http.Error(c.Ctx.Output.Context.ResponseWriter, err.Error(), http.StatusInternalServerError)
  65. return err
  66. }
  67. ip := c.Ctx.Input.IP()
  68. requestBody, err := url.QueryUnescape(string(c.Ctx.Input.RequestBody))
  69. if err != nil {
  70. requestBody = string(c.Ctx.Input.RequestBody)
  71. }
  72. if requestBody == "" {
  73. requestBody = c.Ctx.Input.URI()
  74. }
  75. authorization := c.Ctx.Input.Header("authorization")
  76. if authorization == "" {
  77. authorization = c.Ctx.Input.Header("Authorization")
  78. }
  79. utils.ApiLog.Info("uri:%s, authorization:%s, requestBody:%s, responseBody:%s, ip:%s", c.Ctx.Input.URI(), authorization, requestBody, content, ip)
  80. if coding {
  81. content = []byte(utils.StringsToJSON(string(content)))
  82. }
  83. return c.Ctx.Output.Body(content)
  84. }