diff --git a/define/response.go b/define/response.go index 02fd489..2ad67de 100644 --- a/define/response.go +++ b/define/response.go @@ -34,6 +34,8 @@ type HttpHandleConfig struct { StartRequestTimeField string FinishRequestTimeField string RequestIsSuccessField string // 请求处理是否成功的标识 + ExtensionOutputField string // 扩展信息对外输出字段 + EnableExtensionOutput bool } // ConvertDefaultConfig 覆盖默认配置 @@ -112,5 +114,10 @@ func GetHttpHandleConfig() *HttpHandleConfig { consts.GinRequestIsSuccessField, wrapper.String(inputHttpHandleConfig.RecordResponseDataField), ).Value(), + ExtensionOutputField: wrapper.TernaryOperator.String( + nil == inputHttpHandleConfig || inputHttpHandleConfig.ExtensionOutputField == "", + consts.GinResponseExtensionField, + wrapper.String(inputHttpHandleConfig.RecordResponseDataField), + ).Value(), } } diff --git a/go.mod b/go.mod index 386497e..b6a2306 100644 --- a/go.mod +++ b/go.mod @@ -5,13 +5,14 @@ go 1.21.0 toolchain go1.23.1 require ( - git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240823041145-d4df71cf37e5 + git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240924065029-c865046cd9e7 git.zhangdeman.cn/zhangdeman/exception v0.0.0-20231105153815-e8561a060cc8 git.zhangdeman.cn/zhangdeman/logger v0.0.0-20240725055115-98eb52ae307a git.zhangdeman.cn/zhangdeman/network v0.0.0-20230925112156-f0eb86dd2442 - git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240823103024-c38d16dc28d3 + git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240924063449-ef80c6cb79d1 github.com/gin-gonic/gin v1.10.0 github.com/go-playground/validator/v10 v10.22.1 + github.com/mcuadros/go-defaults v1.2.0 github.com/pkg/errors v0.9.1 github.com/stretchr/testify v1.9.0 go.uber.org/zap v1.27.0 @@ -26,7 +27,7 @@ require ( git.zhangdeman.cn/zhangdeman/websocket v0.0.0-20240723075210-85feada512b2 // indirect github.com/BurntSushi/toml v1.4.0 // indirect github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 // indirect - github.com/bytedance/sonic v1.12.2 // indirect + github.com/bytedance/sonic v1.12.3 // indirect github.com/bytedance/sonic/loader v0.2.0 // indirect github.com/cloudwego/base64x v0.1.4 // indirect github.com/cloudwego/iasm v0.2.0 // indirect @@ -45,7 +46,6 @@ require ( github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible // indirect github.com/lestrrat-go/strftime v1.1.0 // indirect github.com/mattn/go-isatty v0.0.20 // indirect - github.com/mcuadros/go-defaults v1.2.0 // indirect github.com/mitchellh/go-homedir v1.1.0 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect diff --git a/go.sum b/go.sum index ace09fd..b786f17 100644 --- a/go.sum +++ b/go.sum @@ -8,6 +8,8 @@ git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240817091513-491f455a23c0 h1:U12XDt git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240817091513-491f455a23c0/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k= git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240823041145-d4df71cf37e5 h1:pmIHln0gWW+5xAB762h3WDsRkZuYLUDndvJDsGMKoOY= git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240823041145-d4df71cf37e5/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k= +git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240924065029-c865046cd9e7 h1:tyCPCMK+68PZ0axZylQHitMVp1d5mzNr9/YqMHXqo+A= +git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240924065029-c865046cd9e7/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k= git.zhangdeman.cn/zhangdeman/easylock v0.0.0-20230731062340-983985c12eda h1:bMD6r9gjRy7cO+T4zRQVYAesgIblBdTnhzT1vN5wjvI= git.zhangdeman.cn/zhangdeman/easylock v0.0.0-20230731062340-983985c12eda/go.mod h1:dT0rmHcJ9Z9IqWeMIt7YzR88nKkNV2V3dfG0j9Q6lK0= git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20240311030808-e2a2e6a3c211 h1:I/wOsRpCSRkU9vo1u703slQsmK0wnNeZzsWQOGtIAG0= @@ -32,6 +34,8 @@ git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240813083016-da44ae07ab9b h1:CcO2t git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240813083016-da44ae07ab9b/go.mod h1:gnaF3v9/om6gaxFKeNVuKeFTYM61gHyW7vign7vrwyo= git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240823103024-c38d16dc28d3 h1:RcWNxrHmhZksZWrP/HLEwAM8uIIHYlPLQ20HnLzC+j0= git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240823103024-c38d16dc28d3/go.mod h1:KcojKP22mv9/IZrQWlIBfa1EuBxtEOqfWMgN3SYK2N8= +git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240924063449-ef80c6cb79d1 h1:LYw8NJeWcOiyrGjH9weyxnaMit94MlIngL+uskbLjtw= +git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240924063449-ef80c6cb79d1/go.mod h1:+2qNxuRsfyfOvXk9HNwn+CmyPmmhhrQm/eIi1FDU1jw= github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0= github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho= github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 h1:OYA+5W64v3OgClL+IrOD63t4i/RW7RqrAVl9LTZ9UqQ= @@ -42,6 +46,8 @@ github.com/bytedance/sonic v1.12.1 h1:jWl5Qz1fy7X1ioY74WqO0KjAMtAGQs4sYnjiEBiyX2 github.com/bytedance/sonic v1.12.1/go.mod h1:B8Gt/XvtZ3Fqj+iSKMypzymZxw/FVwgIGKzMzT9r/rk= github.com/bytedance/sonic v1.12.2 h1:oaMFuRTpMHYLpCntGca65YWt5ny+wAceDERTkT2L9lg= github.com/bytedance/sonic v1.12.2/go.mod h1:B8Gt/XvtZ3Fqj+iSKMypzymZxw/FVwgIGKzMzT9r/rk= +github.com/bytedance/sonic v1.12.3 h1:W2MGa7RCU1QTeYRTPE3+88mVC0yXmsRQRChiyVocVjU= +github.com/bytedance/sonic v1.12.3/go.mod h1:B8Gt/XvtZ3Fqj+iSKMypzymZxw/FVwgIGKzMzT9r/rk= github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM= github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU= github.com/bytedance/sonic/loader v0.2.0 h1:zNprn+lsIP06C/IqCHs3gPQIvnvpKbbxyXQP1iU4kWM= diff --git a/response/response.go b/response/response.go index 8e0e14d..739d946 100644 --- a/response/response.go +++ b/response/response.go @@ -43,7 +43,17 @@ func SetBusinessSuccessCode(code any) { // Date : 22:37 2022/6/25 func Success(ctx *gin.Context, data any) { successException := exception.NewSuccess(data) - Send(ctx, successException.GetCode(), successException.GetHttpCode(), successException.GetData()) + Send(ctx, successException.GetCode(), successException.GetHttpCode(), successException.GetData(), nil) +} + +// SuccessWithExtension 返回扩展数据 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 14:52 2024/9/24 +func SuccessWithExtension(ctx *gin.Context, data any, extension map[string]any) { + successException := exception.NewSuccess(data) + Send(ctx, successException.GetCode(), successException.GetHttpCode(), successException.GetData(), extension) } // Send 基础的发送数据 @@ -51,7 +61,7 @@ func Success(ctx *gin.Context, data any) { // Author : go_developer@163.com<白茶清欢> // // Date : 22:40 2022/6/25 -func Send(ctx *gin.Context, code any, httpCode int, data any) { +func Send(ctx *gin.Context, code any, httpCode int, data any, extension map[string]any) { // 设置请求是否成功的标识 ctx.Set(define.GetHttpHandleConfig().RequestIsSuccessField, fmt.Sprintf("%v", code) == fmt.Sprintf("%v", successBusinessCode)) if ctx.GetBool(hasSendResponseFlag) { @@ -69,6 +79,9 @@ func Send(ctx *gin.Context, code any, httpCode int, data any) { responseConfig.ResponseDataField: data, responseConfig.HandleRequestCostField: finishRequestTime - ctx.GetInt64(responseConfig.StartRequestTimeField), } + if responseConfig.EnableExtensionOutput && nil != extension { + responseData[responseConfig.ExtensionOutputField] = extension + } // 记录完成时间 ctx.Set(responseConfig.FinishRequestTimeField, finishRequestTime) ctx.Set(responseConfig.ResponseDataField, responseData) @@ -82,7 +95,7 @@ func Send(ctx *gin.Context, code any, httpCode int, data any) { // // Date : 22:41 2022/6/25 func SendWithStatusOK(ctx *gin.Context, code any, data any) { - Send(ctx, code, http.StatusOK, data) + Send(ctx, code, http.StatusOK, data, nil) } // SendWithException 使用exception发送数据 @@ -98,7 +111,7 @@ func SendWithException(ctx *gin.Context, e exception.IException, data any) { "e_data": e.GetData(), "u_e_data": data, } - Send(ctx, e.GetCode(), e.GetHttpCode(), outputData) + Send(ctx, e.GetCode(), e.GetHttpCode(), outputData, nil) } // JSON ctx.JSON 的平替, 增加了数据是否已相应的标识