/** * @Author: zou.yingbin * @Create : 2021/1/25 9:42 * @Modify : 2021/1/25 9:42 */ package app import ( "fmt" "mtp2_if/global/e" "mtp2_if/logger" "net/http" "github.com/gin-gonic/gin" ) // GetData 获取数据接口 type IGetData interface { GetData() ([]interface{}, error) } // IGetDataEx 获取数据接口 type IGetDataEx interface { GetDataEx() (interface{}, error) } // IGetDataByPage 获取数据接口 type IGetDataByPage interface { // 返回值 数据, 错误号, 页码, 页大小, 总记录数 GetDataByPage() (interface{}, error, int, int, int) } // ICalc 数据处理接口 type ICalc interface { Calc() } // Check 参数检查接口 type ICheck interface { Check() bool } // GinUtils 封装绑定和数据获取处理 type GinUtils struct { Gin err error } // DoBindReq 处理参数绑定 func (r *GinUtils) DoBindReq(req interface{}) { if r.err != nil { return } if r.err = r.C.ShouldBind(req); r.err != nil { r.Gin.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil) } } // DoBindJsonReq 处理Json Body参数绑定 func (r *GinUtils) DoBindJsonReq(req interface{}) { if r.err != nil { return } if r.err = r.C.ShouldBindJSON(req); r.err != nil { r.Gin.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil) } } // DoGetData 处理数据获取, 参数obj为models结构体 // Deprecated func (r *GinUtils) DoGetData(obj interface{}) { // 如果在参数绑定阶段有错误, 则不执行数据获取 if r.err != nil { return } if v, ok := obj.(IGetData); ok { if d, err := v.GetData(); err == nil { // 执行数据处理 for i := range d { if val, ok := d[i].(ICalc); ok { val.Calc() } else { break } } r.Gin.Response(http.StatusOK, e.SUCCESS, d) } else { logger.GetLogger().Errorf("query fail, %v", err) r.Gin.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil) } } } // DoGetDataEx 处理数据获取, 参数obj为models结构体 // Deprecated func (r *GinUtils) DoGetDataEx(modelObj interface{}) { // 如果在参数绑定阶段有错误, 则不执行数据获取 if r.err != nil { return } if v, ok := modelObj.(IGetDataEx); ok { if d, err := v.GetDataEx(); err == nil { r.Gin.Response(http.StatusOK, e.SUCCESS, d) } else { logger.GetLogger().Errorf("query fail, %v", err) r.Gin.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil) } } } // DoGetDataI 获取数据 func (r *GinUtils) DoGetDataI(iObj IGetDataEx) { // 如果在参数绑定阶段有错误, 则不执行数据获取 if r.err != nil { return } if d, err := iObj.GetDataEx(); err == nil { r.Gin.Response(http.StatusOK, e.SUCCESS, d) } else { logger.GetLogger().Errorf("query fail, %v", err) r.Gin.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil) } } // DoGetDataByPage 获取数据 func (r *GinUtils) DoGetDataByPage(iObj IGetDataByPage) { // 如果在参数绑定阶段有错误, 则不执行数据获取 if r.err != nil { return } var p PageInfo if d, err, page, pageSize, total := iObj.GetDataByPage(); err == nil { p.Page, p.PageSize, p.Total = page, pageSize, total r.Gin.ResponseByPage(http.StatusOK, e.SUCCESS, d, p) } else { logger.GetLogger().Errorf("query fail, %v", err) r.Gin.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil) } } // CheckParam 参数检查 func (r *GinUtils) CheckParam(iObj ICheck) { if r.err != nil { return } if iObj.Check() == false { r.err = fmt.Errorf("check param err") r.Gin.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil) } } // CheckParamF 参数检查 func (r *GinUtils) CheckParamF(f func() bool) { if r.err != nil { return } if f() == false { r.err = fmt.Errorf("check param err") r.Gin.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil) } } // NewGinUtils 生成处理GinUtils实例 func NewGinUtils(c *gin.Context) *GinUtils { return &GinUtils{Gin: Gin{C: c}} }