base_auth.go 4.8 KB

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