提交 5645b76b authored 作者: mooncake9527's avatar mooncake9527

change pagination

上级 5fa39fc9
......@@ -9,6 +9,7 @@ import (
"time"
"gitlab.wanzhuangkj.com/tush/xpkg/pkg/httpcli/entity"
"gitlab.wanzhuangkj.com/tush/xpkg/pkg/sgorm/query"
"github.com/gin-gonic/gin"
......@@ -203,24 +204,26 @@ func Success(c *gin.Context, data ...interface{}) {
// SuccessWithPage return success
func SuccessWithPage[T any](c *gin.Context, list []*T, total int64) {
// currPage := 1
// pageSize := 10
// if req, ok := c.Get("reqbody"); ok {
// if pagination, ok := req.(query.IPagination); ok {
// currPage = pagination.GetPageIndex()
// pageSize = pagination.GetPageSize()
// }
// }
// totalPage := total / int64(pageSize)
// if total%int64(pageSize) > 0 {
// totalPage = totalPage + 1
// }
currPage := 1
pageSize := 10
if req, ok := c.Get(ctxUtils.KeyPagination); ok {
if pagination, ok := req.(query.Pagination); ok {
currPage = pagination.PageIndex
if pagination.PageSize > 0 {
pageSize = pagination.PageSize
}
}
}
totalPage := total / int64(pageSize)
if total%int64(pageSize) > 0 {
totalPage = totalPage + 1
}
c.Set(ctxUtils.KeyRspCode, 1)
if list == nil {
list = []*T{}
}
respJSONWith200(c, errcode.Success.Code(), errcode.Success.Msg(), gin.H{
"list": list, "total": total,
"list": list, "total": total, "curPage": currPage, "totalPage": totalPage,
})
}
......
......@@ -36,6 +36,9 @@ const (
KeyToken = "token"
KeyCost = "X-Request-Cost"
KeyRspBodyMax = "rsp-body-max"
KeyApiType = "apiType" // enums: page
KeyPagination = "pagination"
)
var (
......
......@@ -270,6 +270,14 @@ type IParams interface {
}
func (x *ODao[T]) Page(ctx context.Context, where IParams) (rs []*T, total int64, err error) {
if ginCtx, err := ctxUtils.GetGinCtx(ctx); err == nil && ginCtx != nil {
ginCtx.Set(ctxUtils.KeyApiType, "page")
pagination := query.Pagination{
PageIndex: where.GetPageIndex(),
PageSize: where.GetPageSize(),
}
ginCtx.Set(ctxUtils.KeyPagination, pagination)
}
condition := x.DB().WithContext(ctx).Model(new(T)).Scopes(x.MakeCondition(where))
if where.Unscoped() {
condition = condition.Unscoped()
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论