beegoinit/response/json.go
liangzy eba7a63f4b 两项优化
1. 优化json类
2. 优化数据库默认连接配置
2020-08-11 14:31:27 +08:00

56 lines
1015 B
Go

package response
import (
"bytes"
"encoding/json"
)
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 {
s, _ := j.ToString()
return s
}
func (j *Json) ToString() (str string, err error) {
var b []byte
b, err = json.Marshal(j)
return bytes.NewBuffer(b).String(), err
}
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,
}
}
}