Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d3786198df | |||
| ce2190e3de | |||
| cd0df49d4b |
@@ -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
@@ -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) {
|
||||
|
||||
+37
-36
@@ -14,6 +14,14 @@ import (
|
||||
"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 {
|
||||
Page int64 `json:"page"`
|
||||
Size int64 `json:"size"`
|
||||
@@ -50,18 +58,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 +89,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,51 +98,44 @@ 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
|
||||
}
|
||||
resp := response{}
|
||||
resp.Code = def.code
|
||||
resp.Message = def.message
|
||||
resp.Data = def.data
|
||||
resp.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
|
||||
val, ok := t.(string)
|
||||
if ok {
|
||||
resp.TraceId = val
|
||||
}
|
||||
} else {
|
||||
// go-zero
|
||||
res["trace_id"] = trace.TraceIDFromContext(ctx)
|
||||
resp.TraceId = trace.TraceIDFromContext(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
if !op.ignoreLog {
|
||||
bs, _ := json.Marshal(resp)
|
||||
|
||||
if !def.ignoreLog {
|
||||
// 记录日志
|
||||
op.logger.Info(ctx, res)
|
||||
def.logger.Info(ctx, "response:", string(bs))
|
||||
}
|
||||
|
||||
bs, _ := json.Marshal(res)
|
||||
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.WriteHeader(200)
|
||||
n, err := w.Write(bs)
|
||||
|
||||
Reference in New Issue
Block a user