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 (
|
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
2022-06-25 23:03:46 +08:00
|
|
|
// Success 成功的响应
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 22:37 2022/6/25
|
2022-06-26 13:33:03 +08:00
|
|
|
func Success(ctx *gin.Context, data interface{}) {
|
2022-06-25 23:03:46 +08:00
|
|
|
successException := exception.NewSuccess(data)
|
|
|
|
Send(ctx, successException.GetCode(), successException.GetHttpCode(), successException.GetData())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send 基础的发送数据
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 22:40 2022/6/25
|
2022-06-26 13:03:41 +08:00
|
|
|
func Send(ctx *gin.Context, code interface{}, httpCode int, data interface{}) {
|
2023-02-15 14:52:49 +08:00
|
|
|
if ctx.GetBool(hasSendResponseFlag) {
|
|
|
|
// 已经发送过数据, 后面在发送数据, 不执行
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// 设置数据已发送的标识
|
|
|
|
defer ctx.Set(hasSendResponseFlag, true)
|
2023-08-10 12:13:49 +08:00
|
|
|
finishRequestTime := time.Now()
|
2022-06-25 23:03:46 +08:00
|
|
|
responseData := map[string]interface{}{
|
2022-06-26 13:03:41 +08:00
|
|
|
define.ResponseCodeField: code,
|
|
|
|
define.ResponseMessageField: exception.GetMessage(code),
|
2022-06-25 23:03:46 +08:00
|
|
|
define.ResponseTraceIDField: ctx.GetString(define.TraceIDField),
|
|
|
|
define.ResponseRequestIDField: ctx.GetString(define.RequestIDField),
|
|
|
|
define.ResponseDataField: data,
|
2023-08-10 12:13:49 +08:00
|
|
|
define.HandleRequestCostField: finishRequestTime.UnixMilli() - ctx.GetTime(define.StartRequestTimeField).UnixMilli(),
|
2022-06-25 23:03:46 +08:00
|
|
|
}
|
2022-07-14 11:51:44 +08:00
|
|
|
// 记录完成时间
|
|
|
|
ctx.Set(define.FinishRequestTimeField, finishRequestTime)
|
2023-12-29 15:35:46 +08:00
|
|
|
ctx.Set(define.RecordResponseDataField, 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
|
2023-09-10 15:29:35 +08:00
|
|
|
func SendWithStatusOK(ctx *gin.Context, code interface{}, data interface{}) {
|
2022-06-25 23:03:46 +08:00
|
|
|
Send(ctx, code, http.StatusOK, data)
|
|
|
|
}
|
2022-06-26 13:28:31 +08:00
|
|
|
|
|
|
|
// SendWithException 使用exception发送数据
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 13:08 2022/6/26
|
2022-07-03 13:12:10 +08:00
|
|
|
func SendWithException(ctx *gin.Context, e exception.IException, data interface{}) {
|
2022-06-26 13:28:31 +08:00
|
|
|
if nil == e {
|
|
|
|
e = exception.NewSuccess(data)
|
|
|
|
}
|
2024-03-15 10:50:01 +08:00
|
|
|
outputData := map[string]interface{}{
|
|
|
|
"e_data": e.GetData(),
|
|
|
|
"u_e_data": data,
|
2022-06-26 13:28:31 +08:00
|
|
|
}
|
2024-03-15 10:50:01 +08:00
|
|
|
Send(ctx, e.GetCode(), e.GetHttpCode(), outputData)
|
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
|
|
|
|
func JSON(ctx *gin.Context, httpCode int, data interface{}) {
|
|
|
|
if ctx.GetBool(hasSendResponseFlag) {
|
|
|
|
// 已经发送过数据, 后面在发送数据, 不执行
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// 设置数据已发送的标识
|
|
|
|
defer ctx.Set(hasSendResponseFlag, true)
|
|
|
|
ctx.JSON(httpCode, data)
|
|
|
|
}
|