webhook_controller.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "errors"
  5. logger "eta/eta_mini_ht_api/common/component/log"
  6. "eta/eta_mini_ht_api/common/exception"
  7. "eta/eta_mini_ht_api/common/http"
  8. "github.com/beego/beego/v2/server/web"
  9. "github.com/google/uuid"
  10. "net/url"
  11. "strings"
  12. )
  13. const (
  14. TraceHeaderKey = "X-Trace-ID"
  15. MDCTraceId = "traceId"
  16. )
  17. type WebhookResponse struct {
  18. Ret int `description:"返回状态码"`
  19. Msg string
  20. ErrMsg string
  21. ErrCode int
  22. Data interface{} `json:"data,omitempty"`
  23. Success bool `description:"true 执行成功,false 执行失败"`
  24. TraceId string `json:"traceId"`
  25. }
  26. type WebHookController struct {
  27. web.Controller
  28. }
  29. type WebhookRequest struct {
  30. Data string
  31. EncryptKey string
  32. }
  33. // JsonResult /*
  34. func (b *WebHookController) JsonResult(status int, errCode int, errMsg string, msg string, success bool, data interface{}) {
  35. traceId := b.Ctx.Input.GetData(MDCTraceId).(string)
  36. retData := WebhookResponse{
  37. Ret: status,
  38. Msg: msg,
  39. ErrMsg: errMsg,
  40. ErrCode: errCode,
  41. Data: data,
  42. Success: success,
  43. TraceId: traceId}
  44. b.Ctx.Output.SetStatus(status)
  45. //content, err := json.Marshal(retData)
  46. //if err != nil {
  47. // logger.Error("加密失败")
  48. //} else {
  49. // if htConfig.NeedEncode() {
  50. // content = auth.DesBase64Encrypt(content, htConfig.GetDesCode())
  51. // }
  52. //}
  53. //fmt.Printf("%s", content)
  54. //b.Data["json"] = content
  55. b.Data["json"] = retData
  56. b.ServeJSON()
  57. }
  58. // Wrap ControllerWrap 是一个用于封装控制器方法的函数
  59. func WrapWebhook(a *WebHookController, fn func() (*WrapData, error)) {
  60. result, err := fn()
  61. if err != nil {
  62. logger.Error("%v", err)
  63. a.FailResponse(err, result.Msg)
  64. return
  65. }
  66. a.JsonResult(http.GetHttpStatusByAlias("ok"), http.ErrOK, "", result.Msg, http.Success, result.Data)
  67. }
  68. func (b *WebHookController) InitWrapData(msg string) *WrapData {
  69. return &WrapData{Msg: msg}
  70. }
  71. func (b *WebHookController) SuccessResult(msg string, data interface{}, wrapData *WrapData) {
  72. wrapData.Msg = msg
  73. wrapData.Data = data
  74. }
  75. func (b *WebHookController) FailedResult(msg string, wrapData *WrapData) {
  76. wrapData.Msg = msg
  77. }
  78. func (b *WebHookController) FailResponse(errInfo error, msg string) {
  79. var retData WebhookResponse
  80. var etaError *exception.EtaError
  81. traceId := b.Ctx.Input.GetData(MDCTraceId).(string)
  82. if !errors.As(errInfo, &etaError) {
  83. etaError = exception.New(exception.UnknownError)
  84. }
  85. retData = WebhookResponse{
  86. Ret: 200,
  87. Msg: msg,
  88. ErrMsg: etaError.ErrorMsg,
  89. ErrCode: etaError.ErrorCode,
  90. Data: nil,
  91. TraceId: traceId}
  92. b.Data["json"] = retData
  93. b.ServeJSON()
  94. }
  95. func (b *WebHookController) GetPostParams(data interface{}) {
  96. err := json.Unmarshal(b.Ctx.Input.RequestBody, data)
  97. if err != nil {
  98. logger.Error("解析请求参数失败:%v", err)
  99. data = nil
  100. }
  101. }
  102. func (b *WebHookController) Prepare() {
  103. var requestBody string
  104. uri := b.Ctx.Input.URI()
  105. method := b.Ctx.Input.Method()
  106. if method == "GET" {
  107. requestBody = b.Ctx.Request.RequestURI
  108. } else {
  109. requestBody, _ = url.QueryUnescape(string(b.Ctx.Input.RequestBody))
  110. }
  111. ip := b.Ctx.Input.IP()
  112. b.Ctx.Input.URL()
  113. traceId := uuid.NewString()
  114. traceId = strings.ReplaceAll(traceId, "-", "")
  115. b.Ctx.Output.Header(TraceHeaderKey, traceId)
  116. //// 设置MDC中的traceId
  117. b.Ctx.Input.SetData(MDCTraceId, traceId)
  118. // 清理MDC中的traceId
  119. logger.InfoWithTraceId(b.Ctx, "webhookRequest:[uri:%s, requestBody:%s, ip:%s]", uri, requestBody, ip)
  120. }
  121. func (b *WebHookController) Finish() {
  122. runMode := web.BConfig.RunMode
  123. if b.Data["json"] == nil {
  124. logger.WarnWithTraceId(b.Ctx, "webhookRequest:[异常提醒:%v 接口:URI:%v;无返回值]", runMode, b.Ctx.Input.URI())
  125. return
  126. }
  127. baseRes := b.Data["json"].(WebhookResponse)
  128. content, err := json.Marshal(baseRes)
  129. if err != nil {
  130. logger.Error("webhookRequest:[应答json格式化失败:%s]", err)
  131. }
  132. if !baseRes.Success {
  133. logger.InfoWithTraceId(b.Ctx, "webhookRequest:[异常提醒:%v接口:URI:%v;ErrMsg:&v;Msg:%v]", b.Ctx.Input.URI(), baseRes.ErrMsg, baseRes.Msg)
  134. } else {
  135. logger.InfoWithTraceId(b.Ctx, "webhookRequest:[uri:%s, resData:%s, ip:%s]", b.Ctx.Input.URI(), content)
  136. }
  137. }