66 lines
1.9 KiB
Go
66 lines
1.9 KiB
Go
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"` // 右边显示更多
|
|
}
|
|
|
|
// GetPaginationData 分页数据
|
|
// numPages 总页数
|
|
// currentPage 当前页
|
|
// aroundCount 当前页左右两边显示多少个分页按钮
|
|
func GetPaginationData(numPages int, currentPage int, aroundCount int) PaginationData {
|
|
// 边缘条件判断
|
|
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,
|
|
NumPages: numPages, LeftHasMore: leftHasMore, RightHasMore: rightHasMore}
|
|
}
|
|
|
|
// 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
|
|
}
|