index.vue 4.0 KB

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