优化响应的方式

This commit is contained in:
Yun
2025-12-07 01:51:01 +08:00
parent c50605587a
commit cd0df49d4b
3 changed files with 74 additions and 38 deletions
+15 -7
View File
@@ -8,7 +8,7 @@ func GinError(ctx *gin.Context, err error) {
Error(ctx, ctx.Writer, err)
}
func GinErrorData(ctx *gin.Context, err error, data interface{}) {
func GinErrorData(ctx *gin.Context, err error, data any) {
ErrorWithData(ctx, ctx.Writer, err, data)
}
@@ -16,14 +16,22 @@ func GinErrorStr(ctx *gin.Context, msg string) {
ErrorStr(ctx, ctx.Writer, msg)
}
func GinSuccess(ctx *gin.Context, data interface{}, info ...interface{}) {
Success(ctx, ctx.Writer, data, info)
func GinSuccess(ctx *gin.Context, data any) {
Success(ctx, ctx.Writer, data)
}
func GinSuccessWithPage(ctx *gin.Context, data interface{}, page pagination) {
SuccessWithPage(ctx, ctx.Writer, data, page)
func GinSuccessWithPage(ctx *gin.Context, data any, page pagination) {
SuccessWithPage(ctx, ctx.Writer, data, &page)
}
func GinFormatMessage(ctx *gin.Context, message string, format map[string]string, data interface{}) {
FormatMessage(ctx, ctx.Writer, message, format, data)
func GinOptions(ctx *gin.Context, ops ...Option) {
ResponseCtx(ctx, ctx.Writer, ops...)
}
func GinMsgMapData(ctx *gin.Context, msgKey string, format map[string]string, data any) {
FormatMessage(ctx, ctx.Writer, msgKey, format, data)
}
func GinMsgKV(ctx *gin.Context, msgKey string, k string, v string) {
FormatMessage(ctx, ctx.Writer, msgKey, map[string]string{k: v}, nil)
}
+33
View File
@@ -11,6 +11,11 @@ type options struct {
traceId string
defaultSuccessCode int
defaultErrorCode int
code int
data any
message string
pagination *pagination
}
var op *options = nil
@@ -30,11 +35,39 @@ func defaultOptions() *options {
logger: &defaultLogger{},
defaultSuccessCode: 200,
defaultErrorCode: 400,
code: 200,
data: nil, // 默认为nil
message: "请求成功",
}
}
type Option func(*options)
func SetCode(code int) Option {
return func(o *options) {
o.code = code
}
}
func SetData(data any) Option {
return func(o *options) {
o.data = data
}
}
func SetMessage(message string) Option {
return func(o *options) {
o.message = message
}
}
func SetPage(page *pagination) Option {
return func(o *options) {
o.pagination = page
}
}
// 设置默认的成功code
func SetDefaultSuccessCode(code int) Option {
return func(o *options) {
+26 -31
View File
@@ -50,18 +50,18 @@ func GetPage(page, size, totalCount int64) pagination {
}
// Msg格式化响应
func FormatMessage(ctx context.Context, w http.ResponseWriter, message string, format map[string]string, data interface{}) {
code, msg := langx.GetTransFormatCtx(ctx, message, format)
ResponseCtx(ctx, w, code, msg, data)
func FormatMessage(ctx context.Context, w http.ResponseWriter, msgKey string, format map[string]string, data interface{}) {
code, msg := langx.GetTransFormatCtx(ctx, msgKey, format)
ResponseCtx(ctx, w, SetCode(code), SetMessage(msg), SetData(data))
}
// 成功的响应
func Success(ctx context.Context, w http.ResponseWriter, data interface{}, info ...interface{}) {
ResponseCtx(ctx, w, op.defaultSuccessCode, "请求成功", data, info)
func Success(ctx context.Context, w http.ResponseWriter, data any) {
ResponseCtx(ctx, w, SetCode(op.defaultSuccessCode), SetMessage("请求成功"), SetData(data))
}
func SuccessWithPage(ctx context.Context, w http.ResponseWriter, data interface{}, page pagination) {
ResponseCtx(ctx, w, op.defaultSuccessCode, "请求成功", data, page)
func SuccessWithPage(ctx context.Context, w http.ResponseWriter, data any, page *pagination) {
ResponseCtx(ctx, w, SetCode(op.defaultSuccessCode), SetMessage("请求成功"), SetData(data), SetPage(page))
}
// 失败的响应
@@ -81,7 +81,7 @@ func ErrorWithData(ctx context.Context, w http.ResponseWriter, err error, data i
}
msg = err.Error()
}
ResponseCtx(ctx, w, code, msg, data)
ResponseCtx(ctx, w, SetCode(code), SetMessage(msg), SetData(data))
}
func ErrorStr(ctx context.Context, w http.ResponseWriter, msg string) {
@@ -90,36 +90,31 @@ func ErrorStr(ctx context.Context, w http.ResponseWriter, msg string) {
if code == langx.GetDefaultCode() {
code = op.defaultErrorCode
}
ResponseCtx(ctx, w, code, msg, "")
ResponseCtx(ctx, w, SetCode(code), SetMessage(msg))
}
// 基础的响应
func ResponseCtx(ctx context.Context, w http.ResponseWriter, code int, message string, data interface{}, other ...interface{}) {
func ResponseCtx(ctx context.Context, w http.ResponseWriter, ops ...Option) {
res := make(map[string]interface{})
res["code"] = code
res["message"] = message
// 分页的内容
var page *pagination
for _, value := range other {
vv, ok := value.(pagination)
if ok {
page = &vv
}
def := defaultOptions() // 默认值
for _, o := range ops {
o(def)
}
if page == nil {
// 不需要分页
res["data"] = data
} else {
res["data"] = data
res["pagination"] = page
res := make(map[string]interface{})
res["code"] = def.code
res["message"] = def.message
if def.data != nil {
res["data"] = def.data
}
if def.pagination != nil {
res["pagination"] = def.pagination
}
// 获取trace_id
if op.traceId != "" {
t := ctx.Value(op.traceId)
if def.traceId != "" {
t := ctx.Value(def.traceId)
if t != nil {
res["trace_id"] = t
} else {
@@ -128,9 +123,9 @@ func ResponseCtx(ctx context.Context, w http.ResponseWriter, code int, message s
}
}
if !op.ignoreLog {
if !def.ignoreLog {
// 记录日志
op.logger.Info(ctx, res)
def.logger.Info(ctx, res)
}
bs, _ := json.Marshal(res)