mta-scroll-list.vue 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <template>
  2. <view class="mta-refresh-list">
  3. <slot></slot>
  4. </view>
  5. </template>
  6. <script setup>
  7. import {
  8. onReachBottom,
  9. onLoad,
  10. onPullDownRefresh
  11. } from "@dcloudio/uni-app"
  12. import {
  13. ref
  14. } from "vue";
  15. const props = defineProps({
  16. refreshUrl: {
  17. type: String,
  18. default: '/api/admin/config'
  19. },
  20. options: {
  21. type: Object,
  22. default: () => {}
  23. },
  24. size: {
  25. type: Number,
  26. default: 10
  27. }
  28. })
  29. const Emits = defineEmits(['pull-down-refresh', 'reach-buttom']);
  30. /**
  31. * 页码
  32. */
  33. const page = ref(1);
  34. /**
  35. * @summary 将请求转换为promise
  36. */
  37. const transformUniRequestToPromise = (config) =>
  38. new Promise((resolve, reject) => {
  39. uni.request({
  40. ...config,
  41. success(data) {
  42. resolve(data);
  43. },
  44. fail(err) {
  45. reject(err);
  46. },
  47. });
  48. });
  49. /**
  50. * @summary 更新数据
  51. * @param {String} action 'pull-down-refresh' | 'reach-buttom'
  52. */
  53. async function getData(action) {
  54. try {
  55. // 刷新页码重置
  56. action === 'pull-down-refresh' && (page.value = 1);
  57. // 触底页码 加1
  58. action === 'reach-buttom' && (page.value++);
  59. const opt = {
  60. url: props.refreshUrl,
  61. method: "POST",
  62. data: {
  63. page: page.value,
  64. size: props.size,
  65. ...props.options
  66. }
  67. }
  68. const data = await transformUniRequestToPromise(opt)
  69. action === 'pull-down-refresh' && (uni.stopPullDownRefresh());
  70. Emit(action, data)
  71. } catch (err) {
  72. console.log('错误', err)
  73. uni.stopPullDownRefresh()
  74. }
  75. }
  76. /**
  77. * @summary 下拉刷新
  78. */
  79. onPullDownRefresh(() => getData('pull-down-refresh'));
  80. /**
  81. * @summary 加载完成 首次进行下拉刷新
  82. */
  83. onLoad(() => uni.startPullDownRefresh());
  84. /**
  85. * @summary 触底更新
  86. */
  87. onReachBottom(() => getData('reach-buttom'));
  88. </script>
  89. <style lang="scss">
  90. </style>