You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
paopao-ce/pkg/app/pagination.go

45 lines
924 B

package app
import (
"github.com/gin-gonic/gin"
"github.com/rocboss/paopao-ce/internal/conf"
"github.com/rocboss/paopao-ce/pkg/convert"
)
func GetPage(c *gin.Context) int {
page := convert.StrTo(c.Query("page")).MustInt()
if page <= 0 {
return 1
}
return page
}
func GetPageSize(c *gin.Context) int {
pageSize := convert.StrTo(c.Query("page_size")).MustInt()
if pageSize <= 0 {
return conf.AppSetting.DefaultPageSize
}
if pageSize > conf.AppSetting.MaxPageSize {
return conf.AppSetting.MaxPageSize
}
return pageSize
}
func GetPageOffset(c *gin.Context) (offset, limit int) {
page := convert.StrTo(c.Query("page")).MustInt()
if page <= 0 {
page = 1
}
limit = convert.StrTo(c.Query("page_size")).MustInt()
if limit <= 0 {
limit = conf.AppSetting.DefaultPageSize
} else if limit > conf.AppSetting.MaxPageSize {
limit = conf.AppSetting.MaxPageSize
}
offset = (page - 1) * limit
return
}