优化响应的方式

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) 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) ErrorWithData(ctx, ctx.Writer, err, data)
} }
@@ -16,14 +16,22 @@ func GinErrorStr(ctx *gin.Context, msg string) {
ErrorStr(ctx, ctx.Writer, msg) ErrorStr(ctx, ctx.Writer, msg)
} }
func GinSuccess(ctx *gin.Context, data interface{}, info ...interface{}) { func GinSuccess(ctx *gin.Context, data any) {
Success(ctx, ctx.Writer, data, info) Success(ctx, ctx.Writer, data)
} }
func GinSuccessWithPage(ctx *gin.Context, data interface{}, page pagination) { func GinSuccessWithPage(ctx *gin.Context, data any, page pagination) {
SuccessWithPage(ctx, ctx.Writer, data, page) SuccessWithPage(ctx, ctx.Writer, data, &page)
} }
func GinFormatMessage(ctx *gin.Context, message string, format map[string]string, data interface{}) { func GinOptions(ctx *gin.Context, ops ...Option) {
FormatMessage(ctx, ctx.Writer, message, format, data) 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 traceId string
defaultSuccessCode int defaultSuccessCode int
defaultErrorCode int defaultErrorCode int
code int
data any
message string
pagination *pagination
} }
var op *options = nil var op *options = nil
@@ -30,11 +35,39 @@ func defaultOptions() *options {
logger: &defaultLogger{}, logger: &defaultLogger{},
defaultSuccessCode: 200, defaultSuccessCode: 200,
defaultErrorCode: 400, defaultErrorCode: 400,
code: 200,
data: nil, // 默认为nil
message: "请求成功",
} }
} }
type Option func(*options) 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 // 设置默认的成功code
func SetDefaultSuccessCode(code int) Option { func SetDefaultSuccessCode(code int) Option {
return func(o *options) { return func(o *options) {
+25 -30
View File
@@ -50,18 +50,18 @@ func GetPage(page, size, totalCount int64) pagination {
} }
// Msg格式化响应 // Msg格式化响应
func FormatMessage(ctx context.Context, w http.ResponseWriter, message string, format map[string]string, data interface{}) { func FormatMessage(ctx context.Context, w http.ResponseWriter, msgKey string, format map[string]string, data interface{}) {
code, msg := langx.GetTransFormatCtx(ctx, message, format) code, msg := langx.GetTransFormatCtx(ctx, msgKey, format)
ResponseCtx(ctx, w, code, msg, data) ResponseCtx(ctx, w, SetCode(code), SetMessage(msg), SetData(data))
} }
// 成功的响应 // 成功的响应
func Success(ctx context.Context, w http.ResponseWriter, data interface{}, info ...interface{}) { func Success(ctx context.Context, w http.ResponseWriter, data any) {
ResponseCtx(ctx, w, op.defaultSuccessCode, "请求成功", data, info) ResponseCtx(ctx, w, SetCode(op.defaultSuccessCode), SetMessage("请求成功"), SetData(data))
} }
func SuccessWithPage(ctx context.Context, w http.ResponseWriter, data interface{}, page pagination) { func SuccessWithPage(ctx context.Context, w http.ResponseWriter, data any, page *pagination) {
ResponseCtx(ctx, w, op.defaultSuccessCode, "请求成功", data, page) 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() 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) { 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() { if code == langx.GetDefaultCode() {
code = op.defaultErrorCode 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) {
def := defaultOptions() // 默认值
for _, o := range ops {
o(def)
}
res := make(map[string]interface{}) res := make(map[string]interface{})
res["code"] = code res["code"] = def.code
res["message"] = message res["message"] = def.message
// 分页的内容 if def.data != nil {
var page *pagination res["data"] = def.data
for _, value := range other {
vv, ok := value.(pagination)
if ok {
page = &vv
} }
} if def.pagination != nil {
res["pagination"] = def.pagination
if page == nil {
// 不需要分页
res["data"] = data
} else {
res["data"] = data
res["pagination"] = page
} }
// 获取trace_id // 获取trace_id
if op.traceId != "" { if def.traceId != "" {
t := ctx.Value(op.traceId) t := ctx.Value(def.traceId)
if t != nil { if t != nil {
res["trace_id"] = t res["trace_id"] = t
} else { } 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) bs, _ := json.Marshal(res)