You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
62 lines
1.1 KiB
62 lines
1.1 KiB
package resp |
|
|
|
import ( |
|
"sig-pub/pkg/zlog" |
|
|
|
"github.com/bytedance/sonic" |
|
) |
|
|
|
const ( |
|
CodeOK = 200 |
|
CodeFail = 400 |
|
CodeError = 500 |
|
) |
|
|
|
type H map[string]any |
|
|
|
// Response 响应体包装 |
|
type Response struct { |
|
Seq int `json:"seq,omitempty"` |
|
Code int `json:"code,omitempty"` |
|
Msg string `json:"msg,omitempty"` |
|
Data any `json:"data,omitempty"` |
|
Extra map[string]any `json:"extra,omitempty"` |
|
} |
|
|
|
func (resp *Response) Json() []byte { |
|
json, err := sonic.Marshal(resp) |
|
if err != nil { |
|
zlog.Error("unknown json error: ", err) |
|
return nil |
|
} |
|
return json |
|
} |
|
|
|
func (resp *Response) JsonString() string { |
|
return string(resp.Json()) |
|
} |
|
|
|
func SeqResp(seq int, code int, msg string, data any) *Response { |
|
return &Response{ |
|
Seq: seq, |
|
Code: code, |
|
Msg: msg, |
|
Data: data, |
|
} |
|
} |
|
|
|
func Resp(code int, msg string, data any) *Response { |
|
return SeqResp(0, code, msg, data) |
|
} |
|
|
|
func Success(data any) *Response { |
|
return Resp(CodeOK, "", data) |
|
} |
|
|
|
func Fail(msg string) *Response { |
|
return Resp(CodeFail, msg, nil) |
|
} |
|
|
|
func Error(msg string) *Response { |
|
return Resp(CodeError, msg, nil) |
|
}
|
|
|