reply.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package toolkit
  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. Total int `json:"total,omitempty" xml:"total,omitempty"` // Total data total
  39. List interface{} `json:"rows,omitempty" xml:"rows,omitempty"` // List data list
  40. Data interface{} `json:"data,omitempty" xml:"data,omitempty"` // Data data attribute
  41. }
  42. func init() {
  43. statusMessage = make(map[int]string)
  44. statusMessage[ErrOk] = `ok`
  45. statusMessage[ErrNotFound] = `Not found`
  46. statusMessage[ErrException] = `Exception`
  47. statusMessage[ErrBadRequest] = `Routing parameter error`
  48. statusMessage[ErrMethodNotAllowed] = `Method not allowed`
  49. statusMessage[ErrParamsError] = `Parameter or format error`
  50. statusMessage[ErrUnAuthorized] = `Not sign in or session has expired`
  51. statusMessage[ErrDataNotFound] = `Data not found`
  52. statusMessage[ErrNotAllowed] = `No access`
  53. statusMessage[ErrDataExists] = `Data exists`
  54. statusMessage[ErrDataValidate] = `Data verification failed`
  55. }
  56. // NewReplyData creates and return ReplyData with status and message
  57. func NewReplyData(status int) *ReplyData {
  58. var (
  59. text string
  60. exists bool
  61. )
  62. if text, exists = statusMessage[status]; !exists {
  63. text = `incorrect data type`
  64. }
  65. return &ReplyData{
  66. Status: status,
  67. Message: text,
  68. }
  69. }
  70. // OkReplyData creates and return ReplyData with ok
  71. func OkReplyData() *ReplyData {
  72. message, _ := statusMessage[ErrOk]
  73. return &ReplyData{
  74. Status: ErrOk,
  75. Message: message,
  76. }
  77. }
  78. // ErrReplyData creates and return ReplyData with error and message
  79. func ErrReplyData(status int, message string) *ReplyData {
  80. text, _ := statusMessage[status]
  81. errs := map[string]string{
  82. "message": message,
  83. }
  84. return &ReplyData{
  85. Status: status,
  86. Message: text,
  87. Errs: errs,
  88. }
  89. }
  90. // ErrorsReplyData creates and return ReplyData with errors
  91. func ErrorsReplyData(status int, errors map[string]string) *ReplyData {
  92. message, _ := statusMessage[status]
  93. return &ReplyData{
  94. Status: status,
  95. Message: message,
  96. Errs: errors,
  97. }
  98. }
  99. // RowsReplyData creates and return ReplyData with total and list
  100. func RowsReplyData(total int, rows interface{}) *ReplyData {
  101. message, _ := statusMessage[ErrOk]
  102. return &ReplyData{
  103. Status: ErrOk,
  104. Message: message,
  105. List: rows,
  106. Total: total,
  107. }
  108. }
  109. // RowReplyData creates and return ReplyData with attr row
  110. func RowReplyData(row interface{}) *ReplyData {
  111. message, _ := statusMessage[ErrOk]
  112. return &ReplyData{
  113. Status: ErrOk,
  114. Message: message,
  115. Data: row,
  116. }
  117. }