优化响应的方式

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
+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)