base_open.go 4.5 KB

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