List.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. <view v-if="item.coverImage" class="img-wrap">
  7. <image class="cover-img" :src="imgUrl + item.coverImage" mode="aspectFill" />
  8. </view>
  9. <view class="info">
  10. <view class="title">{{ item.title }}</view>
  11. <view class="subtitle">{{ item.subTitle }}</view>
  12. <view class="meta">
  13. <text class="date">{{ formatDate(item.deliveryTime) }}</text>
  14. </view>
  15. </view>
  16. </view>
  17. </view>
  18. <!-- 空状态 -->
  19. <view v-else-if="!loading && finished" class="empty">
  20. <cwg-empty-state />
  21. </view>
  22. <view class="table-loading-mask">
  23. <uni-loading v-if="loading" />
  24. </view>
  25. <!-- 加载更多状态 -->
  26. <view v-if="loadingMore" class="load-more">
  27. <text v-t11="'common.loading'" />
  28. </view>
  29. <view v-else-if="finished && list.length > 0" class="load-more">
  30. <text v-t11="'common.noMore'" />
  31. </view>
  32. </view>
  33. </template>
  34. <script setup>
  35. import { ref, watch, computed } from 'vue'
  36. import { useI18n } from 'vue-i18n'
  37. import Config from '@/config/index'
  38. const { locale } = useI18n()
  39. const props = defineProps({
  40. fetchData: { type: Function, required: true },
  41. queryParams: { type: Object, default: () => ({}) },
  42. pageSize: { type: Number, default: 10 },
  43. type: { type: Number, default: 0 },
  44. immediate: { type: Boolean, default: true },
  45. })
  46. const imgUrl = computed(() => props.queryParams.tag !== 3 ? Config.Host80 : Config.Host05)
  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. const totalRows = res.page?.rowTotal || total.value
  101. finished.value = list.value.length >= totalRows
  102. } else {
  103. throw new Error(res.msg || '请求失败')
  104. }
  105. } catch (err) {
  106. uni.showToast({ title: err.message || '加载更多失败', icon: 'none' })
  107. } finally {
  108. loadingMore.value = false
  109. }
  110. }
  111. const handleItemClick = (item) => {
  112. uni.navigateTo({ url: `/pages/analytics/detail?type=${props.type}&id=${item.id}` })
  113. }
  114. const lang = computed(() => uni.getLocale())
  115. watch(lang, () => {
  116. load()
  117. }, { immediate: true })
  118. defineExpose({ load, loadMore })
  119. </script>
  120. <style lang="scss" scoped>
  121. @import "@/uni.scss";
  122. .news-list-container {
  123. width: 100%;
  124. box-sizing: border-box;
  125. overflow-x: hidden;
  126. .list {
  127. display: grid;
  128. grid-row-gap: px2rpx(16);
  129. grid-column-gap: px2rpx(24);
  130. grid-template-columns: repeat(auto-fill, minmax(px2rpx(389), 1fr));
  131. width: 100%;
  132. box-sizing: border-box;
  133. }
  134. .news-item {
  135. display: flex;
  136. flex-direction: column;
  137. justify-content: flex-start;
  138. align-items: flex-start;
  139. padding: px2rpx(20);
  140. gap: px2rpx(10);
  141. margin-bottom: px2rpx(10);
  142. background: #fafafa;
  143. border-radius: px2rpx(8);
  144. overflow: hidden;
  145. box-shadow: 0 2rpx 4rpx rgba(0, 0, 0, 0.05);
  146. box-sizing: border-box;
  147. transition: all 0.3s ease;
  148. &:hover {
  149. transform: translateY(-4rpx);
  150. box-shadow: 0 8rpx 16rpx rgba(0, 0, 0, 0.1);
  151. background: #fff;
  152. }
  153. /* 添加悬停效果(仅H5) */
  154. /* #ifdef H5 */
  155. .news-item:hover {
  156. .img-wrap .cover-img {
  157. transform: scale(1.05);
  158. }
  159. }
  160. /* #endif */
  161. /* 外层固定图片容器,防止溢出 */
  162. .img-wrap {
  163. width: px2rpx(300);
  164. height: px2rpx(242);
  165. border-radius: px2rpx(8);
  166. overflow: hidden;
  167. flex-shrink: 0;
  168. position: relative;
  169. }
  170. .cover-img {
  171. width: 100%;
  172. height: 100%;
  173. object-fit: cover;
  174. transition: transform 0.6s ease;
  175. }
  176. .info {
  177. flex: 1;
  178. width: 100%;
  179. display: flex;
  180. flex-direction: column;
  181. justify-content: space-between;
  182. }
  183. .title {
  184. font-size: px2rpx(16);
  185. font-weight: bold;
  186. color: #333;
  187. line-height: 1.4;
  188. margin-bottom: px2rpx(6);
  189. display: -webkit-box;
  190. -webkit-box-orient: vertical;
  191. -webkit-line-clamp: 2;
  192. overflow: hidden;
  193. }
  194. .subtitle {
  195. font-size: px2rpx(14);
  196. color: #666;
  197. line-height: 1.4;
  198. display: -webkit-box;
  199. -webkit-box-orient: vertical;
  200. -webkit-line-clamp: 2;
  201. overflow: hidden;
  202. margin-bottom: px2rpx(6);
  203. }
  204. .meta {
  205. display: flex;
  206. justify-content: space-between;
  207. align-items: center;
  208. font-size: px2rpx(12);
  209. color: #999;
  210. }
  211. }
  212. }
  213. .load-more {
  214. text-align: center;
  215. padding: px2rpx(10);
  216. font-size: px2rpx(14);
  217. color: #999;
  218. }
  219. .empty {
  220. text-align: center;
  221. padding: 100rpx 0;
  222. color: #999;
  223. font-size: px2rpx(14);
  224. }
  225. </style>