base_auth.go 4.5 KB

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