mta-scroll-list.vue 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. const page = ref(1);
  31. function transformUniRequestToPromise(config) => new Promise((resolve, reject) => uni.request(...config, success(data) {resolve(data)}, fail(err) {reject(err)})
  32. async function getData(action) {
  33. try {
  34. action === 'pull-down-refresh' && (page.value = 1)
  35. const opt = {
  36. url: props.refreshUrl,
  37. method: "POST",
  38. data: {
  39. page: page.value,
  40. size: props.size,
  41. ...props.options
  42. }
  43. }
  44. const data = await transformUniRequestToPromise(opt)
  45. action === 'pull-down-refresh' && (uni.stopPullDownRefresh());
  46. Emit(action, data)
  47. } catch (err) {
  48. console.log('错误', err)
  49. uni.stopPullDownRefresh()
  50. }
  51. }
  52. onPullDownRefresh(() => getData('pull-down-refresh'))
  53. onLoad(() => uni.startPullDownRefresh())
  54. onReachBottom(() => getData('reach-buttom'))
  55. </script>
  56. <style lang="scss">
  57. </style>