response.go 839 B

12345678910111213141516171819202122232425262728293031323334
  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. // PageResult 分页结果返回
  12. type PageResult struct {
  13. Total int64 `json:"total"`
  14. Data interface{} `json:"data"`
  15. }
  16. // Success 请求成功响应
  17. func Success(message string, data interface{}, c *gin.Context) {
  18. c.JSON(http.StatusOK, Response{200, message, data})
  19. }
  20. // Failed 请求失败响应
  21. func Failed(message string, c *gin.Context) {
  22. c.JSON(http.StatusOK, Response{400, message, 0})
  23. }
  24. // SuccessPage 请求成功返回分页结果
  25. func SuccessPage(message string, data interface{}, rows int64, c *gin.Context) {
  26. page := &PageResult{Total: rows, Data: data}
  27. c.JSON(http.StatusOK, Response{200, message, page})
  28. }