feat: 升级hook处理逻辑

This commit is contained in:
2025-11-01 17:16:11 +08:00
parent 954f6fef8a
commit d4052bcc4f
8 changed files with 95 additions and 42 deletions

View File

@ -78,7 +78,8 @@ func (s *server) RequestHandler(uriCfg UriConfig) gin.HandlerFunc {
// 非必传参数设置默认值
defaults.SetDefaults(formValue)
isSuccess := false
// 默认请求失败
ctx.Set(consts.GinRequestSuccess, false)
// 初始化响应之后logic
logicAfterResponse := &define.LogicAfterResponse{
SuccessHookFuncList: make([]func(), 0),
@ -86,30 +87,8 @@ func (s *server) RequestHandler(uriCfg UriConfig) gin.HandlerFunc {
Lock: &sync.RWMutex{},
}
// 此处暴露出去,是为了使用方可以获取到对应数据
ctx.Set(define.LogicAfterResponseKey, logicAfterResponse)
defer func() {
go func() {
// 执行响应之后的相关逻辑
defer func() {
if r := recover(); r != nil {
}
}()
if isSuccess {
for _, itemFunc := range logicAfterResponse.SuccessHookFuncList {
if nil != itemFunc {
itemFunc()
}
}
} else {
for _, itemFunc := range logicAfterResponse.FailureHookFuncList {
if nil != itemFunc {
itemFunc()
}
}
}
}()
}()
ctx.Set(consts.GinLogicAfterResponseKey, logicAfterResponse)
defer s.hook(ctx, uriCfg) // 执行Logic之后的相关逻辑
// 执行逻辑
if uriCfg.FormDataType.Kind() != reflect.Ptr {
inputValue = inputValue.Elem()
@ -118,8 +97,8 @@ func (s *server) RequestHandler(uriCfg UriConfig) gin.HandlerFunc {
firstParam = reflect.ValueOf(ctx)
resList := uriCfg.ApiLogicFunc.Func.Call([]reflect.Value{uriCfg.ApiStructValue, firstParam, inputValue})
if resList[1].IsNil() {
// 请求成功
isSuccess = true
// 请求成功, 更新标识
ctx.Set(consts.GinRequestSuccess, true)
response.SuccessWithExtension(ctx, resList[0].Interface(), &define.ResponseOption{ContentType: consts.MimeTypeJson})
return
}