list copy.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <template>
  2. <view>
  3. <!-- @scrolltolower="scrolltolower" -->
  4. <up-list style="height:auto" id="item_height" class="wrap_list">
  5. <up-list-item v-for="(item, index) in lists" :key="index">
  6. <slot name="item" :item="item" :index="index"></slot>
  7. </up-list-item>
  8. </up-list>
  9. <view class="null_warp" v-if="!!status && !lists.length">
  10. <slot name="null">
  11. <view class="null_text">{{ t(nullText || '无数据') }}</view>
  12. </slot>
  13. </view>
  14. <template v-if="!!status && lists.length">
  15. <view class="load_more">
  16. <u-loadmore :status="status" :loading-text="t(loadingText)" :loadmore-text="t(loadmoreText)"
  17. :nomore-text="t(nomoreText)" />
  18. </view>
  19. </template>
  20. </view>
  21. </template>
  22. <script setup>
  23. import { useCacheStore } from '@/store';
  24. import { query, $post, systemInfo } from '@/utils';
  25. import { ref, nextTick } from "vue";
  26. import { t } from "@/locale";
  27. const useCache = useCacheStore();
  28. const props = defineProps({
  29. topHeight: {
  30. type: Number,
  31. default: 0
  32. },
  33. url: {
  34. type: String,
  35. default: ''
  36. },
  37. pageSize: {
  38. type: Number,
  39. default: 20
  40. },
  41. defaultParams: {
  42. type: Object,
  43. default: () => { }
  44. },
  45. nullText: String,
  46. loadingText: {
  47. type: String,
  48. default: '正在加载中'
  49. },
  50. loadmoreText: {
  51. type: String,
  52. default: '加载更多'
  53. },
  54. nomoreText: {
  55. type: String,
  56. default: '没有更多了'
  57. },
  58. getIndex: {
  59. type: Number,
  60. default: 0
  61. },
  62. activeIndex: {
  63. type: Number,
  64. default: 0
  65. },
  66. isCache: {
  67. type: Boolean,
  68. default: false
  69. }
  70. })
  71. const emit = defineEmits(['datas'])
  72. const lists = ref([]);
  73. const page = ref(1);
  74. const totals = ref(0);
  75. const status = ref('nomore');
  76. const noData = ref(false);
  77. const height = ref(0);
  78. const scrolltolower = () => {
  79. if (!noData.value) {
  80. let num = page.value;
  81. num += 1;
  82. page.value = num;
  83. getData();
  84. }
  85. }
  86. const getListKey = () => `${props.url}_${JSON.stringify(props.defaultParams)}`;
  87. const getData = async (force = false) => {
  88. try {
  89. if (props.isCache) {
  90. const key = getListKey()
  91. const list = await useCache.getListPageData({
  92. key,
  93. page: page.value,
  94. pageSize: props.pageSize,
  95. apiFn: props.url,
  96. params: props.defaultParams,
  97. force
  98. })
  99. if (list) {
  100. lists.value = list;
  101. emit('datas', lists.value)
  102. nextTick(() => {
  103. getHeight();
  104. })
  105. return;
  106. }
  107. }
  108. status.value = 'loading';
  109. const { data: { data: { item: data, total_results: count } } } = await $post(props.url, { pageSize: props.pageSize, page: page.value, ...props.defaultParams });
  110. status.value = 'nomore';
  111. if (data.length < props.pageSize) {
  112. noData.value = true
  113. } else {
  114. noData.value = false
  115. }
  116. totals.value = count - 0;
  117. if (page.value > 1) {
  118. const arrList = lists.value;
  119. lists.value = [...arrList, ...data]
  120. emit('datas', lists.value)
  121. nextTick(() => {
  122. getHeight();
  123. })
  124. return;
  125. }
  126. lists.value = data;
  127. emit('datas', lists.value)
  128. nextTick(() => {
  129. getHeight();
  130. })
  131. } catch (error) {
  132. console.error(error, 'getData');
  133. }
  134. }
  135. const getHeight = async (name = '#item_height') => {
  136. const { height: heights } = await query(name, this);
  137. nextTick(() => {
  138. height.value = heights
  139. const { windowHeight, statusBarHeight } = systemInfo();
  140. let contHeight = windowHeight - 44 - props.topHeight;
  141. if (height < contHeight) {
  142. scrolltolower();
  143. }
  144. })
  145. return height
  146. }
  147. const handleRefresh = (flag = false) => {
  148. page.value = 1;
  149. status.value = 'nomore';
  150. noData.value = false;
  151. if (!flag) {
  152. lists.value = []
  153. }
  154. nextTick(() => {
  155. getData();
  156. })
  157. }
  158. const setList = (_list) => {
  159. nextTick(() => {
  160. lists.value = _list;
  161. emit("datas", lists.value)
  162. nextTick(() => {
  163. getHeight();
  164. })
  165. })
  166. }
  167. defineExpose({
  168. handleRefresh,
  169. setList,
  170. getData,
  171. scrolltolower
  172. })
  173. </script>
  174. <style lang="less" scoped>
  175. @import url('@/style.less');
  176. :deep(.u-list) {
  177. height: 100% !important;
  178. }
  179. /deep/ .uni-scroll-view {
  180. overflow: unset !important;
  181. }
  182. .null_warp {
  183. .null_text {
  184. min-height: 100rpx;
  185. .flex_center();
  186. font-size: 28rpx;
  187. color: #333;
  188. }
  189. }
  190. .load_more {
  191. padding: 16rpx 0;
  192. }
  193. </style>