101 lines
2.9 KiB
Go
101 lines
2.9 KiB
Go
// 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"
|
|
)
|
|
|
|
const (
|
|
// hasSendResponseFlag 已经发送响应数据的标识, 一旦识别到, 重复调用不会执行
|
|
hasSendResponseFlag = "GIN_PKG_HAS_SEND_RESPONSE"
|
|
)
|
|
|
|
// Success 成功的响应
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 22:37 2022/6/25
|
|
func Success(ctx *gin.Context, data interface{}) {
|
|
successException := exception.NewSuccess(data)
|
|
Send(ctx, successException.GetCode(), successException.GetHttpCode(), successException.GetData())
|
|
}
|
|
|
|
// Send 基础的发送数据
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 22:40 2022/6/25
|
|
func Send(ctx *gin.Context, code interface{}, httpCode int, data interface{}) {
|
|
if ctx.GetBool(hasSendResponseFlag) {
|
|
// 已经发送过数据, 后面在发送数据, 不执行
|
|
return
|
|
}
|
|
// 设置数据已发送的标识
|
|
defer ctx.Set(hasSendResponseFlag, true)
|
|
responseConfig := define.GetHttpHandleConfig()
|
|
finishRequestTime := time.Now()
|
|
responseData := map[string]interface{}{
|
|
responseConfig.ResponseCodeField: code,
|
|
responseConfig.ResponseMessageField: exception.GetMessage(code),
|
|
responseConfig.ResponseTraceIDField: ctx.GetString(responseConfig.ResponseTraceIDField),
|
|
responseConfig.ResponseDataField: data,
|
|
responseConfig.HandleRequestCostField: finishRequestTime.UnixMilli() - ctx.GetTime(responseConfig.StartRequestTimeField).UnixMilli(),
|
|
}
|
|
// 记录完成时间
|
|
ctx.Set(responseConfig.FinishRequestTimeField, finishRequestTime)
|
|
ctx.Set(responseConfig.ResponseDataField, responseData)
|
|
responseException := exception.New(code, httpCode, responseData)
|
|
ctx.JSON(responseException.GetHttpCode(), responseException.GetData())
|
|
}
|
|
|
|
// SendWithStatusOK ...
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 22:41 2022/6/25
|
|
func SendWithStatusOK(ctx *gin.Context, code interface{}, data interface{}) {
|
|
Send(ctx, code, http.StatusOK, data)
|
|
}
|
|
|
|
// SendWithException 使用exception发送数据
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 13:08 2022/6/26
|
|
func SendWithException(ctx *gin.Context, e exception.IException, data interface{}) {
|
|
if nil == e {
|
|
e = exception.NewSuccess(data)
|
|
}
|
|
outputData := map[string]interface{}{
|
|
"e_data": e.GetData(),
|
|
"u_e_data": data,
|
|
}
|
|
Send(ctx, e.GetCode(), e.GetHttpCode(), outputData)
|
|
}
|
|
|
|
// 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)
|
|
}
|