| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- /**
- * @Author: zou.yingbin
- * @Create : 2021/1/25 9:42
- * @Modify : 2021/1/25 9:42
- */
- package app
- import (
- "mtp2_if/global/e"
- "mtp2_if/logger"
- "net/http"
- )
- // 获取数据接口
- type IGetData interface {
- GetData()([]interface{}, error)
- }
- // 数据处理接口
- type ICalc interface {
- Calc()
- }
- // 封装绑定和数据获取处理
- type GinUtils struct {
- Gin
- err error
- }
- // 处理参数绑定
- func (r *GinUtils) DoBindReq(req interface{}) {
- if r.err = r.C.ShouldBind(req); r.err != nil{
- r.Gin.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
- }
- }
- // 处理数据获取, 参数obj为models结构体
- 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)
- }
- }
- }
|