优化响应的方式
This commit is contained in:
+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) {
|
||||
|
||||
Reference in New Issue
Block a user