base_auth.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_mini_bridge/models"
  5. "eta/eta_mini_bridge/utils"
  6. "fmt"
  7. "net/http"
  8. "net/url"
  9. "github.com/beego/beego/v2/server/web"
  10. )
  11. type BaseAuthController struct {
  12. web.Controller
  13. Token string
  14. }
  15. func (this *BaseAuthController) Prepare() {
  16. method := this.Ctx.Input.Method()
  17. uri := this.Ctx.Input.URI()
  18. fmt.Println("Url:", uri)
  19. header := this.Ctx.Request.Header
  20. headerBody, err := json.Marshal(header)
  21. fmt.Println(err)
  22. fmt.Println("header:" + string(headerBody))
  23. if method != "HEAD" {
  24. //校验签名
  25. nonce := this.Ctx.Input.Header("Nonce")
  26. timestamp := this.Ctx.Input.Header("Timestamp")
  27. appid := this.Ctx.Input.Header("Appid")
  28. signature := this.Ctx.Input.Header("Signature")
  29. if nonce == "" {
  30. errMsg := "随机字符串不能为空"
  31. this.JSON(models.BaseResponse{Ret: 400, Msg: "", ErrMsg: errMsg}, false, false)
  32. this.StopRun()
  33. return
  34. }
  35. if timestamp == "" {
  36. errMsg := "时间戳不能为空"
  37. this.JSON(models.BaseResponse{Ret: 400, Msg: "", ErrMsg: errMsg}, false, false)
  38. this.StopRun()
  39. return
  40. }
  41. checkSign := utils.GetSign(nonce, timestamp, appid, utils.ETA_MINI_API_SECRET)
  42. if signature != checkSign {
  43. errMsg := "签名错误"
  44. this.JSON(models.BaseResponse{Ret: 401, Msg: "", ErrMsg: errMsg}, false, false)
  45. this.StopRun()
  46. return
  47. }
  48. if method != "GET" && method != "POST" {
  49. errMsg := "无效的请求方式"
  50. this.JSON(models.BaseResponse{Ret: 501, Msg: "", ErrMsg: errMsg}, false, false)
  51. this.StopRun()
  52. return
  53. }
  54. } else {
  55. this.JSON(models.BaseResponse{Ret: 500, Msg: "系统异常,请联系客服!", ErrMsg: "method:" + method}, false, false)
  56. this.StopRun()
  57. return
  58. }
  59. }
  60. func (c *BaseAuthController) ServeJSON(encoding ...bool) {
  61. //所有请求都做这么个处理吧,目前这边都是做编辑、刷新逻辑处理(新增的话,并没有指标id,不会有影响)
  62. var (
  63. hasIndent = false
  64. hasEncoding = false
  65. )
  66. if web.BConfig.RunMode == web.PROD {
  67. hasIndent = false
  68. }
  69. if len(encoding) > 0 && encoding[0] == true {
  70. hasEncoding = true
  71. }
  72. if c.Data["json"] == nil {
  73. //go utils.SendEmail("异常提醒:", "接口:"+"URI:"+c.Ctx.Input.URI()+";无返回值", utils.EmailSendToUsers)
  74. //body := "接口:" + "URI:" + c.Ctx.Input.URI() + ";无返回值"
  75. //go alarm_msg.SendAlarmMsg(body, 1)
  76. return
  77. }
  78. baseRes := c.Data["json"].(*models.BaseResponse)
  79. if baseRes != nil && baseRes.Ret != 408 {
  80. //body, _ := json.Marshal(baseRes)
  81. //var requestBody string
  82. //method := c.Ctx.Input.Method()
  83. //if method == "GET" {
  84. // requestBody = c.Ctx.Request.RequestURI
  85. //} else {
  86. // requestBody, _ = url.QueryUnescape(string(c.Ctx.Input.RequestBody))
  87. //}
  88. //if baseRes.Ret != 200 && baseRes.IsSendEmail {
  89. // 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)
  90. //}
  91. // 记录错误日志, 并清掉错误信息避免暴露给外部
  92. if baseRes.ErrMsg != "" {
  93. utils.FileLog.Info(baseRes.ErrMsg)
  94. baseRes.ErrMsg = ""
  95. }
  96. }
  97. c.JSON(c.Data["json"], hasIndent, hasEncoding)
  98. }
  99. func (c *BaseAuthController) JSON(data interface{}, hasIndent bool, coding bool) error {
  100. c.Ctx.Output.Header("Content-Type", "application/json; charset=utf-8")
  101. var content []byte
  102. var err error
  103. if hasIndent {
  104. content, err = json.MarshalIndent(data, "", " ")
  105. } else {
  106. content, err = json.Marshal(data)
  107. }
  108. if err != nil {
  109. http.Error(c.Ctx.Output.Context.ResponseWriter, err.Error(), http.StatusInternalServerError)
  110. return err
  111. }
  112. ip := c.Ctx.Input.IP()
  113. requestBody, err := url.QueryUnescape(string(c.Ctx.Input.RequestBody))
  114. if err != nil {
  115. requestBody = string(c.Ctx.Input.RequestBody)
  116. }
  117. if requestBody == "" {
  118. requestBody = c.Ctx.Input.URI()
  119. }
  120. c.logUri(data, requestBody, ip)
  121. if coding {
  122. content = []byte(utils.StringsToJSON(string(content)))
  123. }
  124. return c.Ctx.Output.Body(content)
  125. }
  126. func (c *BaseAuthController) logUri(respContent interface{}, requestBody, ip string) {
  127. utils.ApiLog.Info("uri:%s, requestBody:%s, responseBody:%s, ip:%s", c.Ctx.Input.URI(), requestBody, respContent, ip)
  128. return
  129. }