ginUtils.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. "mtp2_if/global/e"
  10. "mtp2_if/logger"
  11. "net/http"
  12. "github.com/gin-gonic/gin"
  13. )
  14. // GetData 获取数据接口
  15. type IGetData interface {
  16. GetData() ([]interface{}, error)
  17. }
  18. // IGetDataEx 获取数据接口
  19. type IGetDataEx interface {
  20. GetDataEx() (interface{}, error)
  21. }
  22. // IGetDataByPage 获取数据接口
  23. type IGetDataByPage interface {
  24. // 返回值 数据, 错误号, 页码, 页大小, 总记录数
  25. GetDataByPage() (interface{}, error, int, int, int)
  26. }
  27. // ICalc 数据处理接口
  28. type ICalc interface {
  29. Calc()
  30. }
  31. // Check 参数检查接口
  32. type ICheck interface {
  33. Check() bool
  34. }
  35. // GinUtils 封装绑定和数据获取处理
  36. type GinUtils struct {
  37. Gin
  38. err error
  39. }
  40. // DoBindReq 处理参数绑定
  41. func (r *GinUtils) DoBindReq(req interface{}) {
  42. if r.err != nil {
  43. return
  44. }
  45. if r.err = r.C.ShouldBind(req); r.err != nil {
  46. r.Gin.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  47. }
  48. }
  49. // DoBindJsonReq 处理Json Body参数绑定
  50. func (r *GinUtils) DoBindJsonReq(req interface{}) {
  51. if r.err != nil {
  52. return
  53. }
  54. if r.err = r.C.ShouldBindJSON(req); r.err != nil {
  55. r.Gin.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  56. }
  57. }
  58. // DoGetData 处理数据获取, 参数obj为models结构体
  59. // Deprecated
  60. func (r *GinUtils) DoGetData(obj interface{}) {
  61. // 如果在参数绑定阶段有错误, 则不执行数据获取
  62. if r.err != nil {
  63. return
  64. }
  65. if v, ok := obj.(IGetData); ok {
  66. if d, err := v.GetData(); err == nil {
  67. // 执行数据处理
  68. for i := range d {
  69. if val, ok := d[i].(ICalc); ok {
  70. val.Calc()
  71. } else {
  72. break
  73. }
  74. }
  75. r.Gin.Response(http.StatusOK, e.SUCCESS, d)
  76. } else {
  77. logger.GetLogger().Errorf("query fail, %v", err)
  78. r.Gin.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  79. }
  80. }
  81. }
  82. // DoGetDataEx 处理数据获取, 参数obj为models结构体
  83. // Deprecated
  84. func (r *GinUtils) DoGetDataEx(modelObj interface{}) {
  85. // 如果在参数绑定阶段有错误, 则不执行数据获取
  86. if r.err != nil {
  87. return
  88. }
  89. if v, ok := modelObj.(IGetDataEx); ok {
  90. if d, err := v.GetDataEx(); err == nil {
  91. r.Gin.Response(http.StatusOK, e.SUCCESS, d)
  92. } else {
  93. logger.GetLogger().Errorf("query fail, %v", err)
  94. r.Gin.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  95. }
  96. }
  97. }
  98. // DoGetDataI 获取数据
  99. func (r *GinUtils) DoGetDataI(iObj IGetDataEx) {
  100. // 如果在参数绑定阶段有错误, 则不执行数据获取
  101. if r.err != nil {
  102. return
  103. }
  104. if d, err := iObj.GetDataEx(); err == nil {
  105. r.Gin.Response(http.StatusOK, e.SUCCESS, d)
  106. } else {
  107. logger.GetLogger().Errorf("query fail, %v", err)
  108. r.Gin.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  109. }
  110. }
  111. // DoGetDataByPage 获取数据
  112. func (r *GinUtils) DoGetDataByPage(iObj IGetDataByPage) {
  113. // 如果在参数绑定阶段有错误, 则不执行数据获取
  114. if r.err != nil {
  115. return
  116. }
  117. var p PageInfo
  118. if d, err, page, pageSize, total := iObj.GetDataByPage(); err == nil {
  119. p.Page, p.PageSize, p.Total = page, pageSize, total
  120. r.Gin.ResponseByPage(http.StatusOK, e.SUCCESS, d, p)
  121. } else {
  122. logger.GetLogger().Errorf("query fail, %v", err)
  123. r.Gin.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  124. }
  125. }
  126. // CheckParam 参数检查
  127. func (r *GinUtils) CheckParam(iObj ICheck) {
  128. if r.err != nil {
  129. return
  130. }
  131. if iObj.Check() == false {
  132. r.err = fmt.Errorf("check param err")
  133. r.Gin.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  134. }
  135. }
  136. // CheckParamF 参数检查
  137. func (r *GinUtils) CheckParamF(f func() bool) {
  138. if r.err != nil {
  139. return
  140. }
  141. if f() == false {
  142. r.err = fmt.Errorf("check param err")
  143. r.Gin.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  144. }
  145. }
  146. // NewGinUtils 生成处理GinUtils实例
  147. func NewGinUtils(c *gin.Context) *GinUtils {
  148. return &GinUtils{Gin: Gin{C: c}}
  149. }