This commit is contained in:
withchao
2023-02-14 15:02:45 +08:00
parent 8435508f35
commit 1ae1f77a14
4 changed files with 108 additions and 177 deletions
+18 -8
View File
@@ -25,26 +25,36 @@ type CommonCallbackReq struct {
Ex string `json:"ex"`
}
func (c *CommonCallbackReq) GetCallbackCommand() string {
return c.CallbackCommand
}
type CallbackReq interface {
GetCallbackCommand() string
}
type CallbackResp interface {
Parse() (err error)
}
type CommonCallbackResp struct {
ActionCode int `json:"actionCode"`
ErrCode int32 `json:"errCode"`
ErrMsg string `json:"errMsg"`
OperationID string `json:"operationID"`
ActionCode int `json:"actionCode"`
ErrCode int32 `json:"errCode"`
ErrMsg string `json:"errMsg"`
//OperationID string `json:"operationID"`
}
func (c *CommonCallbackResp) Parse() (err error) {
func (c *CommonCallbackResp) Parse() error {
if c == nil {
return constant.ErrData.Wrap("callback common is nil")
}
if c.ActionCode != constant.NoError || c.ErrCode != constant.NoError {
newErr := constant.ErrCallback
newErr.ErrCode = c.ErrCode
newErr.DetailErrMsg = fmt.Sprintf("callback response error actionCode is %d, errCode is %d, errMsg is %s", c.ActionCode, c.ErrCode, c.ErrMsg)
err = newErr
return
return newErr.Wrap()
}
return
return nil
}
type UserStatusBaseCallback struct {
+20
View File
@@ -78,3 +78,23 @@ func CallBackPostReturn(url, callbackCommand string, input interface{}, output c
}
return output.Parse()
}
func CallBackPostReturnV2(url string, req cbapi.CallbackReq, resp cbapi.CallbackResp, callbackConfig config.CallBackConfig) error {
v := urlLib.Values{}
v.Set("callbackCommand", req.GetCallbackCommand())
url = url + "?" + v.Encode()
b, err := Post(url, req, callbackConfig.CallbackTimeOut)
if err != nil {
if callbackConfig.CallbackFailedContinue != nil && *callbackConfig.CallbackFailedContinue {
return constant.ErrCallbackContinue
}
return constant.NewErrNetwork(err)
}
if err = json.Unmarshal(b, resp); err != nil {
if callbackConfig.CallbackFailedContinue != nil && *callbackConfig.CallbackFailedContinue {
return constant.ErrCallbackContinue
}
return constant.NewErrData(err)
}
return resp.Parse()
}
+8
View File
@@ -425,3 +425,11 @@ func Unwrap(err error) error {
}
return err
}
// NotNilReplace 当new_不为空时, 将old设置为new_
func NotNilReplace[T any](old, new_ *T) {
if old == nil || new_ == nil {
return
}
*old = *new_
}