common.go 841 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package controller
  2. import "github.com/gin-gonic/gin"
  3. type JsonStruct struct {
  4. Code int `json:"code"`
  5. Msg interface{} `json:"msg"`
  6. Data interface{} `json:"data"`
  7. }
  8. type JsonErrStruct struct {
  9. Code int `json:"code"`
  10. Msg interface{} `json:"msg"`
  11. }
  12. func returnSuccess(c *gin.Context, code int, data interface{}) {
  13. json := &JsonStruct{Code: code, Msg: "ok", Data: data}
  14. c.JSON(200, json)
  15. }
  16. func returnSuccessWithMessage(c *gin.Context, code int, msg string, data interface{}) {
  17. if msg == "" {
  18. msg = "ok"
  19. }
  20. json := &JsonStruct{Code: code, Msg: msg, Data: data}
  21. c.JSON(200, json)
  22. }
  23. func returnError(c *gin.Context, code int, msg string) {
  24. json := &JsonErrStruct{}
  25. if msg == "" {
  26. json = &JsonErrStruct{Code: code, Msg: "error"}
  27. } else {
  28. json = &JsonErrStruct{Code: code, Msg: msg}
  29. }
  30. c.JSON(400, json)
  31. }