response.go 539 B

12345678910111213141516171819202122232425262728
  1. package handlers
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "net/http"
  5. )
  6. func ResponseSuccess(c *gin.Context, data interface{}) {
  7. c.JSON(http.StatusOK, map[string]interface{}{
  8. "code": 0,
  9. "data": data,
  10. })
  11. }
  12. //使用这个方法需要返回自己定义的错误
  13. func ResponseErr(c *gin.Context, err error) {
  14. c.JSON(http.StatusOK, err)
  15. }
  16. func ResponseErrWithCode(c *gin.Context, code int, err error) {
  17. var resp = map[string]interface{}{
  18. "code": code,
  19. }
  20. if err != nil {
  21. resp["err"] = err.Error()
  22. }
  23. c.JSON(http.StatusOK, resp)
  24. }