picgo/corelib/page.go

67 lines
2.0 KiB
Go
Raw Permalink Normal View History

2024-08-06 20:37:34 +08:00
package corelib
type PaginationData struct {
LeftPages []int `json:"left_pages"` // 当前页左边显示
RightPages []int `json:"right_pages"` // 当前页右边
CurrentPage int `json:"current_page"` // 当前页
NumPages int `json:"num_pages"` // 总页数
LeftHasMore bool `json:"left_has_more"` // 左边显示更多
RightHasMore bool `json:"right_has_more"` // 右边显示更多
2024-08-09 20:30:35 +08:00
Total int64 `json:"total"` // 数据总量
2024-08-06 20:37:34 +08:00
}
// GetPaginationData 分页数据
// numPages 总页数
// currentPage 当前页
// aroundCount 当前页左右两边显示多少个分页按钮
2024-08-09 20:30:35 +08:00
func GetPaginationData(numPages int, currentPage int, aroundCount int, total int64) PaginationData {
2024-08-06 20:37:34 +08:00
// 边缘条件判断
if currentPage > numPages {
currentPage = numPages
}
if currentPage < 1 {
currentPage = 1
}
// 当前页向左减去around_count后剩下的页数
leftAroundPages := currentPage - aroundCount
// 当前页向右减去around_count后剩下的页数
rightAroundPages := numPages - currentPage - aroundCount + 1
// 左边还有更多页
leftHasMore := false
// 右边还有更多页
rightHasMore := false
// 当前页左边显示页码
var leftPages []int
// 当前页右边显示页码
var rightPages []int
if leftAroundPages <= 1 || currentPage-aroundCount-1 == 1 {
leftPages = rangeList(1, currentPage-1)
} else {
leftHasMore = true
leftPages = rangeList(leftAroundPages, currentPage-1)
}
if rightAroundPages <= 1 || currentPage+aroundCount+1 == numPages {
rightPages = rangeList(currentPage+1, numPages)
} else {
rightHasMore = true
rightPages = rangeList(currentPage+1, currentPage+aroundCount)
}
return PaginationData{
LeftPages: leftPages, RightPages: rightPages, CurrentPage: currentPage,
2024-08-09 20:30:35 +08:00
NumPages: numPages, LeftHasMore: leftHasMore, RightHasMore: rightHasMore, Total: total}
2024-08-06 20:37:34 +08:00
}
// LeftPages RightPages
func rangeList(start int, end int) []int {
var list []int
for i := start; i <= end; i++ {
list = append(list, i)
}
if list == nil {
return []int{}
}
return list
}