SpontaneityProject.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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"
  19. v-for="item in spontaneityList"
  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 || item.year) }}</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="spontaneityList.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 { spontaneityList, spontaneityPagination, fetchSpontaneityLoading } = 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. await fetchData(true);
  84. isTriggered.value = false;
  85. };
  86. const getStatusName = (code: string) => {
  87. const item = projectStatusOptions.find(opt => opt.dictValue === code);
  88. return item ? item.dictLabel : code || '未知状态';
  89. };
  90. const fetchData = async (reset = false) => {
  91. loadStatus.value = 'loading';
  92. const { success, rows, total } = await projectStore.fetchSpontaneityProjects(props.queryParams, reset);
  93. if (success) {
  94. if (spontaneityList.value.length >= total || rows.length < spontaneityPagination.value.pageSize) {
  95. loadStatus.value = 'nomore';
  96. } else {
  97. loadStatus.value = 'loadmore';
  98. }
  99. } else {
  100. loadStatus.value = 'nomore';
  101. }
  102. };
  103. const loadMore = async () => {
  104. if (loadStatus.value === 'nomore' || fetchSpontaneityLoading.value) return;
  105. loadStatus.value = 'loading';
  106. const { success, rows, total } = await projectStore.loadMoreSpontaneityProjects(props.queryParams);
  107. if (success) {
  108. if (spontaneityList.value.length >= total || rows.length < spontaneityPagination.value.pageSize) {
  109. loadStatus.value = 'nomore';
  110. } else {
  111. loadStatus.value = 'loadmore';
  112. }
  113. } else {
  114. loadStatus.value = 'nomore';
  115. }
  116. };
  117. const debouncedFetchData = debounce(() => {
  118. fetchData(true);
  119. }, 500);
  120. watch(() => props.queryParams, () => {
  121. debouncedFetchData();
  122. }, { deep: true });
  123. onMounted(() => {
  124. fetchData(true);
  125. });
  126. const onRowClick = (item: any) => {
  127. emit('goDetail', { ...item, _type: 'spontaneity' });
  128. };
  129. </script>
  130. <style lang="scss" scoped>
  131. .project-list {
  132. height: 100%;
  133. display: flex;
  134. flex-direction: column;
  135. }
  136. .scroll-list {
  137. flex: 1;
  138. height: 100%;
  139. }
  140. </style>