package resp import ( "github.com/bytedance/sonic" ) const ( CodeOK = 200 CodeFail = 400 CodeError = 500 ) type H map[string]interface{} // 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, error) { json, err := sonic.Marshal(resp) if err != nil { return nil, err } return json, nil } func (resp *Response) JsonString() (string, error) { bytes, err := resp.Json() if err != nil { return "", err } return string(bytes), nil } func RespSeq(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 RespSeq(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) }