result.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package result
  2. type Result struct {
  3. Code int32 `json:"code"`
  4. Msg string `json:"msg"`
  5. Data interface{} `json:"data"`
  6. }
  7. func (errcode *Result) Error() string {
  8. return errcode.Msg
  9. }
  10. func NewResult(Code int32, Msg string, Data interface{}) *Result {
  11. return &Result{Code, Msg, Data}
  12. }
  13. func SuccessResult(Data interface{}) *Result {
  14. return &Result{2000, "操作成功", Data}
  15. }
  16. func (r Result) SetMsg(msg string) *Result {
  17. r.Msg = msg
  18. return &r
  19. }
  20. func (r Result) SetData(data interface{}) *Result {
  21. r.Data = data
  22. return &r
  23. }
  24. type Page struct {
  25. Page int `json:"page" db:"page"`
  26. Size int `json:"size,omitempty" db:"size,omitempty"`
  27. Total int `json:"total,omitempty" db:"total,omitempty"`
  28. Data interface{} `json:"data,omitempty" db:"data,omitempty"`
  29. }
  30. func NewPage(page, size, total int, data interface{}) *Page {
  31. return &Page{
  32. page,
  33. size,
  34. total,
  35. data,
  36. }
  37. }
  38. // ResultChange 影响行数
  39. type ResultChange struct {
  40. RowsAffected int64 `json:"rowsAffected"`
  41. RowsFailed int64 `json:"rowsFailed"`
  42. }
  43. func NewResultChange(row int64) ResultChange {
  44. return ResultChange{row, 0}
  45. }
  46. var (
  47. SUCCESS = NewResult(2000, "操作成功", nil)
  48. UNAUTHORIZED = NewResult(4001, "未授权", nil)
  49. FORMATE_ERROR = NewResult(4002, "格式转换异常", nil)
  50. NO_PERMISSION = NewResult(4003, "无权限", nil)
  51. DATA_NOT_FOUND = NewResult(4004, "不存在该数据", nil)
  52. DATA_NO_CHANGE = NewResult(4005, "数据未发生改变", nil)
  53. PASSWORD_ERROR = NewResult(4006, "密码错误", nil)
  54. PARAM_ERROR = NewResult(4007, "参数错误", nil)
  55. USER_IS_EXISTED = NewResult(4008, "用户已存在", nil)
  56. USER_IS_NOT_EXISTED = NewResult(4009, "用户不存在", nil)
  57. USER_PENDING_REVIEW = NewResult(4010, "用户待审核", nil)
  58. ROLE_IS_NOT_EXISTED = NewResult(4011, "角色不存在", nil)
  59. UNKNOW_ERROR = NewResult(5000, "未知异常", nil)
  60. )