统一初始beego mvc 的方法
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
963 B

  1. package response
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "golib.gaore.com/GaoreGo/grlogs"
  6. )
  7. const (
  8. JsonMsgDefaultOk = "ok"
  9. JsonMsgDefaultFailed = "unknown"
  10. )
  11. type Json struct {
  12. Code int `json:"code"`
  13. Data interface{} `json:"data"`
  14. Msg string `json:"msg"`
  15. Status bool `json:"status"`
  16. }
  17. func (j *Json) String() string {
  18. b, err := json.Marshal(j)
  19. grlogs.Debug(err)
  20. return bytes.NewBuffer(b).String()
  21. }
  22. func NewJsonByDefaultSuccess(data ...interface{}) *Json {
  23. return NewJson(true, 0, JsonMsgDefaultOk, data...)
  24. }
  25. func NewJsonByDefaultFailed(data ...interface{}) *Json {
  26. return NewJson(false, 1, JsonMsgDefaultFailed, data...)
  27. }
  28. func NewJson(status bool, code int, msg string, data ...interface{}) *Json {
  29. if len(data) > 0 {
  30. return &Json{
  31. Code: code,
  32. Data: data[0],
  33. Msg: msg,
  34. Status: status,
  35. }
  36. } else {
  37. return &Json{
  38. Code: code,
  39. Data: nil,
  40. Msg: msg,
  41. Status: status,
  42. }
  43. }
  44. }