reachBottom.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. module.exports = Behavior({
  2. data: {
  3. loading: false, // 加载状态标志位
  4. list: [],
  5. grade: '',
  6. pageNo: 1,
  7. totalSize: 0,
  8. noMoreData: false, // 无更多数据置为 false
  9. },
  10. properties: {
  11. pageSize: {
  12. type: Number,
  13. value: 10
  14. }
  15. },
  16. methods: {
  17. async getData(fn, args) {
  18. if (this.data.noMoreData) {
  19. return
  20. }
  21. let params = {
  22. pageNo: this.data.pageNo,
  23. pageSize: this.properties.pageSize,
  24. ...args
  25. }
  26. // 加载状态,用于页面中控制加载动画的显示
  27. this.setData({
  28. loading: true
  29. })
  30. let {
  31. list,
  32. totalSize
  33. } = await fn(params).finally(() => {
  34. // 页面渲染完成后关闭加载动画字段
  35. wx.nextTick(() => {
  36. this.setData({
  37. loading: false
  38. })
  39. });
  40. })
  41. list = [...this.data.list, ...list]
  42. console.log(list,
  43. totalSize);
  44. this.setData({
  45. pageNo: ++this.data.pageNo,
  46. list,
  47. totalSize,
  48. noMoreData: list.length >= totalSize
  49. })
  50. },
  51. resetData() {
  52. console.log("触发resetData");
  53. if (this.data.loading) return; // 如果接口已经在请求中,则不重置数据,防止用户持续下拉刷新
  54. this.setData({
  55. loading: false,
  56. list: [],
  57. pageNo: 1,
  58. totalSize: 0,
  59. noMoreData: false,
  60. })
  61. this.loadMore(); // 调用页面中的接口请求方法
  62. },
  63. },
  64. })