common.go 923 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package common
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "net/http"
  5. )
  6. type ResponseJson struct {
  7. Code uint `yaml:"code" json:"code"` // response code
  8. Message string `yaml:"message" json:"message"` // response message
  9. Data interface{} `yaml:"data" json:"data,omitempty"` // response data
  10. }
  11. /**
  12. 返回成功Response
  13. */
  14. func ReturnSuccess(c *gin.Context, data interface{}) {
  15. if data != nil {
  16. c.JSON(http.StatusOK, gin.H{
  17. "code": 200,
  18. "message": "SUCCESS",
  19. "data": data,
  20. })
  21. } else {
  22. c.JSON(http.StatusOK, gin.H{
  23. "code": 200,
  24. "message": "SUCCESS",
  25. })
  26. }
  27. }
  28. /**
  29. 返回失败Response
  30. */
  31. func ReturnFailure(c *gin.Context, code int) {
  32. c.JSON(http.StatusOK, gin.H{
  33. "code": code,
  34. "message": ErrorMap[code],
  35. })
  36. }
  37. func ReturnFailureMsg(c *gin.Context, code int, msg string) {
  38. c.JSON(http.StatusOK, gin.H{
  39. "code": code,
  40. "message": msg,
  41. })
  42. }