// 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" ) // 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{}) { responseData := map[string]interface{}{ define.ResponseCodeField: code, define.ResponseMessageField: exception.GetMessage(code), define.ResponseTraceIDField: ctx.GetString(define.TraceIDField), define.ResponseRequestIDField: ctx.GetString(define.RequestIDField), define.ResponseDataField: data, define.HandleRequestCostField: float64(time.Now().UnixNano()-ctx.GetInt64(define.StartRequestTimeField)) / 1e9, } 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 map[string]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) } if nil != e.GetData() { Send(ctx, e.GetCode(), e.GetHttpCode(), e.GetData()) } else { Send(ctx, e.GetCode(), e.GetHttpCode(), data) } }