base_auth.go 4.5 KB

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