paging.go 1.5 KB

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