响应数据支持根据不同的content_type使用不同的发送方式
This commit is contained in:
@ -8,8 +8,11 @@
|
||||
package response
|
||||
|
||||
import (
|
||||
"git.zhangdeman.cn/zhangdeman/dynamic-struct/wrapper"
|
||||
"fmt"
|
||||
"git.zhangdeman.cn/zhangdeman/serialize"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.zhangdeman.cn/zhangdeman/exception"
|
||||
@ -51,9 +54,9 @@ func Success(ctx *gin.Context, data any) {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 14:52 2024/9/24
|
||||
func SuccessWithExtension(ctx *gin.Context, data any, extension map[string]any) {
|
||||
func SuccessWithExtension(ctx *gin.Context, data any, responseOption *define.ResponseOption) {
|
||||
successException := exception.NewSuccess(data)
|
||||
Send(ctx, successException.GetCode(), successException.GetHttpCode(), successException.GetData(), extension)
|
||||
Send(ctx, successException.GetCode(), successException.GetHttpCode(), successException.GetData(), responseOption)
|
||||
}
|
||||
|
||||
// Send 基础的发送数据
|
||||
@ -61,32 +64,64 @@ func SuccessWithExtension(ctx *gin.Context, data any, extension map[string]any)
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 22:40 2022/6/25
|
||||
func Send(ctx *gin.Context, code any, httpCode int, data any, extension map[string]any) {
|
||||
func Send(ctx *gin.Context, code any, httpCode int, data any, responseOption *define.ResponseOption) {
|
||||
if nil == responseOption {
|
||||
responseOption = &define.ResponseOption{
|
||||
ContentType: "application/json",
|
||||
Extension: make(map[string]any),
|
||||
}
|
||||
}
|
||||
if len(responseOption.ContentType) == 0 {
|
||||
responseOption.ContentType = "application/json"
|
||||
}
|
||||
if len(responseOption.XmlName) == 0 {
|
||||
responseOption.XmlName = "ResponseData"
|
||||
}
|
||||
// 设置请求是否成功的标识
|
||||
ctx.Set(define.GetHttpHandleConfig().RequestIsSuccessField, fmt.Sprintf("%v", code) == fmt.Sprintf("%v", successBusinessCode))
|
||||
if ctx.GetBool(hasSendResponseFlag) {
|
||||
// 已经发送过数据, 后面在发送数据, 不执行
|
||||
return
|
||||
}
|
||||
finishRequestTime := time.Now().UnixMilli()
|
||||
// 设置数据已发送的标识
|
||||
defer ctx.Set(hasSendResponseFlag, true)
|
||||
finishRequestTime := time.Now()
|
||||
responseData := BuildResponseData(ctx, finishRequestTime, code, data, responseOption.Extension)
|
||||
// 记录完成时间
|
||||
responseConfig := define.GetHttpHandleConfig()
|
||||
ctx.Set(responseConfig.FinishRequestTimeField, finishRequestTime.UnixMilli())
|
||||
ctx.Set(responseConfig.ResponseDataField, responseData)
|
||||
responseException := exception.New(code, httpCode, responseData)
|
||||
responseContentType := getResponseDataType(responseOption.ContentType)
|
||||
responseInstance, _ := wrapper.NewJson(serialize.JSON.MarshalForStringIgnoreError(responseException.GetData()), &wrapper.Option{XmlName: responseOption.XmlName})
|
||||
finalResponseData, _ := responseInstance.Marshal(responseContentType)
|
||||
ctx.Data(responseException.GetHttpCode(), responseContentType, finalResponseData)
|
||||
}
|
||||
|
||||
// getResponseDataType 获取相应数据类型
|
||||
func getResponseDataType(contentType string) string {
|
||||
if contentType == "" {
|
||||
return "json"
|
||||
}
|
||||
applicationInfo := strings.Split(contentType, ";")[0]
|
||||
applicationInfoArr := strings.Split(applicationInfo, "/")
|
||||
return applicationInfoArr[len(applicationInfoArr)-1]
|
||||
}
|
||||
|
||||
// BuildResponseData 构建响应数据
|
||||
func BuildResponseData(ctx *gin.Context, finishTime time.Time, code any, data any, extension map[string]any) map[string]any {
|
||||
responseConfig := define.GetHttpHandleConfig()
|
||||
responseData := map[string]any{
|
||||
responseConfig.ResponseCodeField: code,
|
||||
responseConfig.ResponseMessageField: exception.GetMessage(code),
|
||||
responseConfig.ResponseTraceIDField: ctx.GetString(responseConfig.ResponseTraceIDField),
|
||||
responseConfig.ResponseDataField: data,
|
||||
responseConfig.HandleRequestCostField: finishRequestTime - ctx.GetInt64(responseConfig.StartRequestTimeField),
|
||||
responseConfig.HandleRequestCostField: finishTime.UnixMilli() - ctx.GetInt64(responseConfig.StartRequestTimeField),
|
||||
}
|
||||
if responseConfig.EnableExtensionOutput && nil != extension {
|
||||
responseData[responseConfig.ExtensionOutputField] = extension
|
||||
}
|
||||
// 记录完成时间
|
||||
ctx.Set(responseConfig.FinishRequestTimeField, finishRequestTime)
|
||||
ctx.Set(responseConfig.ResponseDataField, responseData)
|
||||
responseException := exception.New(code, httpCode, responseData)
|
||||
ctx.JSON(responseException.GetHttpCode(), responseException.GetData())
|
||||
return responseData
|
||||
}
|
||||
|
||||
// SendWithStatusOK ...
|
||||
|
Reference in New Issue
Block a user