From 4dc70ee96bcb954900eeaea8e595c406be37c815 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Fri, 29 Dec 2023 15:35:46 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96response=20+=20wrapper?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- request/wrapper.go | 8 ++++++++ response/response.go | 5 +---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/request/wrapper.go b/request/wrapper.go index 6a05726..b2e4f0d 100644 --- a/request/wrapper.go +++ b/request/wrapper.go @@ -73,3 +73,11 @@ func (wh *wrapperHandle) GetContentType(ctx *gin.Context, defaultVal string) str contentType := strings.ToLower(ctx.ContentType()) return wrapper.TernaryOperator.String(len(contentType) > 0, wrapper.String(contentType), wrapper.String(defaultVal)).Value() } + +func (wh *wrapperHandle) GetResponseBody(ctx *gin.Context, defaultVal string) string { + if nil == ctx { + return defaultVal + } + contentType := strings.ToLower(ctx.ContentType()) + return wrapper.TernaryOperator.String(len(contentType) > 0, wrapper.String(contentType), wrapper.String(defaultVal)).Value() +} diff --git a/response/response.go b/response/response.go index 39916b9..a6d148c 100644 --- a/response/response.go +++ b/response/response.go @@ -8,7 +8,6 @@ package response import ( - "encoding/json" "net/http" "time" @@ -53,11 +52,9 @@ func Send(ctx *gin.Context, code interface{}, httpCode int, data interface{}) { define.ResponseDataField: data, define.HandleRequestCostField: finishRequestTime.UnixMilli() - ctx.GetTime(define.StartRequestTimeField).UnixMilli(), } - // 记录响应数据 - recordData, _ := json.Marshal(responseData) // 记录完成时间 ctx.Set(define.FinishRequestTimeField, finishRequestTime) - ctx.Set(define.RecordResponseDataField, string(recordData)) + ctx.Set(define.RecordResponseDataField, responseData) responseException := exception.New(code, httpCode, responseData) ctx.JSON(responseException.GetHttpCode(), responseException.GetData()) }