reply.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package util
  2. const (
  3. // ErrOk OK
  4. ErrOk = 0
  5. // ErrNotFound 404 route not found
  6. ErrNotFound = 1001
  7. // ErrException 500
  8. ErrException = 1002
  9. // ErrBadRequest 400 route params error
  10. ErrBadRequest = 1003
  11. // ErrMethodNotAllowed 405
  12. ErrMethodNotAllowed = 1004
  13. // ErrParamsError 415
  14. ErrParamsError = 1005
  15. // ErrUnAuthorized 401
  16. ErrUnAuthorized = 1006
  17. // ErrDataNotFound 404
  18. ErrDataNotFound = 1007
  19. // ErrNotAllowed 405
  20. ErrNotAllowed = 1008
  21. // ErrDataExists 400
  22. ErrDataExists = 1009
  23. // ErrDataValidate 403
  24. ErrDataValidate = 1010
  25. // VarUserAuthorization oauth token
  26. VarUserAuthorization = `access_token`
  27. // HTTPHeaderAuthorization HTTP header Authorization
  28. HTTPHeaderAuthorization = `Authorization`
  29. )
  30. var (
  31. statusMessage map[int]string
  32. )
  33. // ReplyData define API output data
  34. type ReplyData struct {
  35. Status int `json:"status" xml:"status"` // Status code
  36. Message string `json:"message" xml:"message"` // Message description
  37. Errs map[string]string `json:"errors,omitempty" xml:"errors,omitempty"` // Errs errors
  38. PageCount int `json:"pageCount,omitempty"`
  39. Total int `json:"total,omitempty" xml:"total,omitempty"` // Total data total
  40. List interface{} `json:"rows,omitempty" xml:"rows,omitempty"` // List data list
  41. Data interface{} `json:"data,omitempty" xml:"data,omitempty"` // Data data attribute
  42. }
  43. func init() {
  44. statusMessage = make(map[int]string)
  45. statusMessage[ErrOk] = `ok`
  46. statusMessage[ErrNotFound] = `Not found`
  47. statusMessage[ErrException] = `Exception`
  48. statusMessage[ErrBadRequest] = `Routing parameter error`
  49. statusMessage[ErrMethodNotAllowed] = `Method not allowed`
  50. statusMessage[ErrParamsError] = `Parameter or format error`
  51. statusMessage[ErrUnAuthorized] = `Not sign in or session has expired`
  52. statusMessage[ErrDataNotFound] = `Data not found`
  53. statusMessage[ErrNotAllowed] = `No access`
  54. statusMessage[ErrDataExists] = `Data exists`
  55. statusMessage[ErrDataValidate] = `Data verification failed`
  56. }
  57. // NewReplyData creates and return ReplyData with status and message
  58. func NewReplyData(status int) *ReplyData {
  59. var (
  60. text string
  61. exists bool
  62. )
  63. if text, exists = statusMessage[status]; !exists {
  64. text = `incorrect data type`
  65. }
  66. return &ReplyData{
  67. Status: status,
  68. Message: text,
  69. }
  70. }
  71. // OkReplyData creates and return ReplyData with ok
  72. func OkReplyData() *ReplyData {
  73. message, _ := statusMessage[ErrOk]
  74. return &ReplyData{
  75. Status: ErrOk,
  76. Message: message,
  77. }
  78. }
  79. // ErrReplyData creates and return ReplyData with error and message
  80. func ErrReplyData(status int, message string) *ReplyData {
  81. text, _ := statusMessage[status]
  82. errs := map[string]string{
  83. "message": message,
  84. }
  85. return &ReplyData{
  86. Status: status,
  87. Message: text,
  88. Errs: errs,
  89. }
  90. }
  91. // ErrorsReplyData creates and return ReplyData with errors
  92. func ErrorsReplyData(status int, errors map[string]string) *ReplyData {
  93. message, _ := statusMessage[status]
  94. return &ReplyData{
  95. Status: status,
  96. Message: message,
  97. Errs: errors,
  98. }
  99. }
  100. // RowsReplyData creates and return ReplyData with total and list
  101. func RowsReplyData(total, pageCount int, rows interface{}) *ReplyData {
  102. message, _ := statusMessage[ErrOk]
  103. return &ReplyData{
  104. Status: ErrOk,
  105. Message: message,
  106. List: rows,
  107. Total: total,
  108. PageCount: pageCount,
  109. }
  110. }
  111. // RowReplyData creates and return ReplyData with attr row
  112. func RowReplyData(row interface{}) *ReplyData {
  113. message, _ := statusMessage[ErrOk]
  114. return &ReplyData{
  115. Status: ErrOk,
  116. Message: message,
  117. Data: row,
  118. }
  119. }