You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.1 KiB

  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 OkDetailed(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(code int, data interface{}, message string, c *gin.Context) {
  42. Result(code, data, message, c)
  43. }