63 lines
1.0 KiB
Go
63 lines
1.0 KiB
Go
package definition
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
)
|
|
|
|
func UnmarshalMsgModel(data []byte) (Response, error) {
|
|
var r Response
|
|
err := json.Unmarshal(data, &r)
|
|
return r, err
|
|
}
|
|
|
|
func (r *Response) Marshal() ([]byte, error) {
|
|
return json.Marshal(r)
|
|
}
|
|
|
|
type Response struct {
|
|
Code int `json:"code"`
|
|
Msg string `json:"msg"`
|
|
Data []byte `json:"data"`
|
|
}
|
|
|
|
func Resp() *Response {
|
|
// 初始化response
|
|
return &Response{
|
|
Code: 0,
|
|
Msg: "",
|
|
Data: nil,
|
|
}
|
|
}
|
|
|
|
// FailCode 自定义错误码返回
|
|
func (r *Response) Fail(msg ...string) *Response {
|
|
r.Code = http.StatusInternalServerError
|
|
if msg != nil {
|
|
r.Msg = msg[0]
|
|
}
|
|
return r
|
|
}
|
|
|
|
// FailCode 自定义错误码返回
|
|
func (r *Response) FailWithData(data []byte, msg ...string) *Response {
|
|
r.Code = http.StatusInternalServerError
|
|
if msg != nil {
|
|
r.Msg = msg[0]
|
|
}
|
|
r.Data = data
|
|
|
|
return r
|
|
}
|
|
|
|
// Success 正确返回
|
|
func (r *Response) Success(data []byte, msg ...string) *Response {
|
|
r.Code = http.StatusOK
|
|
if msg != nil {
|
|
r.Msg = msg[0]
|
|
}
|
|
r.Data = data
|
|
|
|
return r
|
|
}
|