base_auth.go 4.2 KB

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