base_auth.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "net/url"
  7. "github.com/beego/beego/v2/server/web"
  8. "eta_gn/eta_index_lib/models"
  9. "eta_gn/eta_index_lib/utils"
  10. )
  11. type BaseAuthController struct {
  12. web.Controller
  13. Lang string `description:"当前语言类型,中文:zh;英文:en;默认:zh"`
  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. {
  23. lang := this.Ctx.Input.Header("Lang")
  24. if lang == "" {
  25. lang = utils.ZhLangVersion
  26. }
  27. this.Lang = lang
  28. }
  29. authorization := this.Ctx.Input.Header("authorization")
  30. if authorization == "" {
  31. this.JSON(models.BaseResponse{Ret: 408, Msg: "请重新授权!", ErrMsg: "请重新授权:authorization is empty "}, false, false)
  32. this.StopRun()
  33. return
  34. }
  35. checkAuthorization := utils.MD5(utils.APP_NAME_EN + utils.Md5Key)
  36. fmt.Println(checkAuthorization)
  37. if authorization != checkAuthorization {
  38. this.JSON(models.BaseResponse{Ret: 408, Msg: "签名错误!", ErrMsg: "签名错误:authorization is err "}, false, false)
  39. this.StopRun()
  40. return
  41. }
  42. } else {
  43. this.JSON(models.BaseResponse{Ret: 408, Msg: "请求异常,请联系客服!", ErrMsg: "POST之外的请求,暂不支持"}, false, false)
  44. this.StopRun()
  45. return
  46. }
  47. } else {
  48. this.JSON(models.BaseResponse{Ret: 408, Msg: "请求异常,请联系客服!", ErrMsg: "method:" + method}, false, false)
  49. this.StopRun()
  50. return
  51. }
  52. }
  53. func (c *BaseAuthController) ServeJSON(encoding ...bool) {
  54. models.DeleteChartInfoDataRedis(c.Ctx.Input.RequestBody)
  55. var (
  56. hasIndent = false
  57. hasEncoding = false
  58. )
  59. if web.BConfig.RunMode == web.PROD {
  60. hasIndent = false
  61. }
  62. if len(encoding) > 0 && encoding[0] == true {
  63. hasEncoding = true
  64. }
  65. if c.Data["json"] == nil {
  66. go utils.SendEmail("异常提醒:", "接口:"+"URI:"+c.Ctx.Input.URI()+";无返回值", utils.EmailSendToUsers)
  67. return
  68. }
  69. baseRes := c.Data["json"].(*models.BaseResponse)
  70. if baseRes != nil && baseRes.Ret != 408 {
  71. body, _ := json.Marshal(baseRes)
  72. var requestBody string
  73. method := c.Ctx.Input.Method()
  74. if method == "GET" {
  75. requestBody = c.Ctx.Request.RequestURI
  76. } else {
  77. requestBody, _ = url.QueryUnescape(string(c.Ctx.Input.RequestBody))
  78. }
  79. if baseRes.Ret != 200 && baseRes.IsSendEmail {
  80. 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)
  81. }
  82. }
  83. c.JSON(c.Data["json"], hasIndent, hasEncoding)
  84. }
  85. func (c *BaseAuthController) JSON(data interface{}, hasIndent bool, coding bool) error {
  86. c.Ctx.Output.Header("Content-Type", "application/json; charset=utf-8")
  87. var content []byte
  88. var err error
  89. if hasIndent {
  90. content, err = json.MarshalIndent(data, "", " ")
  91. } else {
  92. content, err = json.Marshal(data)
  93. }
  94. if err != nil {
  95. http.Error(c.Ctx.Output.Context.ResponseWriter, err.Error(), http.StatusInternalServerError)
  96. return err
  97. }
  98. ip := c.Ctx.Input.IP()
  99. requestBody, err := url.QueryUnescape(string(c.Ctx.Input.RequestBody))
  100. if err != nil {
  101. requestBody = string(c.Ctx.Input.RequestBody)
  102. }
  103. if requestBody == "" {
  104. requestBody = c.Ctx.Input.URI()
  105. }
  106. authorization := c.Ctx.Input.Header("authorization")
  107. if authorization == "" {
  108. authorization = c.Ctx.Input.Header("Authorization")
  109. }
  110. utils.ApiLog.Info("uri:%s, authorization:%s, requestBody:%s, responseBody:%s, ip:%s", c.Ctx.Input.URI(), authorization, requestBody, content, ip)
  111. if coding {
  112. content = []byte(utils.StringsToJSON(string(content)))
  113. }
  114. return c.Ctx.Output.Body(content)
  115. }