3 Commits

Author SHA1 Message Date
yun d3786198df 调整日志 2025-12-07 12:07:21 +08:00
yun ce2190e3de 响应优化 2025-12-07 11:21:00 +08:00
yun cd0df49d4b 优化响应的方式 2025-12-07 01:51:01 +08:00
3 changed files with 86 additions and 44 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) {
+38 -37
View File
@@ -14,6 +14,14 @@ import (
"github.com/zeromicro/go-zero/core/trace" "github.com/zeromicro/go-zero/core/trace"
) )
type response struct {
Code int `json:"code"`
Message string `json:"message"`
Data any `json:"data,omitempty"`
Pagination *pagination `json:"pagination,omitempty"`
TraceId string `json:"trace_id,omitempty"`
}
type pagination struct { type pagination struct {
Page int64 `json:"page"` Page int64 `json:"page"`
Size int64 `json:"size"` Size int64 `json:"size"`
@@ -50,18 +58,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 +89,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,50 +98,43 @@ 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) {
res := make(map[string]interface{}) def := defaultOptions() // 默认值
res["code"] = code for _, o := range ops {
res["message"] = message o(def)
// 分页的内容
var page *pagination
for _, value := range other {
vv, ok := value.(pagination)
if ok {
page = &vv
}
} }
if page == nil { resp := response{}
// 不需要分页 resp.Code = def.code
res["data"] = data resp.Message = def.message
} else { resp.Data = def.data
res["data"] = data resp.Pagination = def.pagination
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 val, ok := t.(string)
if ok {
resp.TraceId = val
}
} else { } else {
// go-zero // go-zero
res["trace_id"] = trace.TraceIDFromContext(ctx) resp.TraceId = trace.TraceIDFromContext(ctx)
} }
} }
if !op.ignoreLog { bs, _ := json.Marshal(resp)
// 记录日志
op.logger.Info(ctx, res)
}
bs, _ := json.Marshal(res) if !def.ignoreLog {
// 记录日志
def.logger.Info(ctx, "response:", string(bs))
}
w.Header().Set("Content-Type", "application/json; charset=utf-8") w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(200) w.WriteHeader(200)