List.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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(20);
  129. grid-column-gap: px2rpx(16);
  130. grid-template-columns: repeat(3, 1fr);
  131. @media screen and (max-width: 768px) {
  132. grid-template-columns: 1fr;
  133. }
  134. width: 100%;
  135. box-sizing: border-box;
  136. }
  137. .news-item {
  138. display: flex;
  139. flex-direction: column;
  140. justify-content: flex-start;
  141. align-items: flex-start;
  142. padding: px2rpx(16);
  143. gap: px2rpx(12);
  144. background: #fff;
  145. border-radius: px2rpx(12);
  146. overflow: hidden;
  147. box-shadow:
  148. 0 2rpx 8rpx rgba(0, 0, 0, 0.04),
  149. 0 1rpx 2rpx rgba(0, 0, 0, 0.06);
  150. box-sizing: border-box;
  151. transition: all 0.3s ease;
  152. border: 1rpx solid rgba(0, 0, 0, 0.03);
  153. &:active {
  154. transform: scale(0.98);
  155. box-shadow: 0 1rpx 4rpx rgba(0, 0, 0, 0.08);
  156. }
  157. /* #ifdef H5 */
  158. &:hover {
  159. transform: translateY(-4rpx);
  160. box-shadow:
  161. 0 8rpx 24rpx rgba(0, 0, 0, 0.08),
  162. 0 4rpx 8rpx rgba(0, 0, 0, 0.06);
  163. }
  164. &:hover .img-wrap .cover-img {
  165. transform: scale(1.05);
  166. }
  167. /* #endif */
  168. .img-wrap {
  169. width: 100%;
  170. height: px2rpx(220);
  171. border-radius: px2rpx(8);
  172. overflow: hidden;
  173. flex-shrink: 0;
  174. position: relative;
  175. background: linear-gradient(135deg, #f5f7fa 0%, #e4e8ec 100%);
  176. }
  177. .cover-img {
  178. width: 100%;
  179. height: 100%;
  180. object-fit: cover;
  181. transition: transform 0.5s cubic-bezier(0.4, 0, 0.2, 1);
  182. }
  183. .info {
  184. flex: 1;
  185. width: 100%;
  186. display: flex;
  187. flex-direction: column;
  188. justify-content: space-between;
  189. min-height: px2rpx(120);
  190. }
  191. .title {
  192. font-size: px2rpx(17);
  193. font-weight: 600;
  194. color: #fff;
  195. line-height: 1.5;
  196. margin-bottom: px2rpx(8);
  197. display: -webkit-box;
  198. -webkit-box-orient: vertical;
  199. -webkit-line-clamp: 2;
  200. overflow: hidden;
  201. letter-spacing: 0.3rpx;
  202. }
  203. .subtitle {
  204. font-size: px2rpx(14);
  205. color: #666666;
  206. line-height: 1.6;
  207. display: -webkit-box;
  208. -webkit-box-orient: vertical;
  209. -webkit-line-clamp: 2;
  210. overflow: hidden;
  211. margin-bottom: px2rpx(10);
  212. }
  213. .meta {
  214. display: flex;
  215. justify-content: flex-start;
  216. align-items: center;
  217. font-size: px2rpx(12);
  218. color: #999999;
  219. padding-top: px2rpx(8);
  220. border-top: 1rpx solid #f0f0f0;
  221. }
  222. }
  223. }
  224. .load-more {
  225. text-align: center;
  226. padding: px2rpx(10);
  227. font-size: px2rpx(14);
  228. color: var(--bs-heading-color);
  229. }
  230. .empty {
  231. text-align: center;
  232. padding: 100rpx 0;
  233. color: var(--bs-heading-color);
  234. font-size: px2rpx(14);
  235. }
  236. </style>