response.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 CodeMessage(code int, message string, c *gin.Context) {
  30. Result(code, map[string]interface{}{}, message, c)
  31. }
  32. func CodeWithDetailed(code int, data interface{}, message string, c *gin.Context) {
  33. Result(code, data, message, c)
  34. }
  35. func OkWithData(data interface{}, c *gin.Context) {
  36. Result(SUCCESS, data, "操作成功", c)
  37. }
  38. func OkWithDetailed(data interface{}, message string, c *gin.Context) {
  39. Result(SUCCESS, data, message, c)
  40. }
  41. func Fail(c *gin.Context) {
  42. Result(ERROR, map[string]interface{}{}, "操作失败", c)
  43. }
  44. func FailWithMessage(message string, c *gin.Context) {
  45. Result(ERROR, map[string]interface{}{}, message, c)
  46. }
  47. func FailWithDetailed(data interface{}, message string, c *gin.Context) {
  48. Result(ERROR, data, message, c)
  49. }