index.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <template>
  2. <view class="u-page">
  3. <view class="u-demo-block" style="padding:10px;height:100vh">
  4. <view>
  5. <search-bar @toFilterSearch="filterSearch"></search-bar>
  6. </view>
  7. <view v-if="list.length > 0">
  8. <u-list @scrolltolower="scrollSearch">
  9. <u-list-item v-for="i in list" :key="i._id">
  10. <item :data="i" @toRefresh="refresh"></item>
  11. </u-list-item>
  12. <view v-if="noData" style="margin-bottom: 20px;">
  13. <u-empty iconSize="0" text="没数据啦!点击按钮再次加载!">
  14. <up-row>
  15. <up-col>
  16. <up-button type="primary" icon="reload" shape="circle" @click="trySearch()"></up-button>
  17. </up-col>
  18. </up-row>
  19. </u-empty>
  20. </view>
  21. </u-list>
  22. </view>
  23. <view v-else>
  24. <u-empty mode="list">
  25. <up-row>
  26. <up-col>
  27. <up-button type="primary" icon="reload" shape="circle" @click="trySearch()"></up-button>
  28. </up-col>
  29. </up-row>
  30. </u-empty>
  31. </view>
  32. </view>
  33. <view style="position: fixed;right:5vw;bottom:10vh;z-index:9999">
  34. <up-row>
  35. <up-col>
  36. <up-button type="primary" icon="plus" shape="circle" @click="toAdd()"></up-button>
  37. </up-col>
  38. </up-row>
  39. </view>
  40. </view>
  41. </template>
  42. <script setup>
  43. import { getCurrentInstance, ref, onMounted } from 'vue';
  44. import item from './item.vue'
  45. import searchBar from './searchBar.vue'
  46. let skip = ref(0);
  47. let limit = ref(5);
  48. let list = ref([]);
  49. let total = ref(0);
  50. let noData = ref(false);
  51. let searchCondition = ref({});
  52. const search = async (skip = 0, limit = 5, add = true) => {
  53. uni.showLoading({
  54. title: '加载中...',
  55. mask: true,
  56. success: async () => {
  57. try {
  58. const query = { skip, limit, ...searchCondition.value };
  59. const result = await $api('/perfume', 'GET', query);
  60. if (result.errcode == 0) {
  61. if (add) list.value.push(...result.data);
  62. else list.value = result.data;
  63. total.value = result.total
  64. if (list.value.length >= result.total) noData.value = true
  65. }
  66. uni.hideLoading()
  67. } catch (error) {
  68. console.log(error)
  69. uni.hideLoading()
  70. }
  71. },
  72. fail: (e) => console.log(e)
  73. })
  74. }
  75. const refresh = () => {
  76. skip.value = 0
  77. search()
  78. }
  79. onMounted(async () => {
  80. await search()
  81. })
  82. const $api = getCurrentInstance()?.appContext.config.globalProperties.$api;
  83. const toAdd = () => {
  84. uni.navigateTo({
  85. url: `/pages/list/edit?type=add`,
  86. })
  87. }
  88. const scrollSearch = () => {
  89. if (noData.value) return
  90. skip.value = skip.value + limit.value;
  91. search(skip.value, limit.value)
  92. }
  93. const trySearch = async () => {
  94. const res = await $api('/perfume/count', 'GET')
  95. if (res.data > total.value) {
  96. skip.value = total.value;
  97. noData.value = false;
  98. search(skip.value, limit.value)
  99. }
  100. }
  101. const filterSearch = (data) => {
  102. skip.value = 0;
  103. total.value = 0;
  104. noData.value = false;
  105. searchCondition.value = data;
  106. search(skip.value, limit.value, false);
  107. }
  108. </script>
  109. <style></style>