From 8b73672fa95ff69b6af0fa410fcfbd646cbe8251 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Thu, 2 Feb 2023 17:23:41 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0SendResult?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- define.go | 6 ++++++ send.go | 33 ++++++++++++++++++++------------- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/define.go b/define.go index 6e5bb67..0c083a8 100644 --- a/define.go +++ b/define.go @@ -100,4 +100,10 @@ type StructInfo struct { // // Date : 15:27 2023/2/2 type SendResult struct { + IsNeedSend bool `json:"is_need_send"` // 是否需要发送 + NoSendDetail map[string]interface{} `json:"no_send_detail"` // 未发送详细原因 + Data interface{} `json:"data"` // 发送的数据 + IsSuccess bool `json:"is_success"` // 是否处理成功 + FailReason string `json:"fail_reason"` // 失败原因 + SendResult map[string]interface{} `json:"send_result"` // 发送结果 } diff --git a/send.go b/send.go index 73b09c5..ef717c0 100644 --- a/send.go +++ b/send.go @@ -17,7 +17,7 @@ import ( // Author : go_developer@163.com<白茶清欢> // // Date : 15:16 2023/2/2 -func SendEvent(dataFlag string, data interface{}, preSendHandler abstract.IPreSendHandler, sendEventHandler abstract.ISendEventHandler) { +func SendEvent(dataFlag string, data interface{}, preSendHandler abstract.IPreSendHandler, sendEventHandler abstract.ISendEventHandler) SendResult { if nil == preSendHandler { preSendHandler = &DefaultPreSendHandler{} } @@ -25,26 +25,33 @@ func SendEvent(dataFlag string, data interface{}, preSendHandler abstract.IPreSe if nil == sendEventHandler { sendEventHandler = &DefaultSendEventHandler{} } - // noSendReason, isNeedSend := preSendHandler.NeedSend() - _, isNeedSend := preSendHandler.NeedSend() res := SendResult{} - if !isNeedSend { + + if res.NoSendDetail, res.IsNeedSend = preSendHandler.NeedSend(); !res.IsNeedSend { sendEventHandler.NoSendCallback(data, res) - return + return res } // 通过反射填充数据 ReflectValueInstance.Do(dataFlag, data, preSendHandler) - if byteData, err := json.Marshal(data); nil != err { + res.Data = data + var ( + byteData []byte + err error + ) + + if byteData, err = json.Marshal(data); nil != err { + res.IsSuccess = false + res.FailReason = err.Error() sendEventHandler.FailCallback(map[string]interface{}{ "err": err.Error(), "reason": "data marshal fail", }, err) - } else { - sendResult, sendErr := sendEventHandler.Send(byteData) - if nil != sendErr { - sendEventHandler.FailCallback(sendResult, sendErr) - return - } - sendEventHandler.SuccessCallback(sendResult) + return res } + if res.SendResult, err = sendEventHandler.Send(byteData); nil != err { + sendEventHandler.FailCallback(res.SendResult, err) + return res + } + sendEventHandler.SuccessCallback(res.SendResult) + return res }