response.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package response
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "net/http"
  5. )
  6. type Response struct {
  7. Code int `json:"code"`
  8. Message string `json:"message"`
  9. Data interface{} `json:"data"`
  10. }
  11. type PageData struct {
  12. Page int `json:"page"`
  13. Total int `json:"total"`
  14. List interface{} `json:"list"`
  15. }
  16. type DataResponse struct {
  17. Total uint `json:"total"`
  18. Data interface{} `json:"data"`
  19. }
  20. type ErrorResponse struct {
  21. Error string `json:"error"`
  22. }
  23. func SendResponse(c *gin.Context, code int, message string, data interface{}) {
  24. c.JSON(http.StatusOK, Response{
  25. code, message, data,
  26. })
  27. }
  28. func Success(c *gin.Context, data interface{}) {
  29. SendResponse(c, 0, "success", data)
  30. }
  31. func Fail(c *gin.Context, code int, message string) {
  32. SendResponse(c, code, message, nil)
  33. }
  34. func Error(c *gin.Context, message string) {
  35. c.JSON(http.StatusBadRequest, ErrorResponse{
  36. Error: message,
  37. })
  38. }
  39. type ServerConfigResponse struct {
  40. IdServer string `json:"id_server"`
  41. Key string `json:"key"`
  42. RelayServer string `json:"relay_server"`
  43. ApiServer string `json:"api_server"`
  44. }