SpontaneityProject.vue 4.9 KB

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