index.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <template>
  2. <mescroll-uni @init="mescrollInit" @down="downCallback" @up="upCallback" :height="height||windowHeight+'px'"
  3. :up="{textNoMore:'-- 没有更多了 --'}">
  4. <slot></slot>
  5. </mescroll-uni>
  6. </template>
  7. <script setup>
  8. import {
  9. computed,
  10. onMounted,
  11. ref,
  12. watch
  13. } from 'vue';
  14. import {
  15. onShow
  16. } from '@dcloudio/uni-app';
  17. import useMescroll from "@/uni_modules/mescroll-uni/hooks/useMescroll.js";
  18. import {
  19. store
  20. } from "@/store/index.js"
  21. // 调用mescroll的hook (注: mescroll-uni不用传onPageScroll,onReachBottom, 而mescroll-body必传)
  22. const {
  23. mescrollInit,
  24. downCallback,
  25. getMescroll
  26. } = useMescroll()
  27. const windowHeight = computed(() => {
  28. return store.clientHeight - props.diffHeight
  29. })
  30. const emit = defineEmits(['updateList', 'update:total'])
  31. const props = defineProps({
  32. request: {
  33. type: Function
  34. },
  35. requestStr: {
  36. type: String
  37. },
  38. isRefresh: {
  39. type: [Number, Boolean],
  40. default: 0
  41. },
  42. height: {
  43. type: [String, Number]
  44. },
  45. total: {
  46. type: [Number],
  47. default: 0
  48. },
  49. otherParams: {
  50. type: Object,
  51. default: () => ({})
  52. },
  53. diffHeight: {
  54. type: Number,
  55. default: 0
  56. },
  57. immediate: {
  58. type: Boolean,
  59. default: false
  60. }
  61. })
  62. const list = ref([])
  63. const getList = (mescroll) => {
  64. return new Promise((resolve, reject) => {
  65. let params = {
  66. pageNum: mescroll.num,
  67. pageSize: mescroll.size,
  68. ...props.otherParams
  69. }
  70. uni.$u.http.get(props.requestStr, {
  71. params
  72. }).then(res => {
  73. emit('update:total', res.total)
  74. resolve(res)
  75. })
  76. })
  77. }
  78. // 上拉加载的回调: 其中num:当前页 从1开始, size:每页数据条数,默认10
  79. const upCallback = (mescroll) => {
  80. if (!props.requestStr) return;
  81. getList(mescroll).then(res => {
  82. const curPageData = res.rows || res.data || [] // 当前页数据
  83. if (mescroll.num == 1) list.value = []; // 第一页需手动制空列表
  84. list.value = list.value.concat(curPageData); //追加新数据
  85. emit('updateList', list.value)
  86. //联网成功的回调,隐藏下拉刷新和上拉加载的状态;
  87. //mescroll会根据传的参数,自动判断列表如果无任何数据,则提示空;列表无下一页数据,则提示无更多数据;
  88. //方法一(推荐): 后台接口有返回列表的总页数 totalPage
  89. //mescroll.endByPage(curPageData.length, totalPage); //必传参数(当前页的数据个数, 总页数)
  90. //方法二(推荐): 后台接口有返回列表的总数据量 totalSize
  91. mescroll.endBySize(curPageData.length, res.total); //必传参数(当前页的数据个数, 总数据量)
  92. //方法三(推荐): 您有其他方式知道是否有下一页 hasNext
  93. //mescroll.endSuccess(curPageData.length, hasNext); //必传参数(当前页的数据个数, 是否有下一页true/false)
  94. //方法四 (不推荐),会存在一个小问题:比如列表共有20条数据,每页加载10条,共2页.如果只根据当前页的数据个数判断,则需翻到第三页才会知道无更多数据.
  95. // mescroll.endSuccess(curPageData.length); // 请求成功, 结束加载
  96. }).catch(() => {
  97. mescroll.endErr(); // 请求失败, 结束加载
  98. })
  99. }
  100. //重置列表
  101. const resetUpScroll = () => {
  102. emit('updateList', [])
  103. setTimeout(() => {
  104. getMescroll().resetUpScroll()
  105. }, 100)
  106. }
  107. const scroll = (e) => {
  108. emit('scroll', e)
  109. }
  110. defineExpose({
  111. resetUpScroll
  112. })
  113. </script>
  114. <style lang="scss" scoped>
  115. .item {
  116. line-height: 150rpx;
  117. border-bottom: 1px solid #ccc;
  118. }
  119. </style>