123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- package controllers
- import (
- "encoding/json"
- "errors"
- logger "eta/eta_mini_ht_api/common/component/log"
- "eta/eta_mini_ht_api/common/exception"
- "eta/eta_mini_ht_api/common/http"
- "github.com/beego/beego/v2/server/web"
- "github.com/google/uuid"
- "net/url"
- "strings"
- )
- const (
- TraceHeaderKey = "X-Trace-ID"
- MDCTraceId = "traceId"
- )
- type WebhookResponse struct {
- Ret int `description:"返回状态码"`
- Msg string
- ErrMsg string
- ErrCode int
- Data interface{} `json:"data,omitempty"`
- Success bool `description:"true 执行成功,false 执行失败"`
- TraceId string `json:"traceId"`
- }
- type WebHookController struct {
- web.Controller
- }
- type WebhookRequest struct {
- Data string
- EncryptKey string
- }
- // JsonResult /*
- func (b *WebHookController) JsonResult(status int, errCode int, errMsg string, msg string, success bool, data interface{}) {
- traceId := b.Ctx.Input.GetData(MDCTraceId).(string)
- retData := WebhookResponse{
- Ret: status,
- Msg: msg,
- ErrMsg: errMsg,
- ErrCode: errCode,
- Data: data,
- Success: success,
- TraceId: traceId}
- b.Ctx.Output.SetStatus(status)
- //content, err := json.Marshal(retData)
- //if err != nil {
- // logger.Error("加密失败")
- //} else {
- // if htConfig.NeedEncode() {
- // content = auth.DesBase64Encrypt(content, htConfig.GetDesCode())
- // }
- //}
- //fmt.Printf("%s", content)
- //b.Data["json"] = content
- b.Data["json"] = retData
- b.ServeJSON()
- }
- // Wrap ControllerWrap 是一个用于封装控制器方法的函数
- func WrapWebhook(a *WebHookController, fn func() (*WrapData, error)) {
- result, err := fn()
- if err != nil {
- logger.Error("%v", err)
- a.FailResponse(err, result.Msg)
- return
- }
- a.JsonResult(http.GetHttpStatusByAlias("ok"), http.ErrOK, "", result.Msg, http.Success, result.Data)
- }
- func (b *WebHookController) InitWrapData(msg string) *WrapData {
- return &WrapData{Msg: msg}
- }
- func (b *WebHookController) SuccessResult(msg string, data interface{}, wrapData *WrapData) {
- wrapData.Msg = msg
- wrapData.Data = data
- }
- func (b *WebHookController) FailedResult(msg string, wrapData *WrapData) {
- wrapData.Msg = msg
- }
- func (b *WebHookController) FailResponse(errInfo error, msg string) {
- var retData WebhookResponse
- var etaError *exception.EtaError
- traceId := b.Ctx.Input.GetData(MDCTraceId).(string)
- if !errors.As(errInfo, &etaError) {
- etaError = exception.New(exception.UnknownError)
- }
- retData = WebhookResponse{
- Ret: 200,
- Msg: msg,
- ErrMsg: etaError.ErrorMsg,
- ErrCode: etaError.ErrorCode,
- Data: nil,
- TraceId: traceId}
- b.Data["json"] = retData
- b.ServeJSON()
- }
- func (b *WebHookController) GetPostParams(data interface{}) {
- err := json.Unmarshal(b.Ctx.Input.RequestBody, data)
- if err != nil {
- logger.Error("解析请求参数失败:%v", err)
- data = nil
- }
- }
- func (b *WebHookController) Prepare() {
- var requestBody string
- uri := b.Ctx.Input.URI()
- method := b.Ctx.Input.Method()
- if method == "GET" {
- requestBody = b.Ctx.Request.RequestURI
- } else {
- requestBody, _ = url.QueryUnescape(string(b.Ctx.Input.RequestBody))
- }
- ip := b.Ctx.Input.IP()
- b.Ctx.Input.URL()
- traceId := uuid.NewString()
- traceId = strings.ReplaceAll(traceId, "-", "")
- b.Ctx.Output.Header(TraceHeaderKey, traceId)
- //// 设置MDC中的traceId
- b.Ctx.Input.SetData(MDCTraceId, traceId)
- // 清理MDC中的traceId
- logger.InfoWithTraceId(b.Ctx, "webhookRequest:[uri:%s, requestBody:%s, ip:%s]", uri, requestBody, ip)
- }
- func (b *WebHookController) Finish() {
- runMode := web.BConfig.RunMode
- if b.Data["json"] == nil {
- logger.WarnWithTraceId(b.Ctx, "webhookRequest:[异常提醒:%v 接口:URI:%v;无返回值]", runMode, b.Ctx.Input.URI())
- return
- }
- baseRes := b.Data["json"].(WebhookResponse)
- content, err := json.Marshal(baseRes)
- if err != nil {
- logger.Error("webhookRequest:[应答json格式化失败:%s]", err)
- }
- if !baseRes.Success {
- logger.InfoWithTraceId(b.Ctx, "webhookRequest:[异常提醒:%v接口:URI:%v;ErrMsg:&v;Msg:%v]", b.Ctx.Input.URI(), baseRes.ErrMsg, baseRes.Msg)
- } else {
- logger.InfoWithTraceId(b.Ctx, "webhookRequest:[uri:%s, resData:%s, ip:%s]", b.Ctx.Input.URI(), content)
- }
- }
|