resp.go 605 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package models
  2. type RespData struct {
  3. Code int `json:"code"`
  4. Msg string `json:"msg"`
  5. Data interface{} `json:"data"`
  6. }
  7. func (r *RespData) Success() bool {
  8. return r.Code == 0
  9. }
  10. func (r *RespData) SetCode(code int) *RespData {
  11. r.Code = code
  12. return r
  13. }
  14. func SuccessResp(data interface{}) RespData {
  15. return RespData{
  16. Code: 0,
  17. Msg: "请求成功",
  18. Data: data,
  19. }
  20. }
  21. func ErrorResp(msg string) RespData {
  22. return RespData{
  23. Code: -1,
  24. Msg: msg,
  25. Data: nil,
  26. }
  27. }
  28. func NewErrorResp(error error) RespData {
  29. return RespData{
  30. Code: -1,
  31. Msg: error.Error(),
  32. Data: nil,
  33. }
  34. }