diff --git a/internal/handler/index.go b/internal/handler/index.go index 7c16175..367197f 100644 --- a/internal/handler/index.go +++ b/internal/handler/index.go @@ -25,6 +25,15 @@ func (s HandlerService) GetList(c *gin.Context) (data interface{}, err error) { repo := service.NewEdgeService(s.Logger) var req proto.ListRequest err = c.ShouldBindJSON(&req) + if req.Size < 1 { + req.Size = 20 + } + if req.Size > 1000 { + req.Size = 1000 + } + if req.Page < 1 { + req.Page = 1 + } if err != nil { return nil, e.NewValidErr(err) } diff --git a/internal/proto/request.go b/internal/proto/request.go index aea24c3..c89358d 100644 --- a/internal/proto/request.go +++ b/internal/proto/request.go @@ -3,6 +3,12 @@ package proto type ListRequest struct { Path string `json:"path"` IsIncludeSubdirectories bool `json:"isIncludeSubdirectories"` + BasePageList +} + +type BasePageList struct { + Page int64 `json:"page,omitempty" form:"page"` + Size int64 `json:"pageSize,omitempty" form:"pageSize"` } type LabelRequest struct { diff --git a/internal/service/index.go b/internal/service/index.go index 1495e42..117d8fb 100644 --- a/internal/service/index.go +++ b/internal/service/index.go @@ -14,6 +14,7 @@ import ( "net/http" "os" "path" + "sort" ) type PagingStruct struct { @@ -68,7 +69,10 @@ func (rp *repo) GetList(ctx context.Context, req proto.ListRequest) (rsp *proto. if req.IsIncludeSubdirectories { } else { - var files []os.DirEntry + var ( + files []os.DirEntry + count int + ) files, err = os.ReadDir(req.Path) if err != nil { goto ReturnPoint @@ -88,10 +92,23 @@ func (rp *repo) GetList(ctx context.Context, req proto.ListRequest) (rsp *proto. } } + // 从小到大排序(稳定排序) + sort.SliceStable(list, func(i, j int) bool { + if list[i].Name < list[j].Name { + return true + } + return false + }) + count = len(list) + if len(list) >= int(req.Page*req.Size) { + list = list[int((req.Page-1)*req.Size):int(req.Page*req.Size)] + } else { + list = list[int((req.Page-1)*req.Size):] + } rsp.Code = http.StatusOK rsp.Status = http.StatusText(http.StatusOK) rsp.Message = "成功" - rsp.Data = list + rsp = FillPaging(int64(count), req.Page, req.Size, list, rsp) rsp.Err = err return rsp, err }