优化response + wrapper

This commit is contained in:
白茶清欢 2023-12-29 15:35:46 +08:00
parent b7abf88670
commit 4dc70ee96b
2 changed files with 9 additions and 4 deletions

View File

@ -73,3 +73,11 @@ func (wh *wrapperHandle) GetContentType(ctx *gin.Context, defaultVal string) str
contentType := strings.ToLower(ctx.ContentType()) contentType := strings.ToLower(ctx.ContentType())
return wrapper.TernaryOperator.String(len(contentType) > 0, wrapper.String(contentType), wrapper.String(defaultVal)).Value() 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()
}

View File

@ -8,7 +8,6 @@
package response package response
import ( import (
"encoding/json"
"net/http" "net/http"
"time" "time"
@ -53,11 +52,9 @@ func Send(ctx *gin.Context, code interface{}, httpCode int, data interface{}) {
define.ResponseDataField: data, define.ResponseDataField: data,
define.HandleRequestCostField: finishRequestTime.UnixMilli() - ctx.GetTime(define.StartRequestTimeField).UnixMilli(), define.HandleRequestCostField: finishRequestTime.UnixMilli() - ctx.GetTime(define.StartRequestTimeField).UnixMilli(),
} }
// 记录响应数据
recordData, _ := json.Marshal(responseData)
// 记录完成时间 // 记录完成时间
ctx.Set(define.FinishRequestTimeField, finishRequestTime) ctx.Set(define.FinishRequestTimeField, finishRequestTime)
ctx.Set(define.RecordResponseDataField, string(recordData)) ctx.Set(define.RecordResponseDataField, responseData)
responseException := exception.New(code, httpCode, responseData) responseException := exception.New(code, httpCode, responseData)
ctx.JSON(responseException.GetHttpCode(), responseException.GetData()) ctx.JSON(responseException.GetHttpCode(), responseException.GetData())
} }