paging.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package response
  2. import "hongze/hongze_yb/utils"
  3. type PagingItem struct {
  4. IsStart bool `description:"是否首页" json:"is_start"`
  5. IsEnd bool `description:"是否最后一页" json:"is_end"`
  6. PreviousIndex int `description:"上一页页码" json:"previous_index"`
  7. NextIndex int `description:"下一页页码" json:"next_index"`
  8. CurrentIndex int `description:"当前页页码" json:"current_index"`
  9. Pages int `description:"总页数" json:"pages"`
  10. Totals int `description:"总数据量" json:"totals"`
  11. PageSize int `description:"每页数据条数" json:"page_size"`
  12. }
  13. func GetPaging(currentIndex, pageSize, total int) (item *PagingItem) {
  14. if pageSize <= 0 {
  15. pageSize = utils.PageSize20
  16. }
  17. if currentIndex <= 0 {
  18. currentIndex = 1
  19. }
  20. item = new(PagingItem)
  21. item.PageSize = pageSize
  22. item.Totals = total
  23. item.CurrentIndex = currentIndex
  24. if total <= 0 {
  25. item.IsStart = true
  26. item.IsEnd = true
  27. return
  28. }
  29. pages := utils.PageCount(total, pageSize)
  30. item.Pages = pages
  31. if pages <= 1 {
  32. item.IsStart = true
  33. item.IsEnd = true
  34. item.PreviousIndex = 1
  35. item.NextIndex = 1
  36. return
  37. }
  38. if pages == currentIndex {
  39. item.IsStart = false
  40. item.IsEnd = true
  41. item.PreviousIndex = currentIndex - 1
  42. item.NextIndex = currentIndex
  43. return
  44. }
  45. if currentIndex == 1 {
  46. item.IsStart = true
  47. item.IsEnd = false
  48. item.PreviousIndex = 1
  49. item.NextIndex = currentIndex + 1
  50. return
  51. }
  52. item.IsStart = false
  53. item.IsEnd = false
  54. item.PreviousIndex = currentIndex - 1
  55. item.NextIndex = currentIndex + 1
  56. return
  57. }