common.go 680 B

123456789101112131415161718192021222324252627282930
  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. //Count int64 `json:"count"`
  8. }
  9. type JsonErrStruct struct {
  10. Code int `json:"code"`
  11. Msg interface{} `json:"msg"`
  12. }
  13. func returnSuccess(c *gin.Context, code int, data interface{}) {
  14. json := &JsonStruct{Code: code, Msg: "ok", Data: data}
  15. c.JSON(200, json)
  16. }
  17. func returnError(c *gin.Context, code int, msg string) {
  18. json := &JsonErrStruct{}
  19. if msg == "" {
  20. json = &JsonErrStruct{Code: code, Msg: "error"}
  21. } else {
  22. json = &JsonErrStruct{Code: code, Msg: msg}
  23. }
  24. c.JSON(400, json)
  25. }