index.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <template>
  2. <view class="list-page-container">
  3. <view class="search-header">
  4. <uv-input placeholder="搜索公告标题" v-model="queryParams.noticeTitle" prefixIcon="search" clearable shape="circle"
  5. @confirm="onSearch"></uv-input>
  6. </view>
  7. <view class="list-container">
  8. <scroll-view scroll-y class="scroll-list" refresher-enabled :refresher-triggered="isTriggered"
  9. @refresherrefresh="onRefresh" @scrolltolower="loadMore">
  10. <uv-empty v-if="!list.length && loadStatus !== 'loading'" mode="list"></uv-empty>
  11. <view class="notice-card-item" v-for="item in list" :key="item.id" @click="toDetail(item.id)">
  12. <view class="notice-main">
  13. <view class="notice-title-row">
  14. <text class="tag" v-if="item.isTop === '10'">置顶</text>
  15. <text class="title">{{ item.noticeTitle }}</text>
  16. </view>
  17. <view class="notice-info">
  18. <text class="author">{{ item.createdName }}</text>
  19. <text class="time">{{ formatDate(item.noticeTime, 'YYYY-MM-DD HH:mm') }}</text>
  20. </view>
  21. </view>
  22. <uv-icon name="arrow-right" color="#ccc" size="16"></uv-icon>
  23. </view>
  24. <uv-load-more v-if="list.length > 0 || loadStatus === 'loading'" :status="loadStatus"
  25. @loadmore="loadMore"></uv-load-more>
  26. </scroll-view>
  27. </view>
  28. </view>
  29. </template>
  30. <script setup lang="ts">
  31. import { ref, watch } from 'vue';
  32. import { onLoad } from '@dcloudio/uni-app';
  33. import { debounce } from 'lodash-es';
  34. import { useSystemApi } from '@/api/system/index';
  35. import { onRouterPush } from '@/utils/router';
  36. import { formatDate } from '@/utils/date';
  37. import { DEFAULT_PAGE_SIZE } from '@/constants/index';
  38. const systemApi = useSystemApi();
  39. const queryParams = ref({
  40. noticeTitle: '',
  41. pageNum: 1,
  42. pageSize: DEFAULT_PAGE_SIZE,
  43. noticeStatus: "20"
  44. });
  45. const list = ref<any[]>([]);
  46. const loadStatus = ref<'loadmore' | 'loading' | 'nomore'>('loadmore');
  47. const isTriggered = ref(false);
  48. const fetchListData = async (reset = false) => {
  49. if (reset) {
  50. queryParams.value.pageNum = 1;
  51. }
  52. loadStatus.value = 'loading';
  53. try {
  54. const res: any = await systemApi.getNoticeList(queryParams.value);
  55. if (res.code == 200) {
  56. const records = res.data?.list || [];
  57. list.value = reset ? records : [...list.value, ...records];
  58. if (list.value.length >= (res.data?.total || 0)) {
  59. loadStatus.value = 'nomore';
  60. } else {
  61. loadStatus.value = 'loadmore';
  62. queryParams.value.pageNum += 1;
  63. }
  64. } else {
  65. loadStatus.value = 'nomore';
  66. }
  67. } catch (error) {
  68. console.error(error);
  69. loadStatus.value = 'nomore';
  70. } finally {
  71. if (reset) isTriggered.value = false;
  72. }
  73. };
  74. const onSearch = () => {
  75. fetchListData(true);
  76. };
  77. const debouncedSearch = debounce(() => {
  78. fetchListData(true);
  79. }, 500);
  80. watch(() => queryParams.value.noticeTitle, () => {
  81. debouncedSearch();
  82. });
  83. const onRefresh = async () => {
  84. isTriggered.value = true;
  85. await fetchListData(true);
  86. };
  87. const loadMore = () => {
  88. if (loadStatus.value !== 'loadmore') return;
  89. fetchListData(false);
  90. };
  91. const toDetail = (id: number) => {
  92. onRouterPush(`/pages/notice/detail?id=${id}`);
  93. };
  94. onLoad(() => {
  95. fetchListData(true);
  96. });
  97. </script>
  98. <style lang="scss" scoped>
  99. .list-page-container {
  100. height: 100vh;
  101. display: flex;
  102. flex-direction: column;
  103. background-color: #f5f7fa;
  104. }
  105. .search-header {
  106. background-color: #fff;
  107. padding: 20rpx 30rpx;
  108. position: sticky;
  109. top: 0;
  110. z-index: 10;
  111. }
  112. .list-container {
  113. flex: 1;
  114. overflow: hidden;
  115. padding: 20rpx;
  116. }
  117. .scroll-list {
  118. height: 100%;
  119. }
  120. .notice-card-item {
  121. background-color: #fff;
  122. padding: 30rpx;
  123. border-radius: 16rpx;
  124. margin-bottom: 20rpx;
  125. display: flex;
  126. align-items: center;
  127. box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.03);
  128. .notice-main {
  129. flex: 1;
  130. margin-right: 20rpx;
  131. .notice-title-row {
  132. display: flex;
  133. align-items: flex-start;
  134. margin-bottom: 16rpx;
  135. .tag {
  136. background-color: #ff4d4f;
  137. color: #fff;
  138. font-size: 20rpx;
  139. padding: 2rpx 8rpx;
  140. border-radius: 4rpx;
  141. margin-right: 12rpx;
  142. flex-shrink: 0;
  143. margin-top: 6rpx;
  144. }
  145. .title {
  146. font-size: 30rpx;
  147. font-weight: bold;
  148. color: #333;
  149. line-height: 1.4;
  150. }
  151. }
  152. .notice-info {
  153. display: flex;
  154. font-size: 24rpx;
  155. color: #999;
  156. .author {
  157. margin-right: 20rpx;
  158. }
  159. }
  160. }
  161. }
  162. </style>