gocron/modules/utils/json.go

43 lines
915 B
Go
Raw Normal View History

2017-04-01 09:04:40 +00:00
package utils
2017-04-03 07:27:19 +00:00
import (
"encoding/json"
2017-04-07 01:22:00 +00:00
"github.com/ouqiang/gocron/modules/logger"
2017-04-03 07:27:19 +00:00
)
2017-04-01 09:04:40 +00:00
// json 格式输出
type response struct {
2017-04-02 02:38:49 +00:00
Code int `json:"code"` // 状态码 0:成功 非0:失败
Message string `json:"message"` // 信息
Data interface{} `json:"data"` // 数据
2017-04-01 09:04:40 +00:00
}
2017-04-02 02:19:52 +00:00
type Json struct{}
2017-04-01 09:04:40 +00:00
2017-04-02 02:19:52 +00:00
const ResponseSuccess = 0
const ResponseFailure = 1
2017-04-01 09:04:40 +00:00
2017-04-02 02:19:52 +00:00
func (j *Json) Success(message string, data interface{}) string {
2017-04-02 02:38:49 +00:00
return j.response(ResponseSuccess, message, data)
2017-04-01 09:04:40 +00:00
}
2017-04-02 02:19:52 +00:00
func (j *Json) Failure(code int, message string) string {
2017-04-02 02:38:49 +00:00
return j.response(code, message, nil)
2017-04-01 09:04:40 +00:00
}
2017-04-02 02:19:52 +00:00
func (j *Json) response(code int, message string, data interface{}) string {
2017-04-02 02:38:49 +00:00
resp := response{
Code: code,
Message: message,
Data: data,
}
result, err := json.Marshal(resp)
if err != nil {
2017-04-03 07:27:19 +00:00
logger.Error(err)
2017-04-02 02:38:49 +00:00
}
return string(result)
2017-04-01 09:04:40 +00:00
}