List.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <template>
  2. <view class="news-list-container">
  3. <!-- 列表 -->
  4. <view v-if="list.length > 0" class="list">
  5. <view v-for="item in list" :key="item.id" class="news-item" @click="handleItemClick(item)">
  6. <image v-if="item.coverImage" class="cover" :src="Config.Host80 + item.coverImage" mode="aspectFill" />
  7. <view class="info">
  8. <view class="title">{{ item.title }}</view>
  9. <view class="subtitle">{{ item.subTitle }}</view>
  10. <view class="meta">
  11. <text class="date">{{ formatDate(item.deliveryTime) }}</text>
  12. <!-- <text v-if="item.tags && item.tags.length" class="tag">
  13. {{ getTagName(item.tags[0].tag) }}
  14. </text> -->
  15. </view>
  16. </view>
  17. </view>
  18. </view>
  19. <!-- 空状态 -->
  20. <view v-else-if="!loading && finished" class="empty">
  21. <cwg-empty-state />
  22. </view>
  23. <view class="table-loading-mask">
  24. <uni-loading v-if="loading" />
  25. </view>
  26. <!-- 加载更多状态 -->
  27. <view v-if="loadingMore" class="load-more">
  28. <text v-t11="'common.loading'" />
  29. </view>
  30. <view v-else-if="finished && list.length > 0" class="load-more">
  31. <text v-t11="'common.noMore'" />
  32. </view>
  33. </view>
  34. </template>
  35. <script setup>
  36. import { ref, watch, computed } from 'vue'
  37. import { useI18n } from 'vue-i18n'
  38. import Config from '@/config/index'
  39. const { locale } = useI18n()
  40. const props = defineProps({
  41. fetchData: { type: Function, required: true },
  42. queryParams: { type: Object, default: () => ({}) }, // 修正:应为对象
  43. pageSize: { type: Number, default: 10 },
  44. type: { type: Number, default: 0 },
  45. immediate: { type: Boolean, default: true },
  46. })
  47. const list = ref([])
  48. const page = ref(1)
  49. const loading = ref(false)
  50. const loadingMore = ref(false)
  51. const finished = ref(false)
  52. const total = ref(0)
  53. const formatDate = (dateStr) => {
  54. if (!dateStr) return ''
  55. return dateStr.slice(0, 10).replace('T', ' ')
  56. }
  57. const load = async () => {
  58. list.value = []
  59. if (loading.value) return
  60. loading.value = true
  61. finished.value = false
  62. page.value = 1
  63. try {
  64. const res = await props.fetchData({
  65. lang: locale.value == 'vn' ? 'vi' : locale.value,
  66. page: { current: page.value, row: props.queryParams?.pageSize || props.pageSize },
  67. ...props.queryParams
  68. })
  69. if (res.code === 200) {
  70. list.value = res.data || []
  71. total.value = res.page?.rowTotal || 0
  72. finished.value = list.value.length >= total.value
  73. } else {
  74. throw new Error(res.msg || '请求失败')
  75. }
  76. } catch (err) {
  77. console.error('加载失败', err)
  78. uni.showToast({ title: err.message || '加载失败', icon: 'none' })
  79. } finally {
  80. loading.value = false
  81. }
  82. }
  83. const loadMore = async () => {
  84. if (loadingMore.value || finished.value) return
  85. loadingMore.value = true
  86. try {
  87. const nextPage = page.value + 1
  88. const params = {
  89. lang: locale.value,
  90. page: { current: nextPage, row: props.queryParams?.pageSize || props.pageSize },
  91. ...props.queryParams
  92. }
  93. const res = await props.fetchData(params)
  94. if (res.code === 200) {
  95. const newList = res.data || []
  96. if (newList.length > 0) {
  97. list.value.push(...newList)
  98. page.value = nextPage
  99. }
  100. // 更新 finished 状态
  101. const totalRows = res.page?.rowTotal || total.value
  102. finished.value = list.value.length >= totalRows
  103. } else {
  104. throw new Error(res.msg || '请求失败')
  105. }
  106. } catch (err) {
  107. uni.showToast({ title: err.message || '加载更多失败', icon: 'none' })
  108. } finally {
  109. loadingMore.value = false
  110. }
  111. }
  112. const handleItemClick = (item) => {
  113. uni.navigateTo({ url: `/pages/analytics/detail?type=${props.type}&id=${item.id}` })
  114. }
  115. const lang = computed(() => uni.getLocale())
  116. watch(lang, (val) => {
  117. load()
  118. }, { immediate: true })
  119. defineExpose({ load, loadMore })
  120. </script>
  121. <style lang="scss" scoped>
  122. @import "@/uni.scss";
  123. .news-list-container {
  124. .list {
  125. display: grid;
  126. grid-row-gap: px2rpx(16);
  127. grid-column-gap: px2rpx(24);
  128. grid-template-columns: repeat(auto-fill, minmax(px2rpx(389), 1fr));
  129. }
  130. .news-item {
  131. display: flex;
  132. flex-wrap: wrap;
  133. justify-content: center;
  134. padding: px2rpx(20);
  135. gap: px2rpx(10);
  136. margin-bottom: px2rpx(10);
  137. background: #fafafa;
  138. border-radius: px2rpx(8);
  139. overflow: hidden;
  140. box-shadow: 0 2rpx px2rpx(4) rgba(0, 0, 0, 0.05);
  141. box-sizing: border-box;
  142. cursor: pointer;
  143. // 添加过渡效果
  144. transition: all 0.3s ease;
  145. // 鼠标悬浮时的动效
  146. &:hover {
  147. transform: translateY(-4rpx);
  148. box-shadow: 0 8rpx px2rpx(16) rgba(0, 0, 0, 0.1);
  149. background: #fff;
  150. }
  151. .cover {
  152. aspect-ratio: 359 / 242;
  153. width: px2rpx(359);
  154. height: auto;
  155. border: 1px solid rgba(14, 15, 12, 0.12);
  156. border-radius: px2rpx(8);
  157. }
  158. .info {
  159. flex: 1;
  160. display: flex;
  161. flex-direction: column;
  162. justify-content: space-between;
  163. }
  164. .title {
  165. font-size: px2rpx(16);
  166. font-weight: bold;
  167. color: #333;
  168. line-height: 1.4;
  169. margin-bottom: px2rpx(6);
  170. display: -webkit-box;
  171. -webkit-box-orient: vertical;
  172. -webkit-line-clamp: 2;
  173. overflow: hidden;
  174. }
  175. .subtitle {
  176. font-size: px2rpx(14);
  177. color: #666;
  178. line-height: 1.4;
  179. display: -webkit-box;
  180. -webkit-box-orient: vertical;
  181. -webkit-line-clamp: 2;
  182. overflow: hidden;
  183. margin-bottom: px2rpx(6);
  184. }
  185. .meta {
  186. display: flex;
  187. justify-content: space-between;
  188. align-items: center;
  189. font-size: px2rpx(12);
  190. color: #999;
  191. }
  192. }
  193. }
  194. .load-more {
  195. text-align: center;
  196. padding: px2rpx(10);
  197. font-size: px2rpx(14);
  198. color: #999;
  199. }
  200. .empty {
  201. text-align: center;
  202. padding: 100rpx 0;
  203. color: #999;
  204. font-size: px2rpx(10);
  205. }
  206. </style>