diff --git a/router/handler.go b/router/handler.go index 71dd4a7..f0a28dc 100644 --- a/router/handler.go +++ b/router/handler.go @@ -47,6 +47,7 @@ func RequestHandler(uriCfg UriConfig) gin.HandlerFunc { return } + isSuccess := false // 初始化响应之后logic logicAfterResponse := &define.LogicAfterResponse{ SuccessHookFuncList: make([]func(), 0), @@ -55,7 +56,24 @@ func RequestHandler(uriCfg UriConfig) gin.HandlerFunc { } // 此处暴露出去,是为了使用方可以获取到对应数据 ctx.Set(define.LogicAfterResponseKey, logicAfterResponse) - + defer func() { + go func() { + defer recover() + if isSuccess { + for _, itemFunc := range logicAfterResponse.SuccessHookFuncList { + if nil != itemFunc { + itemFunc() + } + } + } else { + for _, itemFunc := range logicAfterResponse.FailureHookFuncList { + if nil != itemFunc { + itemFunc() + } + } + } + }() + }() // 执行逻辑 inputValue := reflect.ValueOf(formValue) if uriCfg.FormDataType.Kind() != reflect.Ptr { @@ -64,13 +82,8 @@ func RequestHandler(uriCfg UriConfig) gin.HandlerFunc { resList := uriCfg.ApiLogicFunc.Func.Call([]reflect.Value{uriCfg.ApiStructValue, reflect.ValueOf(ctx), inputValue}) if resList[1].IsNil() { // 请求成功 + isSuccess = true response.Success(ctx, resList[0].Interface()) - // 执行成功之后的逻辑 - for _, item := range logicAfterResponse.SuccessHookFuncList { - if item != nil { - item() - } - } return } // 请求失败 @@ -84,12 +97,6 @@ func RequestHandler(uriCfg UriConfig) gin.HandlerFunc { }) } response.SendWithException(ctx, e, nil) - // 执行失败之后的逻辑 - for _, item := range logicAfterResponse.FailureHookFuncList { - if item != nil { - item() - } - } return } }