base_auth.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_hub/models"
  5. "eta/eta_hub/utils"
  6. "fmt"
  7. "github.com/beego/beego/v2/server/web"
  8. "github.com/sirupsen/logrus"
  9. "net/http"
  10. "net/url"
  11. )
  12. type BaseAuthController struct {
  13. web.Controller
  14. }
  15. func (this *BaseAuthController) Prepare() {
  16. fmt.Println("enter prepare")
  17. method := this.Ctx.Input.Method()
  18. uri := this.Ctx.Input.URI()
  19. fmt.Println("Url:", uri)
  20. if method != "HEAD" {
  21. //校验签名
  22. //nonce := this.Ctx.Input.Header("nonce")
  23. //timestamp := this.Ctx.Input.Header("timestamp")
  24. //appid := this.Ctx.Input.Header("appid")
  25. //signature := this.Ctx.Input.Header("signature")
  26. //
  27. //if nonce == "" {
  28. // errMsg := "随机字符串不能为空"
  29. // this.JSON(models.BaseResponse{Ret: 400, Msg: "", ErrMsg: errMsg}, false, false)
  30. // this.StopRun()
  31. // return
  32. //}
  33. //
  34. //if timestamp == "" {
  35. // errMsg := "时间戳不能为空"
  36. // this.JSON(models.BaseResponse{Ret: 400, Msg: "", ErrMsg: errMsg}, false, false)
  37. // this.StopRun()
  38. // return
  39. //}
  40. //
  41. //if appid != utils.AppId {
  42. // errMsg := "商家AppId错误,请核查"
  43. // this.JSON(models.BaseResponse{Ret: 400, Msg: "", ErrMsg: errMsg}, false, false)
  44. // this.StopRun()
  45. // return
  46. //}
  47. //
  48. //checkSign := utils.GetSign(nonce, timestamp)
  49. //if signature != checkSign {
  50. // errMsg := "签名错误"
  51. // this.JSON(models.BaseResponse{Ret: 401, Msg: "", ErrMsg: errMsg}, false, false)
  52. // this.StopRun()
  53. // return
  54. //}
  55. if method != "GET" && method != "POST" {
  56. errMsg := "无效的请求方式"
  57. this.JSON(models.BaseResponse{Ret: 501, Msg: "", ErrMsg: errMsg}, false, false)
  58. this.StopRun()
  59. return
  60. }
  61. } else {
  62. this.JSON(models.BaseResponse{Ret: 500, Msg: "系统异常,请联系客服!", ErrMsg: "method:" + method}, false, false)
  63. this.StopRun()
  64. return
  65. }
  66. }
  67. func (c *BaseAuthController) ServeJSON(encoding ...bool) {
  68. //所有请求都做这么个处理吧,目前这边都是做编辑、刷新逻辑处理(新增的话,并没有指标id,不会有影响)
  69. var (
  70. hasIndent = false
  71. hasEncoding = false
  72. )
  73. if web.BConfig.RunMode == web.PROD {
  74. hasIndent = false
  75. }
  76. if len(encoding) > 0 && encoding[0] == true {
  77. hasEncoding = true
  78. }
  79. if c.Data["json"] == nil {
  80. //go utils.SendEmail("异常提醒:", "接口:"+"URI:"+c.Ctx.Input.URI()+";无返回值", utils.EmailSendToUsers)
  81. //body := "接口:" + "URI:" + c.Ctx.Input.URI() + ";无返回值"
  82. //go alarm_msg.SendAlarmMsg(body, 1)
  83. return
  84. }
  85. baseRes := c.Data["json"].(*models.BaseResponse)
  86. if baseRes != nil && baseRes.Ret != 408 {
  87. //body, _ := json.Marshal(baseRes)
  88. //var requestBody string
  89. //method := c.Ctx.Input.Method()
  90. //if method == "GET" {
  91. // requestBody = c.Ctx.Request.RequestURI
  92. //} else {
  93. // requestBody, _ = url.QueryUnescape(string(c.Ctx.Input.RequestBody))
  94. //}
  95. //if baseRes.Ret != 200 && baseRes.IsSendEmail {
  96. // 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)
  97. //}
  98. // 记录错误日志, 并清掉错误信息避免暴露给外部
  99. if baseRes.ErrMsg != "" {
  100. utils.FileLog.Info(baseRes.ErrMsg)
  101. baseRes.ErrMsg = ""
  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. c.logUri(data, requestBody, ip)
  128. if coding {
  129. content = []byte(utils.StringsToJSON(string(content)))
  130. }
  131. return c.Ctx.Output.Body(content)
  132. }
  133. func (c *BaseAuthController) logUri(data interface{}, requestBody, ip string) {
  134. var reqData interface{}
  135. err := json.Unmarshal([]byte(requestBody), &reqData)
  136. if err != nil {
  137. utils.ApiLog.WithFields(logrus.Fields{
  138. "uri": c.Ctx.Input.URI(),
  139. "requestBody": requestBody,
  140. "responseBody": data,
  141. "ip": ip,
  142. }).Info("请求详情")
  143. } else {
  144. utils.ApiLog.WithFields(logrus.Fields{
  145. "uri": c.Ctx.Input.URI(),
  146. "requestBody": reqData,
  147. "responseBody": data,
  148. "ip": ip,
  149. }).Info("请求详情")
  150. }
  151. return
  152. }