base_auth.go 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_report/models"
  5. "eta/eta_report/services"
  6. "eta/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. //authorization := this.Ctx.Input.Header("authorization")
  23. //if authorization == "" {
  24. // this.JSON(models.BaseResponse{Ret: 408, Msg: "请重新授权!", ErrMsg: "请重新授权:authorization is empty "}, false, false)
  25. // this.StopRun()
  26. // return
  27. //}
  28. //checkAuthorization := utils.MD5(utils.EtaReportAppNameEn + utils.EtaReportKey)
  29. //fmt.Println(checkAuthorization)
  30. //if authorization != checkAuthorization {
  31. // this.JSON(models.BaseResponse{Ret: 408, Msg: "签名错误!", ErrMsg: "签名错误:authorization is err "}, false, false)
  32. // this.StopRun()
  33. // return
  34. //}
  35. } else {
  36. this.JSON(models.BaseResponse{Ret: 408, Msg: "请求异常,请联系客服!", ErrMsg: "POST之外的请求,暂不支持"}, false, false)
  37. this.StopRun()
  38. return
  39. }
  40. } else {
  41. this.JSON(models.BaseResponse{Ret: 408, Msg: "请求异常,请联系客服!", ErrMsg: "POST之外的请求,暂不支持"}, false, false)
  42. this.StopRun()
  43. return
  44. }
  45. }
  46. func (c *BaseAuthController) ServeJSON(encoding ...bool) {
  47. var (
  48. hasIndent = false
  49. hasEncoding = false
  50. )
  51. if web.BConfig.RunMode == web.PROD {
  52. hasIndent = false
  53. }
  54. if len(encoding) > 0 && encoding[0] == true {
  55. hasEncoding = true
  56. }
  57. if c.Data["json"] == nil {
  58. go utils.SendEmail(utils.APPNAME+" "+utils.RunMode+"异常提醒:", "接口:"+"URI:"+c.Ctx.Input.URI()+";无返回值", utils.EmailSendToUsers)
  59. return
  60. }
  61. baseRes := c.Data["json"].(*models.BaseResponse)
  62. if baseRes != nil && baseRes.Ret != 200 && baseRes.Ret != 408 && baseRes.IsSendEmail {
  63. body, _ := json.Marshal(baseRes)
  64. 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)
  65. }
  66. c.JSON(c.Data["json"], hasIndent, hasEncoding)
  67. }
  68. func (c *BaseAuthController) JSON(data interface{}, hasIndent bool, coding bool) error {
  69. c.Ctx.Output.Header("Content-Type", "application/json; charset=utf-8")
  70. var content []byte
  71. var err error
  72. if hasIndent {
  73. content, err = json.MarshalIndent(data, "", " ")
  74. } else {
  75. content, err = json.Marshal(data)
  76. }
  77. if err != nil {
  78. http.Error(c.Ctx.Output.Context.ResponseWriter, err.Error(), http.StatusInternalServerError)
  79. return err
  80. }
  81. if coding {
  82. content = []byte(utils.StringsToJSON(string(content)))
  83. }
  84. // 数据加密
  85. if services.CheckEncryption() {
  86. content = utils.DesBase64Encrypt(content)
  87. // get请求时,不加双引号就获取不到数据,不知道什么原因,所以还是在前后加上双引号吧
  88. content = []byte(`"` + string(content) + `"`)
  89. }
  90. return c.Ctx.Output.Body(content)
  91. }