reply.go 3.6 KB

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