58 lines
1.4 KiB
Go
58 lines
1.4 KiB
Go
// Package event ...
|
|
//
|
|
// Description : event ...
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 2023-02-02 15:15
|
|
package event
|
|
|
|
import (
|
|
"encoding/json"
|
|
"git.zhangdeman.cn/zhangdeman/event/abstract"
|
|
)
|
|
|
|
// SendEvent 发送事件
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 15:16 2023/2/2
|
|
func SendEvent(dataFlag string, data interface{}, preSendHandler abstract.IPreSendHandler, sendEventHandler abstract.ISendEventHandler) SendResult {
|
|
if nil == preSendHandler {
|
|
preSendHandler = &DefaultPreSendHandler{}
|
|
}
|
|
|
|
if nil == sendEventHandler {
|
|
sendEventHandler = &DefaultSendEventHandler{}
|
|
}
|
|
res := SendResult{}
|
|
|
|
if res.NoSendDetail, res.IsNeedSend = preSendHandler.NeedSend(); !res.IsNeedSend {
|
|
sendEventHandler.NoSendCallback(data, res)
|
|
return res
|
|
}
|
|
// 通过反射填充数据
|
|
ReflectValueInstance.Do(dataFlag, data, preSendHandler)
|
|
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)
|
|
return res
|
|
}
|
|
if res.SendResult, err = sendEventHandler.Send(byteData); nil != err {
|
|
sendEventHandler.FailCallback(res.SendResult, err)
|
|
return res
|
|
}
|
|
sendEventHandler.SuccessCallback(res.SendResult)
|
|
return res
|
|
}
|