优化响应的方式

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