base_auth.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_forum_hub/models"
  5. "eta/eta_forum_hub/services/alarm_msg"
  6. "eta/eta_forum_hub/utils"
  7. "fmt"
  8. "net/http"
  9. "net/url"
  10. "github.com/beego/beego/v2/server/web"
  11. )
  12. func init() {
  13. }
  14. type BaseAuthController struct {
  15. web.Controller
  16. }
  17. func (this *BaseAuthController) Prepare() {
  18. fmt.Println("enter prepare")
  19. method := this.Ctx.Input.Method()
  20. uri := this.Ctx.Input.URI()
  21. fmt.Println("Url:", uri)
  22. if method != "HEAD" {
  23. if method == "POST" || method == "GET" {
  24. authorization := this.Ctx.Input.Header("authorization")
  25. if authorization == "" {
  26. this.JSON(models.BaseResponse{Ret: 408, Msg: "请重新授权!", ErrMsg: "请重新授权:authorization is empty "}, false, false)
  27. this.StopRun()
  28. return
  29. }
  30. checkAuthorization := utils.MD5(utils.APP_NAME_EN + utils.Md5Key)
  31. fmt.Println(checkAuthorization)
  32. if authorization != checkAuthorization {
  33. this.JSON(models.BaseResponse{Ret: 408, Msg: "签名错误!", ErrMsg: "签名错误:authorization is err "}, false, false)
  34. this.StopRun()
  35. return
  36. }
  37. } else {
  38. this.JSON(models.BaseResponse{Ret: 408, Msg: "请求异常,请联系客服!", ErrMsg: "POST之外的请求,暂不支持"}, false, false)
  39. this.StopRun()
  40. return
  41. }
  42. } else {
  43. this.JSON(models.BaseResponse{Ret: 408, Msg: "请求异常,请联系客服!", ErrMsg: "method:" + method}, false, false)
  44. this.StopRun()
  45. return
  46. }
  47. }
  48. func (c *BaseAuthController) ServeJSON(encoding ...bool) {
  49. // 方法处理完后,需要后置处理的业务逻辑
  50. //if handlerList, ok := AfterHandlerUrlMap[c.Ctx.Request.URL.Path]; ok {
  51. // for _, handler := range handlerList {
  52. // handler(c.Ctx.Input.RequestBody)
  53. // }
  54. //}
  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. body := "接口:" + "URI:" + c.Ctx.Input.URI() + ";无返回值"
  68. go alarm_msg.SendAlarmMsg(body, 1)
  69. return
  70. }
  71. baseRes := c.Data["json"].(*models.BaseResponse)
  72. if baseRes != nil && baseRes.Ret != 408 {
  73. body, _ := json.Marshal(baseRes)
  74. var requestBody string
  75. method := c.Ctx.Input.Method()
  76. if method == "GET" {
  77. requestBody = c.Ctx.Request.RequestURI
  78. } else {
  79. requestBody, _ = url.QueryUnescape(string(c.Ctx.Input.RequestBody))
  80. }
  81. if baseRes.Ret != 200 && baseRes.IsSendEmail {
  82. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "URI:"+c.Ctx.Input.URI()+"<br/> "+"Params"+requestBody+" <br/>"+"ErrMsg:"+baseRes.ErrMsg+";<br/>Msg:"+baseRes.Msg+";<br/> Body:"+string(body)+"<br/>"+c.SysUser.RealName, utils.EmailSendToUsers)
  83. tip := "URI:" + c.Ctx.Input.URI() + "<br/> " + "Params" + requestBody + " <br/>" + "ErrMsg:" + baseRes.ErrMsg + ";<br/>Msg:" + baseRes.Msg + ";<br/> Body:" + string(body)
  84. go alarm_msg.SendAlarmMsg(tip, 1)
  85. }
  86. }
  87. c.JSON(c.Data["json"], hasIndent, hasEncoding)
  88. }
  89. func (c *BaseAuthController) JSON(data interface{}, hasIndent bool, coding bool) error {
  90. c.Ctx.Output.Header("Content-Type", "application/json; charset=utf-8")
  91. var content []byte
  92. var err error
  93. if hasIndent {
  94. content, err = json.MarshalIndent(data, "", " ")
  95. } else {
  96. content, err = json.Marshal(data)
  97. }
  98. if err != nil {
  99. http.Error(c.Ctx.Output.Context.ResponseWriter, err.Error(), http.StatusInternalServerError)
  100. return err
  101. }
  102. ip := c.Ctx.Input.IP()
  103. requestBody, err := url.QueryUnescape(string(c.Ctx.Input.RequestBody))
  104. if err != nil {
  105. requestBody = string(c.Ctx.Input.RequestBody)
  106. }
  107. if requestBody == "" {
  108. requestBody = c.Ctx.Input.URI()
  109. }
  110. authorization := c.Ctx.Input.Header("authorization")
  111. if authorization == "" {
  112. authorization = c.Ctx.Input.Header("Authorization")
  113. }
  114. uri := c.Ctx.Input.URI()
  115. if uri == `/v1/chart/update` || uri == `/v1/chart/save` {
  116. utils.ApiLog.Info("uri:%s, authorization:%s, requestBody:%s, responseBody:%s, ip:%s", uri, authorization, "", content, ip)
  117. } else {
  118. utils.ApiLog.Info("uri:%s, authorization:%s, requestBody:%s, responseBody:%s, ip:%s", uri, authorization, requestBody, content, ip)
  119. }
  120. if coding {
  121. content = []byte(utils.StringsToJSON(string(content)))
  122. }
  123. return c.Ctx.Output.Body(content)
  124. }