ginUtils.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /**
  2. * @Author: zou.yingbin
  3. * @Create : 2021/1/25 9:42
  4. * @Modify : 2021/1/25 9:42
  5. */
  6. package app
  7. import (
  8. "fmt"
  9. "github.com/gin-gonic/gin"
  10. "mtp2_if/global/e"
  11. "mtp2_if/logger"
  12. "net/http"
  13. )
  14. // GetData 获取数据接口
  15. type IGetData interface {
  16. GetData() ([]interface{}, error)
  17. }
  18. // IGetDataEx 获取数据接口
  19. type IGetDataEx interface {
  20. GetDataEx() (interface{}, error)
  21. }
  22. // ICalc 数据处理接口
  23. type ICalc interface {
  24. Calc()
  25. }
  26. // Check 参数检查接口
  27. type ICheck interface {
  28. Check() bool
  29. }
  30. // GinUtils 封装绑定和数据获取处理
  31. type GinUtils struct {
  32. Gin
  33. err error
  34. }
  35. // DoBindReq 处理参数绑定
  36. func (r *GinUtils) DoBindReq(req interface{}) {
  37. if r.err = r.C.ShouldBind(req); r.err != nil {
  38. r.Gin.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  39. }
  40. }
  41. // DoGetData 处理数据获取, 参数obj为models结构体
  42. // Deprecated
  43. func (r *GinUtils) DoGetData(obj interface{}) {
  44. // 如果在参数绑定阶段有错误, 则不执行数据获取
  45. if r.err != nil {
  46. return
  47. }
  48. if v, ok := obj.(IGetData); ok {
  49. if d, err := v.GetData(); err == nil {
  50. // 执行数据处理
  51. for i := range d {
  52. if val, ok := d[i].(ICalc); ok {
  53. val.Calc()
  54. } else {
  55. break
  56. }
  57. }
  58. r.Gin.Response(http.StatusOK, e.SUCCESS, d)
  59. } else {
  60. logger.GetLogger().Errorf("query fail, %v", err)
  61. r.Gin.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  62. }
  63. }
  64. }
  65. // DoGetDataEx 处理数据获取, 参数obj为models结构体
  66. // Deprecated
  67. func (r *GinUtils) DoGetDataEx(modelObj interface{}) {
  68. // 如果在参数绑定阶段有错误, 则不执行数据获取
  69. if r.err != nil {
  70. return
  71. }
  72. if v, ok := modelObj.(IGetDataEx); ok {
  73. if d, err := v.GetDataEx(); err == nil {
  74. r.Gin.Response(http.StatusOK, e.SUCCESS, d)
  75. } else {
  76. logger.GetLogger().Errorf("query fail, %v", err)
  77. r.Gin.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  78. }
  79. }
  80. }
  81. // DoGetDataI 获取数据
  82. func (r *GinUtils) DoGetDataI(iObj IGetDataEx) {
  83. // 如果在参数绑定阶段有错误, 则不执行数据获取
  84. if r.err != nil {
  85. return
  86. }
  87. if d, err := iObj.GetDataEx(); err == nil {
  88. r.Gin.Response(http.StatusOK, e.SUCCESS, d)
  89. } else {
  90. logger.GetLogger().Errorf("query fail, %v", err)
  91. r.Gin.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  92. }
  93. }
  94. // CheckParam 参数检查
  95. func (r *GinUtils) CheckParam(iObj ICheck) {
  96. if iObj.Check() == false {
  97. r.err = fmt.Errorf("check param err")
  98. r.Gin.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  99. }
  100. }
  101. // CheckParamF 参数检查
  102. func (r *GinUtils) CheckParamF(f func() bool) {
  103. if f() == false {
  104. r.err = fmt.Errorf("check param err")
  105. r.Gin.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  106. }
  107. }
  108. // NewGinUtils 生成处理GinUtils实例
  109. func NewGinUtils(c *gin.Context) *GinUtils {
  110. return &GinUtils{Gin: Gin{C: c}}
  111. }