reachBottom.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. module.exports = Behavior({
  2. data: {
  3. loading: false, // 加载状态标志位
  4. list: [],
  5. nullList: false,
  6. grade: '',
  7. pageNo: 1,
  8. totalSize: 0,
  9. noMoreData: false, // 无更多数据置为 false
  10. mainList: [] //前端分页的大数组
  11. },
  12. methods: {
  13. async getData(fn, args) {
  14. if (this.data.noMoreData) {
  15. return
  16. }
  17. let params = {
  18. pageNo: this.data.pageNo,
  19. pageSize: 10,
  20. ...args
  21. }
  22. // 加载状态,用于页面中控制加载动画的显示
  23. this.setData({
  24. loading: true,
  25. nullList: false
  26. })
  27. let {
  28. list,
  29. totalSize
  30. } = await fn(params).finally(() => {
  31. // 页面渲染完成后关闭加载动画字段
  32. wx.nextTick(() => {
  33. this.setData({
  34. loading: false
  35. })
  36. });
  37. })
  38. list = [...this.data.list, ...list]
  39. this.setData({
  40. nullList: list.length == 0
  41. })
  42. this.setData({
  43. pageNo: ++this.data.pageNo,
  44. list,
  45. totalSize,
  46. noMoreData: list.length >= totalSize
  47. })
  48. },
  49. // 前端分页
  50. async localGetData(fn, args) {
  51. let mainList = this.data.mainList
  52. let list = this.data.list
  53. let params = {
  54. pageNo: this.data.pageNo,
  55. pageSize: 100,
  56. ...args
  57. }
  58. if (mainList.length == 0) {
  59. // 加载状态,用于页面中控制加载动画的显示
  60. this.setData({
  61. loading: true,
  62. nullList: false
  63. })
  64. let res = await fn(params).finally(() => {
  65. // 页面渲染完成后关闭加载动画字段
  66. wx.nextTick(() => {
  67. this.setData({
  68. loading: false
  69. })
  70. });
  71. })
  72. mainList = res.list
  73. list = mainList.slice(0, this.data.pageNo * 6)
  74. if (list.length == 0) {
  75. this.setData({
  76. nullList: true
  77. })
  78. } else {
  79. this.setData({
  80. nullList: false
  81. })
  82. }
  83. this.setData({
  84. pageNo: ++this.data.pageNo,
  85. mainList,
  86. list,
  87. })
  88. } else if (mainList.length != 0 && mainList.length > list.length) {
  89. list = this.data.mainList.slice(0, this.data.pageNo * 6)
  90. this.setData({
  91. pageNo: ++this.data.pageNo,
  92. mainList,
  93. list,
  94. })
  95. }
  96. },
  97. resetData(data) {
  98. if (this.data.loading) return; // 如果接口已经在请求中,则不重置数据,防止用户持续下拉刷新
  99. this.setData({
  100. loading: false,
  101. list: [],
  102. mainList: [],
  103. pageNo: 1,
  104. totalSize: 0,
  105. noMoreData: false,
  106. })
  107. this.loadMore(data); // 调用页面中的接口请求方法
  108. },
  109. },
  110. })