base.go 748 B

12345678910111213141516171819202122232425262728293031323334
  1. package api
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "net/http"
  5. )
  6. type BaseController struct {
  7. }
  8. // PageData 分页数据返回格式
  9. type pageData struct {
  10. Limit interface{} `json:"limit"` // 分页数量
  11. Items interface{} `json:"items"` // 数据列表
  12. TotalCount int `json:"total"` // 总数量
  13. }
  14. // ResponseSuccess 成功响应格式
  15. func (b BaseController) responseSuccess(c *gin.Context, data interface{}) {
  16. c.JSON(http.StatusOK, gin.H{
  17. "code": 0,
  18. "message": "success",
  19. "data": data,
  20. })
  21. }
  22. // ResponseError 错误响应格式
  23. func (b BaseController) responseError(c *gin.Context, code int, message string) {
  24. c.JSON(http.StatusOK, gin.H{
  25. "code": code,
  26. "message": message,
  27. "data": nil,
  28. })
  29. }