base_auth.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta_gn/eta_report/models"
  5. "eta_gn/eta_report/services"
  6. "eta_gn/eta_report/utils"
  7. "fmt"
  8. "github.com/beego/beego/v2/server/web"
  9. "net/http"
  10. )
  11. type BaseAuthController struct {
  12. web.Controller
  13. Token string
  14. }
  15. func (this *BaseAuthController) Prepare() {
  16. fmt.Println("enter prepare")
  17. method := this.Ctx.Input.Method()
  18. uri := this.Ctx.Input.URI()
  19. fmt.Println("Url:", uri)
  20. if method != "HEAD" {
  21. if method == "POST" {
  22. } else {
  23. this.JSON(models.BaseResponse{Ret: 408, Msg: "请求异常,请联系客服!", ErrMsg: "POST之外的请求,暂不支持"}, false, false)
  24. this.StopRun()
  25. return
  26. }
  27. } else {
  28. this.JSON(models.BaseResponse{Ret: 408, Msg: "请求异常,请联系客服!", ErrMsg: "POST之外的请求,暂不支持"}, false, false)
  29. this.StopRun()
  30. return
  31. }
  32. }
  33. func (c *BaseAuthController) ServeJSON(encoding ...bool) {
  34. var (
  35. hasIndent = false
  36. hasEncoding = false
  37. )
  38. if web.BConfig.RunMode == web.PROD {
  39. hasIndent = false
  40. }
  41. if len(encoding) > 0 && encoding[0] == true {
  42. hasEncoding = true
  43. }
  44. if c.Data["json"] == nil {
  45. go utils.SendEmail(utils.APPNAME+" "+utils.RunMode+"异常提醒:", "接口:"+"URI:"+c.Ctx.Input.URI()+";无返回值", utils.EmailSendToUsers)
  46. return
  47. }
  48. baseRes := c.Data["json"].(*models.BaseResponse)
  49. if baseRes != nil && baseRes.Ret != 200 && baseRes.Ret != 408 && baseRes.IsSendEmail {
  50. body, _ := json.Marshal(baseRes)
  51. go utils.SendEmail(utils.APPNAME+" "+utils.RunMode+" 失败提醒", "URI:"+c.Ctx.Input.URI()+"<br/> ErrMsg:"+baseRes.ErrMsg+";<br/>Msg:"+baseRes.Msg+";<br/> Body:"+string(body), utils.EmailSendToUsers)
  52. }
  53. c.JSON(c.Data["json"], hasIndent, hasEncoding)
  54. }
  55. func (c *BaseAuthController) JSON(data interface{}, hasIndent bool, coding bool) error {
  56. c.Ctx.Output.Header("Content-Type", "application/json; charset=utf-8")
  57. desEncrypt := utils.DesBase64Encrypt([]byte(utils.DesKey), utils.DesKeySalt)
  58. c.Ctx.Output.Header("Dk", string(desEncrypt)) // des3加解密key
  59. var content []byte
  60. var err error
  61. if hasIndent {
  62. content, err = json.MarshalIndent(data, "", " ")
  63. } else {
  64. content, err = json.Marshal(data)
  65. }
  66. if err != nil {
  67. http.Error(c.Ctx.Output.Context.ResponseWriter, err.Error(), http.StatusInternalServerError)
  68. return err
  69. }
  70. if coding {
  71. content = []byte(utils.StringsToJSON(string(content)))
  72. }
  73. if services.CheckEncryption() {
  74. content = utils.DesBase64Encrypt(content, utils.DesKey)
  75. content = []byte(`"` + string(content) + `"`)
  76. }
  77. return c.Ctx.Output.Body(content)
  78. }
  79. func (c *BaseAuthController) ServeJSONNoEncryption(encoding ...bool) {
  80. var (
  81. hasIndent = false
  82. hasEncoding = false
  83. )
  84. if web.BConfig.RunMode == web.PROD {
  85. hasIndent = false
  86. }
  87. if len(encoding) > 0 && encoding[0] == true {
  88. hasEncoding = true
  89. }
  90. if c.Data["json"] == nil {
  91. go utils.SendEmail(utils.APPNAME+" "+utils.RunMode+"异常提醒:", "接口:"+"URI:"+c.Ctx.Input.URI()+";无返回值", utils.EmailSendToUsers)
  92. return
  93. }
  94. baseRes := c.Data["json"].(*models.BaseResponse)
  95. if baseRes != nil && baseRes.Ret != 200 && baseRes.Ret != 408 && baseRes.IsSendEmail {
  96. body, _ := json.Marshal(baseRes)
  97. go utils.SendEmail(utils.APPNAME+" "+utils.RunMode+" 失败提醒", "URI:"+c.Ctx.Input.URI()+"<br/> ErrMsg:"+baseRes.ErrMsg+";<br/>Msg:"+baseRes.Msg+";<br/> Body:"+string(body), utils.EmailSendToUsers)
  98. }
  99. c.JSONNoEncryption(c.Data["json"], hasIndent, hasEncoding)
  100. }
  101. func (c *BaseAuthController) JSONNoEncryption(data interface{}, hasIndent bool, coding bool) error {
  102. c.Ctx.Output.Header("Content-Type", "application/json; charset=utf-8")
  103. var content []byte
  104. var err error
  105. if hasIndent {
  106. content, err = json.MarshalIndent(data, "", " ")
  107. } else {
  108. content, err = json.Marshal(data)
  109. }
  110. if err != nil {
  111. http.Error(c.Ctx.Output.Context.ResponseWriter, err.Error(), http.StatusInternalServerError)
  112. return err
  113. }
  114. if coding {
  115. content = []byte(utils.StringsToJSON(string(content)))
  116. }
  117. return c.Ctx.Output.Body(content)
  118. }