base_auth.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. desEncrypt := utils.DesBase64Encrypt([]byte(utils.DesKey), utils.DesKeySalt)
  71. c.Ctx.Output.Header("Dk", string(desEncrypt)) // des3加解密key
  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. if coding {
  84. content = []byte(utils.StringsToJSON(string(content)))
  85. }
  86. // 数据加密
  87. if services.CheckEncryption() {
  88. content = utils.DesBase64Encrypt(content, utils.DesKey)
  89. // get请求时,不加双引号就获取不到数据,不知道什么原因,所以还是在前后加上双引号吧
  90. content = []byte(`"` + string(content) + `"`)
  91. }
  92. return c.Ctx.Output.Body(content)
  93. }
  94. func (c *BaseAuthController) ServeJSONNoEncryption(encoding ...bool) {
  95. var (
  96. hasIndent = false
  97. hasEncoding = false
  98. )
  99. if web.BConfig.RunMode == web.PROD {
  100. hasIndent = false
  101. }
  102. if len(encoding) > 0 && encoding[0] == true {
  103. hasEncoding = true
  104. }
  105. if c.Data["json"] == nil {
  106. go utils.SendEmail(utils.APPNAME+" "+utils.RunMode+"异常提醒:", "接口:"+"URI:"+c.Ctx.Input.URI()+";无返回值", utils.EmailSendToUsers)
  107. return
  108. }
  109. baseRes := c.Data["json"].(*models.BaseResponse)
  110. if baseRes != nil && baseRes.Ret != 200 && baseRes.Ret != 408 && baseRes.IsSendEmail {
  111. body, _ := json.Marshal(baseRes)
  112. 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)
  113. }
  114. c.JSONNoEncryption(c.Data["json"], hasIndent, hasEncoding)
  115. }
  116. func (c *BaseAuthController) JSONNoEncryption(data interface{}, hasIndent bool, coding bool) error {
  117. c.Ctx.Output.Header("Content-Type", "application/json; charset=utf-8")
  118. //desEncrypt := utils.DesBase64Encrypt([]byte(utils.DesKey), utils.DesKeySalt)
  119. //c.Ctx.Output.Header("Dk", string(desEncrypt)) // des3加解密key
  120. var content []byte
  121. var err error
  122. if hasIndent {
  123. content, err = json.MarshalIndent(data, "", " ")
  124. } else {
  125. content, err = json.Marshal(data)
  126. }
  127. if err != nil {
  128. http.Error(c.Ctx.Output.Context.ResponseWriter, err.Error(), http.StatusInternalServerError)
  129. return err
  130. }
  131. if coding {
  132. content = []byte(utils.StringsToJSON(string(content)))
  133. }
  134. //// 数据加密
  135. //if services.CheckEncryption() {
  136. // content = utils.DesBase64Encrypt(content, utils.DesKey)
  137. // // get请求时,不加双引号就获取不到数据,不知道什么原因,所以还是在前后加上双引号吧
  138. // content = []byte(`"` + string(content) + `"`)
  139. //}
  140. return c.Ctx.Output.Body(content)
  141. }