VerticalProject.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <template>
  2. <view class="project-list">
  3. <scroll-view
  4. scroll-y
  5. class="scroll-list"
  6. :scroll-top="scrollTopValue"
  7. scroll-with-animation
  8. refresher-enabled
  9. :refresher-triggered="isTriggered"
  10. @refresherrefresh="onRefresh"
  11. @scroll="onScroll"
  12. @scrolltolower="loadMore"
  13. :show-scrollbar="false"
  14. >
  15. <!-- uv-empty 用于查无数据时的展示 -->
  16. <uv-empty v-if="!verticalList.length && loadStatus !== 'loading'" mode="list"></uv-empty>
  17. <view
  18. class="common-list-card"
  19. v-for="item in verticalList"
  20. :key="item.id"
  21. @click="onRowClick(item)"
  22. >
  23. <view class="card-header">
  24. <text class="title">{{ item.projectName || item.name || '未命名' }}</text>
  25. <text class="status-tag" :class="'status-' + item.projectStatus">{{ getStatusName(item.projectStatus) }}</text>
  26. </view>
  27. <view class="card-body">
  28. <view class="info-item">
  29. <text class="label">立项日期:</text>
  30. <view class="value" style="display: flex; justify-content: space-between;">
  31. <text>{{ formatDate(item.approvalDate || item.setupDate || item.date) }}</text>
  32. <text style="color: #585858;">{{ item.projectLeaderName || item.managerName || item.manager || '-' }}</text>
  33. </view>
  34. </view>
  35. </view>
  36. </view>
  37. <!-- 加载更多 -->
  38. <uv-load-more v-if="verticalList.length > 0 || loadStatus === 'loading'" :status="loadStatus" @loadmore="loadMore"></uv-load-more>
  39. </scroll-view>
  40. <!-- 返回顶部按钮 -->
  41. <uv-back-top
  42. :scrollTop="currentScrollTop"
  43. :bottom="100"
  44. :right="30"
  45. @click="backToTop"
  46. ></uv-back-top>
  47. </view>
  48. </template>
  49. <script setup lang="ts">
  50. import { ref, watch, onMounted, nextTick } from 'vue';
  51. import { storeToRefs } from 'pinia';
  52. import { debounce } from 'lodash-es';
  53. import { useProjectStore } from '@/store/modules/project';
  54. import { formatDate } from '@/utils/date';
  55. import { projectStatusOptions } from '@/constants/index';
  56. const props = defineProps({
  57. queryParams: {
  58. type: Object,
  59. default: () => ({})
  60. }
  61. });
  62. const emit = defineEmits(['goDetail']);
  63. const projectStore = useProjectStore();
  64. const { verticalList, verticalPagination, fetchVerticalLoading } = storeToRefs(projectStore);
  65. const loadStatus = ref('loadmore'); // loadmore, loading, nomore
  66. // 滚动距离状态
  67. const scrollTopValue = ref(0);
  68. const currentScrollTop = ref(0); // 监听当前滚动的准确高度用于渲染显隐
  69. const onScroll = (e: any) => {
  70. currentScrollTop.value = e.detail.scrollTop;
  71. };
  72. const backToTop = () => {
  73. // scroll-view 需要这种 hack 方式才能完美由非0回滚到0
  74. scrollTopValue.value = currentScrollTop.value;
  75. nextTick(() => {
  76. scrollTopValue.value = 0;
  77. });
  78. };
  79. // ----------------- 下拉刷新功能 -----------------
  80. const isTriggered = ref(false);
  81. const onRefresh = async () => {
  82. isTriggered.value = true;
  83. // fetchData 内部会重置页码并重新请求数据
  84. await fetchData(true);
  85. isTriggered.value = false;
  86. };
  87. const getStatusName = (code: string) => {
  88. const item = projectStatusOptions.find(opt => opt.dictValue === code);
  89. return item ? item.dictLabel : code || '未知状态';
  90. };
  91. const fetchData = async (reset = false) => {
  92. loadStatus.value = 'loading';
  93. const { success, rows, total } = await projectStore.fetchVerticalProjects(props.queryParams, reset);
  94. if (success) {
  95. if (verticalList.value.length >= total || rows.length < verticalPagination.value.pageSize) {
  96. loadStatus.value = 'nomore';
  97. } else {
  98. loadStatus.value = 'loadmore';
  99. }
  100. } else {
  101. loadStatus.value = 'nomore';
  102. }
  103. };
  104. const loadMore = async () => {
  105. if (loadStatus.value === 'nomore' || fetchVerticalLoading.value) return;
  106. loadStatus.value = 'loading';
  107. const { success, rows, total } = await projectStore.loadMoreVerticalProjects(props.queryParams);
  108. if (success) {
  109. if (verticalList.value.length >= total || rows.length < verticalPagination.value.pageSize) {
  110. loadStatus.value = 'nomore';
  111. } else {
  112. loadStatus.value = 'loadmore';
  113. }
  114. } else {
  115. loadStatus.value = 'nomore';
  116. }
  117. };
  118. const debouncedFetchData = debounce(() => {
  119. fetchData(true);
  120. }, 500);
  121. watch(() => props.queryParams, () => {
  122. debouncedFetchData();
  123. }, { deep: true });
  124. onMounted(() => {
  125. fetchData(true);
  126. });
  127. const onRowClick = (item: any) => {
  128. emit('goDetail', { ...item, _type: 'vertical' });
  129. };
  130. </script>
  131. <style lang="scss" scoped>
  132. .project-list {
  133. height: 100%;
  134. display: flex;
  135. flex-direction: column;
  136. }
  137. .scroll-list {
  138. flex: 1;
  139. height: 100%;
  140. }
  141. </style>