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"
|
2024-09-30 15:25:17 +08:00
|
|
|
"runtime"
|
2024-09-30 15:54:20 +08:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2022-06-25 23:03:46 +08:00
|
|
|
"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
|
|
|
|
}
|
2024-08-17 20:21:34 +08:00
|
|
|
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,
|
2024-08-17 20:21:34 +08:00
|
|
|
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
|
|
|
|
}
|
2022-07-14 11:51:44 +08:00
|
|
|
// 记录完成时间
|
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-08-17 17:31:11 +08:00
|
|
|
func SendWithException(ctx *gin.Context, e exception.IException, data any) {
|
2022-06-26 13:28:31 +08:00
|
|
|
if nil == e {
|
|
|
|
e = exception.NewSuccess(data)
|
|
|
|
}
|
2024-09-30 11:47:04 +08:00
|
|
|
if !define.GetHttpHandleConfig().DisableDebugStackOutput {
|
2024-09-30 15:54:20 +08:00
|
|
|
pcs := make([]uintptr, 128)
|
|
|
|
n := runtime.Callers(2, pcs)
|
|
|
|
|
|
|
|
frames := runtime.CallersFrames(pcs[:n])
|
|
|
|
|
|
|
|
var sb strings.Builder
|
|
|
|
for {
|
|
|
|
frame, more := frames.Next()
|
|
|
|
|
|
|
|
sb.WriteString(frame.Function)
|
|
|
|
sb.WriteByte('\n')
|
|
|
|
sb.WriteByte('\t')
|
|
|
|
sb.WriteString(frame.File)
|
|
|
|
sb.WriteByte(':')
|
|
|
|
sb.WriteString(strconv.Itoa(frame.Line))
|
|
|
|
sb.WriteByte('\n')
|
|
|
|
|
|
|
|
if !more {
|
|
|
|
break
|
|
|
|
}
|
2024-09-30 15:25:17 +08:00
|
|
|
}
|
2024-09-30 15:54:20 +08:00
|
|
|
fmt.Println(sb.String())
|
2024-09-30 11:47:04 +08:00
|
|
|
}
|
2024-08-17 17:31:11 +08:00
|
|
|
outputData := map[string]any{
|
2024-03-15 10:50:01 +08:00
|
|
|
"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)
|
|
|
|
}
|