base_auth.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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/eta_index_lib/models"
  9. "eta/eta_index_lib/utils"
  10. )
  11. type AfterHandle func(bodyByte []byte) error
  12. // AfterHandlerUrlMap 结束后待处理的url
  13. var AfterHandlerUrlMap = map[string][]AfterHandle{
  14. "/edbapi/wind/refresh": {models.DeleteChartInfoDataRedis}, //指标刷新接口
  15. }
  16. func init() {
  17. /*if utils.RunMode == "release" {
  18. logDir := `/data/rdlucklog/` + utils.APP_NAME_EN
  19. ApiLog = log.Init("20060102.api", logDir)
  20. } else {
  21. ApiLog = log.Init("20060102.api")
  22. }*/
  23. }
  24. type BaseAuthController struct {
  25. web.Controller
  26. }
  27. func (this *BaseAuthController) Prepare() {
  28. fmt.Println("enter prepare")
  29. method := this.Ctx.Input.Method()
  30. uri := this.Ctx.Input.URI()
  31. fmt.Println("Url:", uri)
  32. if method != "HEAD" {
  33. if method == "POST" {
  34. authorization := this.Ctx.Input.Header("authorization")
  35. if authorization == "" {
  36. this.JSON(models.BaseResponse{Ret: 408, Msg: "请重新授权!", ErrMsg: "请重新授权:authorization is empty "}, false, false)
  37. this.StopRun()
  38. return
  39. }
  40. checkAuthorization := utils.MD5(utils.APP_NAME_EN + utils.Md5Key)
  41. fmt.Println(checkAuthorization)
  42. if authorization != checkAuthorization {
  43. this.JSON(models.BaseResponse{Ret: 408, Msg: "签名错误!", ErrMsg: "签名错误:authorization is err "}, false, false)
  44. this.StopRun()
  45. return
  46. }
  47. } else {
  48. this.JSON(models.BaseResponse{Ret: 408, Msg: "请求异常,请联系客服!", ErrMsg: "POST之外的请求,暂不支持"}, false, false)
  49. this.StopRun()
  50. return
  51. }
  52. } else {
  53. this.JSON(models.BaseResponse{Ret: 408, Msg: "请求异常,请联系客服!", ErrMsg: "method:" + method}, false, false)
  54. this.StopRun()
  55. return
  56. }
  57. }
  58. func (c *BaseAuthController) ServeJSON(encoding ...bool) {
  59. // 方法处理完后,需要后置处理的业务逻辑
  60. //if handlerList, ok := AfterHandlerUrlMap[c.Ctx.Request.URL.Path]; ok {
  61. // for _, handler := range handlerList {
  62. // handler(c.Ctx.Input.RequestBody)
  63. // }
  64. //}
  65. //所有请求都做这么个处理吧,目前这边都是做编辑、刷新逻辑处理(新增的话,并没有指标id,不会有影响)
  66. models.DeleteChartInfoDataRedis(c.Ctx.Input.RequestBody)
  67. var (
  68. hasIndent = false
  69. hasEncoding = false
  70. )
  71. if web.BConfig.RunMode == web.PROD {
  72. hasIndent = false
  73. }
  74. if len(encoding) > 0 && encoding[0] == true {
  75. hasEncoding = true
  76. }
  77. if c.Data["json"] == nil {
  78. go utils.SendEmail("异常提醒:", "接口:"+"URI:"+c.Ctx.Input.URI()+";无返回值", utils.EmailSendToUsers)
  79. return
  80. }
  81. baseRes := c.Data["json"].(*models.BaseResponse)
  82. if baseRes != nil && baseRes.Ret != 408 {
  83. body, _ := json.Marshal(baseRes)
  84. var requestBody string
  85. method := c.Ctx.Input.Method()
  86. if method == "GET" {
  87. requestBody = c.Ctx.Request.RequestURI
  88. } else {
  89. requestBody, _ = url.QueryUnescape(string(c.Ctx.Input.RequestBody))
  90. }
  91. if baseRes.Ret != 200 && baseRes.IsSendEmail {
  92. 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)
  93. }
  94. }
  95. c.JSON(c.Data["json"], hasIndent, hasEncoding)
  96. }
  97. func (c *BaseAuthController) JSON(data interface{}, hasIndent bool, coding bool) error {
  98. c.Ctx.Output.Header("Content-Type", "application/json; charset=utf-8")
  99. var content []byte
  100. var err error
  101. if hasIndent {
  102. content, err = json.MarshalIndent(data, "", " ")
  103. } else {
  104. content, err = json.Marshal(data)
  105. }
  106. if err != nil {
  107. http.Error(c.Ctx.Output.Context.ResponseWriter, err.Error(), http.StatusInternalServerError)
  108. return err
  109. }
  110. ip := c.Ctx.Input.IP()
  111. requestBody, err := url.QueryUnescape(string(c.Ctx.Input.RequestBody))
  112. if err != nil {
  113. requestBody = string(c.Ctx.Input.RequestBody)
  114. }
  115. if requestBody == "" {
  116. requestBody = c.Ctx.Input.URI()
  117. }
  118. authorization := c.Ctx.Input.Header("authorization")
  119. if authorization == "" {
  120. authorization = c.Ctx.Input.Header("Authorization")
  121. }
  122. utils.ApiLog.Info("uri:%s, authorization:%s, requestBody:%s, responseBody:%s, ip:%s", c.Ctx.Input.URI(), authorization, requestBody, content, ip)
  123. if coding {
  124. content = []byte(utils.StringsToJSON(string(content)))
  125. }
  126. return c.Ctx.Output.Body(content)
  127. }