Commit e80a94c3 by 岳巧源

add split page

parent 567e68b3
Showing with 26 additions and 10 deletions
......@@ -4,9 +4,10 @@ import "github.com/gogf/gf/v2/frame/g"
type ApiListReq struct {
g.Meta `path:"/api/list" method:"get" summary:"查询api列表"`
Page string `json:"page" d:"1" dc:"请输入第几页,不传默认第1页"`
Limit string `json:"limit" d:"10" dc:"每页限制个数"`
OffsetTime int64 `json:"offset_time" dc:"offset_time为时间戳"`
Page int `json:"page" d:"1" dc:"请输入第几页,不传默认第1页"`
Limit int `json:"limit" d:"50" dc:"每页限制个数"`
Sort bool `json:"sort" d:"false" dc:"按照调用次数从多到少排序"`
OffsetTime int64 `json:"offset_time" dc:"offset_time为时间戳"`
}
type ApiListRes struct {
......
......@@ -10,6 +10,8 @@ import (
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"math"
"sort"
"strings"
"time"
)
......@@ -29,13 +31,6 @@ func ApiList() *sApiList {
}
func (s *sApiList) GetApiList(ctx context.Context, req *v1.ApiListReq) (interface{}, gcode.Code) {
param := map[string]interface{}{
"page": req.Page,
"limit": req.Limit,
"offset_time": req.OffsetTime,
}
if param["page"] != 0 {
}
//mongodb数据源
interfaceNameInfoList, err := GetInterfaceNameListFromYapi(ctx)
if err != nil {
......@@ -45,6 +40,26 @@ func (s *sApiList) GetApiList(ctx context.Context, req *v1.ApiListReq) (interfac
for i := 0; i < len(interfaceNameInfoList); i++ {
interfaceNameInfoList[i].DayCallNum = GetNumsByInterfaceName(ctx, interfaceNameInfoList[i].URI)
}
var sortByCallNums bool = req.Sort
if sortByCallNums {
sort.Slice(interfaceNameInfoList, func(i, j int) bool {
return interfaceNameInfoList[i].DayCallNum > interfaceNameInfoList[j].DayCallNum
})
}
//分页查询
page := req.Page
limit := req.Limit
if limit > 0 && page > 0 && limit < len(interfaceNameInfoList) {
maxPageNum := int(math.Ceil(float64(len(interfaceNameInfoList)) / float64(limit)))
if page <= maxPageNum {
start := (page - 1) * limit
end := page * limit
if end > len(interfaceNameInfoList) {
end = len(interfaceNameInfoList)
}
interfaceNameInfoList = interfaceNameInfoList[start:end]
}
}
return interfaceNameInfoList, response.CodeSuccess
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment