gin/response/response.go

137 lines
4.0 KiB
Go
Raw Normal View History

2022-06-25 23:03:46 +08:00
// Package response ...
//
// Description : response ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2022-06-25 20:52
package response
import (
2024-08-17 17:31:11 +08:00
"fmt"
2022-06-25 23:03:46 +08:00
"net/http"
"time"
"git.zhangdeman.cn/zhangdeman/exception"
"git.zhangdeman.cn/zhangdeman/gin/define"
"github.com/gin-gonic/gin"
)
2023-02-15 14:52:49 +08:00
const (
// hasSendResponseFlag 已经发送响应数据的标识, 一旦识别到, 重复调用不会执行
hasSendResponseFlag = "GIN_PKG_HAS_SEND_RESPONSE"
)
2024-08-17 17:31:11 +08:00
var (
// 成功的业务状态码
successBusinessCode any = "200"
)
// SetBusinessSuccessCode 设置成状态码
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:01 2024/8/17
func SetBusinessSuccessCode(code any) {
successBusinessCode = code
}
2022-06-25 23:03:46 +08:00
// Success 成功的响应
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 22:37 2022/6/25
2024-08-17 17:31:11 +08:00
func Success(ctx *gin.Context, data any) {
2022-06-25 23:03:46 +08:00
successException := exception.NewSuccess(data)
2024-09-24 14:56:03 +08:00
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)
2022-06-25 23:03:46 +08:00
}
// Send 基础的发送数据
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 22:40 2022/6/25
2024-09-24 14:56:03 +08:00
func Send(ctx *gin.Context, code any, httpCode int, data any, extension map[string]any) {
2024-08-17 17:31:11 +08:00
// 设置请求是否成功的标识
ctx.Set(define.GetHttpHandleConfig().RequestIsSuccessField, fmt.Sprintf("%v", code) == fmt.Sprintf("%v", successBusinessCode))
2023-02-15 14:52:49 +08:00
if ctx.GetBool(hasSendResponseFlag) {
// 已经发送过数据, 后面在发送数据, 不执行
return
}
finishRequestTime := time.Now().UnixMilli()
2023-02-15 14:52:49 +08:00
// 设置数据已发送的标识
defer ctx.Set(hasSendResponseFlag, true)
2024-07-23 17:09:29 +08:00
responseConfig := define.GetHttpHandleConfig()
2024-08-17 17:31:11 +08:00
responseData := map[string]any{
2024-07-23 17:09:29 +08:00
responseConfig.ResponseCodeField: code,
responseConfig.ResponseMessageField: exception.GetMessage(code),
responseConfig.ResponseTraceIDField: ctx.GetString(responseConfig.ResponseTraceIDField),
responseConfig.ResponseDataField: data,
responseConfig.HandleRequestCostField: finishRequestTime - ctx.GetInt64(responseConfig.StartRequestTimeField),
2022-06-25 23:03:46 +08:00
}
2024-09-24 14:56:03 +08:00
if responseConfig.EnableExtensionOutput && nil != extension {
responseData[responseConfig.ExtensionOutputField] = extension
}
// 记录完成时间
2024-07-23 17:09:29 +08:00
ctx.Set(responseConfig.FinishRequestTimeField, finishRequestTime)
ctx.Set(responseConfig.ResponseDataField, responseData)
2022-06-26 13:03:41 +08:00
responseException := exception.New(code, httpCode, responseData)
2022-06-25 23:03:46 +08:00
ctx.JSON(responseException.GetHttpCode(), responseException.GetData())
}
// SendWithStatusOK ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 22:41 2022/6/25
2024-08-17 17:31:11 +08:00
func SendWithStatusOK(ctx *gin.Context, code any, data any) {
2024-09-24 14:56:03 +08:00
Send(ctx, code, http.StatusOK, data, nil)
2022-06-25 23:03:46 +08:00
}
2022-06-26 13:28:31 +08:00
// SendWithException 使用exception发送数据
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 13:08 2022/6/26
2024-09-30 16:17:14 +08:00
func SendWithException(ctx *gin.Context, e exception.IException, data map[string]any) {
2022-06-26 13:28:31 +08:00
if nil == e {
e = exception.NewSuccess(data)
}
2024-09-30 16:17:14 +08:00
if !define.GetHttpHandleConfig().DisableDebugStackOutput && nil != e {
stack := e.GetStack()
if len(stack) > 0 {
fmt.Println(stack)
2024-09-30 15:25:17 +08:00
}
}
2024-08-17 17:31:11 +08:00
outputData := map[string]any{
"e_data": e.GetData(),
"u_e_data": data,
2022-06-26 13:28:31 +08:00
}
2024-09-24 14:56:03 +08:00
Send(ctx, e.GetCode(), e.GetHttpCode(), outputData, nil)
2022-06-26 13:28:31 +08:00
}
2023-02-15 14:52:49 +08:00
// JSON ctx.JSON 的平替, 增加了数据是否已相应的标识
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:51 2023/2/15
2024-08-17 17:31:11 +08:00
func JSON(ctx *gin.Context, httpCode int, data any) {
2023-02-15 14:52:49 +08:00
if ctx.GetBool(hasSendResponseFlag) {
// 已经发送过数据, 后面在发送数据, 不执行
return
}
// 设置数据已发送的标识
defer ctx.Set(hasSendResponseFlag, true)
ctx.JSON(httpCode, data)
}