beegoinit/response/json.go
2020-07-10 10:51:40 +08:00

52 lines
963 B
Go

package response
import (
"bytes"
"encoding/json"
"golib.gaore.com/GaoreGo/grlogs"
)
const (
JsonMsgDefaultOk = "ok"
JsonMsgDefaultFailed = "unknown"
)
type Json struct {
Code int `json:"code"`
Data interface{} `json:"data"`
Msg string `json:"msg"`
Status bool `json:"status"`
}
func (j *Json) String() string {
b, err := json.Marshal(j)
grlogs.Debug(err)
return bytes.NewBuffer(b).String()
}
func NewJsonByDefaultSuccess(data ...interface{}) *Json {
return NewJson(true, 0, JsonMsgDefaultOk, data...)
}
func NewJsonByDefaultFailed(data ...interface{}) *Json {
return NewJson(false, 1, JsonMsgDefaultFailed, data...)
}
func NewJson(status bool, code int, msg string, data ...interface{}) *Json {
if len(data) > 0 {
return &Json{
Code: code,
Data: data[0],
Msg: msg,
Status: status,
}
} else {
return &Json{
Code: code,
Data: nil,
Msg: msg,
Status: status,
}
}
}