|
|
@@ -12,11 +12,27 @@ const ( |
|
|
|
|
|
|
|
type Json struct { |
|
|
|
Code int `json:"code"` |
|
|
|
Data interface{} `json:"data"` |
|
|
|
Data interface{} `json:"data,omitempty"` |
|
|
|
Msg string `json:"msg"` |
|
|
|
Status bool `json:"status"` |
|
|
|
} |
|
|
|
|
|
|
|
func (j *Json) SetCode(code int) { |
|
|
|
j.Code = code |
|
|
|
} |
|
|
|
|
|
|
|
func (j *Json) SetMessage(msg string) { |
|
|
|
j.Msg = msg |
|
|
|
} |
|
|
|
|
|
|
|
func (j *Json) SetData(data interface{}) { |
|
|
|
j.Data = data |
|
|
|
} |
|
|
|
|
|
|
|
func (j *Json) SetStatus(status bool) { |
|
|
|
j.Status = status |
|
|
|
} |
|
|
|
|
|
|
|
func (j *Json) String() string { |
|
|
|
s, _ := j.ToString() |
|
|
|
return s |
|
|
@@ -28,15 +44,19 @@ func (j *Json) ToString() (str string, err error) { |
|
|
|
return bytes.NewBuffer(b).String(), err |
|
|
|
} |
|
|
|
|
|
|
|
func NewJsonByDefaultSuccess(data ...interface{}) *Json { |
|
|
|
func NewJsonByDefaultSuccess(data ...interface{}) JsonInterface { |
|
|
|
return NewJson(true, 0, JsonMsgDefaultOk, data...) |
|
|
|
} |
|
|
|
|
|
|
|
func NewJsonByDefaultFailed(data ...interface{}) *Json { |
|
|
|
func NewJsonByDefaultFailed(data ...interface{}) JsonInterface { |
|
|
|
return NewJson(false, 1, JsonMsgDefaultFailed, data...) |
|
|
|
} |
|
|
|
|
|
|
|
func NewJson(status bool, code int, msg string, data ...interface{}) *Json { |
|
|
|
func NewJonsByFailed(code int, msg string) JsonInterface { |
|
|
|
return NewJson(false, code, msg) |
|
|
|
} |
|
|
|
|
|
|
|
func NewJson(status bool, code int, msg string, data ...interface{}) JsonInterface { |
|
|
|
if len(data) > 0 { |
|
|
|
return &Json{ |
|
|
|
Code: code, |
|
|
|