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. Data interface{} `json:"data"`
  9. Msg string `json:"msg"`
  10. }
  11. const (
  12. ERROR = 7
  13. SUCCESS = 0
  14. )
  15. func Result(code int, data interface{}, msg string, c *gin.Context) {
  16. // 开始时间
  17. c.JSON(http.StatusOK, Response{
  18. code,
  19. data,
  20. msg,
  21. })
  22. }
  23. func Ok(c *gin.Context) {
  24. Result(SUCCESS, map[string]interface{}{}, "操作成功", c)
  25. }
  26. func OkWithMessage(message string, c *gin.Context) {
  27. Result(SUCCESS, map[string]interface{}{}, message, c)
  28. }
  29. func OkWithData(data interface{}, c *gin.Context) {
  30. Result(SUCCESS, data, "操作成功", c)
  31. }
  32. func OkWithDetailed(data interface{}, message string, c *gin.Context) {
  33. Result(SUCCESS, data, message, c)
  34. }
  35. func Fail(c *gin.Context) {
  36. Result(ERROR, map[string]interface{}{}, "操作失败", c)
  37. }
  38. func FailWithMessage(message string, c *gin.Context) {
  39. Result(ERROR, map[string]interface{}{}, message, c)
  40. }
  41. func FailWithDetailed(data interface{}, message string, c *gin.Context) {
  42. Result(ERROR, data, message, c)
  43. }