response.go 695 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package app
  2. import (
  3. "tmr-watch/pkg/e"
  4. "github.com/gin-gonic/gin"
  5. )
  6. type Gin struct {
  7. C *gin.Context
  8. }
  9. func (g *Gin) Response(httpCode, errCode int, data interface{}) {
  10. g.C.JSON(httpCode, gin.H{
  11. "code": httpCode,
  12. "msg": e.GetMsg(errCode),
  13. "data": data,
  14. })
  15. return
  16. }
  17. func (g *Gin) ResponseEq(httpCode, errCode int, errorNo int, data interface{}) {
  18. g.C.JSON(httpCode, gin.H{
  19. "code": httpCode,
  20. "message": e.GetMsg(errCode),
  21. "results": data,
  22. "errorNo": errorNo,
  23. "success": 1,
  24. })
  25. return
  26. }
  27. func (g *Gin) Get(key string) string {
  28. if str, ok := g.C.Get(key); !ok {
  29. return ""
  30. } else {
  31. switch str.(type) {
  32. case string:
  33. return str.(string)
  34. }
  35. }
  36. return ""
  37. }