RecommendedFeed.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <template>
  2. <view class="section">
  3. <view class="section-header">
  4. <text class="section-title">🔥 发现好物</text>
  5. <text class="section-more" @click="$emit('more')">更多 ›</text>
  6. </view>
  7. <view class="feed-list">
  8. <!-- 无数据时显示占位 -->
  9. <template v-if="(!displayItems || displayItems.length === 0) && !loading">
  10. <view class="feed-card feed-placeholder" v-for="i in 4" :key="i">
  11. <view class="feed-img-placeholder"></view>
  12. <view class="feed-info">
  13. <text class="feed-title-placeholder line-clamp-2">精彩内容即将上线</text>
  14. <text class="feed-sub-placeholder">--</text>
  15. </view>
  16. </view>
  17. </template>
  18. <!-- 混合信息流 -->
  19. <template v-else>
  20. <view
  21. class="feed-card"
  22. v-for="(item, idx) in displayItems"
  23. :key="idx"
  24. @click="$emit('itemClick', item)"
  25. >
  26. <!-- 商品卡片 -->
  27. <template v-if="item.type === 'product'">
  28. <image class="feed-img" :src="getImageUrl(item.coverImage) || '/static/default-product.png'" mode="aspectFill" />
  29. <view class="feed-info">
  30. <view class="feed-tag-row">
  31. <text class="feed-type-badge product-badge">🛍️ 商品</text>
  32. </view>
  33. <text class="feed-title line-clamp-2">{{ item.name }}</text>
  34. <text class="feed-price" v-if="item.priceLabel">{{ item.priceLabel }}</text>
  35. <text class="feed-price fee-free" v-else-if="item.price == null || item.price === 0">免费</text>
  36. <text class="feed-price" v-else>{{ formatPrice(item) }}</text>
  37. </view>
  38. </template>
  39. <!-- 活动卡片 -->
  40. <template v-else-if="item.type === 'activity'">
  41. <image class="feed-img" :src="getImageUrl(item.coverImage) || '/static/default-activity.png'" mode="aspectFill" />
  42. <view class="feed-info">
  43. <view class="feed-tag-row">
  44. <text class="feed-type-badge activity-badge">🔥 活动</text>
  45. <text v-if="item.statusText" class="feed-status-badge" :class="item.statusClass">{{ item.statusText }}</text>
  46. </view>
  47. <text class="feed-title line-clamp-2">{{ item.title }}</text>
  48. <view class="feed-meta-row">
  49. <text class="feed-price" v-if="item.priceLabel">{{ item.priceLabel }}</text>
  50. <text class="feed-price" v-else-if="item.price && item.price > 0">{{ formatPrice(item) }}</text>
  51. <text class="feed-price fee-free" v-else>免费</text>
  52. <text class="feed-date" v-if="item.startTime">{{ item.startTime }}</text>
  53. </view>
  54. </view>
  55. </template>
  56. </view>
  57. </template>
  58. </view>
  59. </view>
  60. </template>
  61. <script>
  62. import config from '@/config.js'
  63. export default {
  64. props: {
  65. products: { type: Array, default: function() { return [] } },
  66. activities: { type: Array, default: function() { return [] } },
  67. maxItems: { type: Number, default: 6 },
  68. loading: { type: Boolean, default: false }
  69. },
  70. computed: {
  71. displayItems: function() {
  72. var productItems = (this.products || []).slice(0, 3).map(function(p) {
  73. return {
  74. id: p.id,
  75. type: 'product',
  76. name: p.name,
  77. coverImage: p.coverImage,
  78. price: p.price,
  79. priceLabel: p.priceLabel,
  80. source: p
  81. }
  82. })
  83. var activityItems = (this.activities || []).slice(0, 3).map(function(a) {
  84. return {
  85. id: a.id,
  86. type: 'activity',
  87. title: a.title,
  88. coverImage: a.coverImage,
  89. price: a.price,
  90. priceLabel: a.priceLabel,
  91. startTime: a.startTime,
  92. statusText: this.getStatusText(a.status),
  93. statusClass: this.getStatusClass(a.status),
  94. source: a
  95. }
  96. }.bind(this))
  97. // 交替合并:商品、活动、商品、活动...
  98. var merged = []
  99. var pi = 0, ai = 0
  100. var toggle = 0
  101. while ((pi < productItems.length || ai < activityItems.length) && merged.length < this.maxItems) {
  102. if (toggle % 2 === 0 && pi < productItems.length) {
  103. merged.push(productItems[pi++])
  104. } else if (ai < activityItems.length) {
  105. merged.push(activityItems[ai++])
  106. } else if (pi < productItems.length) {
  107. merged.push(productItems[pi++])
  108. }
  109. toggle++
  110. }
  111. return merged
  112. }
  113. },
  114. methods: {
  115. formatPrice: function(item) {
  116. var price = item.price
  117. if (price == null || price === 0) return '免费'
  118. return '¥' + (Number(price) / 100).toFixed(2)
  119. },
  120. getStatusText: function(status) {
  121. var map = { upcoming: '即将开始', ongoing: '进行中', ended: '已结束', cancelled: '已取消' }
  122. return map[status] || '即将开始'
  123. },
  124. getStatusClass: function(status) {
  125. var map = { upcoming: 'status-upcoming', ongoing: 'status-ongoing', ended: 'status-ended' }
  126. return map[status] || ''
  127. },
  128. getImageUrl: function(path) {
  129. return config.API_BASE_URL + path
  130. }
  131. }
  132. }
  133. </script>
  134. <style scoped>
  135. .section {
  136. margin: 20rpx 20rpx;
  137. }
  138. .section-header {
  139. display: flex;
  140. justify-content: space-between;
  141. align-items: center;
  142. margin-bottom: 20rpx;
  143. }
  144. .section-title {
  145. font-size: 30rpx;
  146. font-weight: bold;
  147. color: #333;
  148. }
  149. .section-more {
  150. font-size: 24rpx;
  151. color: #999;
  152. }
  153. .feed-list {
  154. display: flex;
  155. flex-direction: row;
  156. flex-wrap: wrap;
  157. gap: 16rpx;
  158. }
  159. .feed-card {
  160. width: calc(50% - 8rpx);
  161. background: #fff;
  162. border-radius: 16rpx;
  163. padding: 16rpx;
  164. box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.06);
  165. box-sizing: border-box;
  166. }
  167. .feed-card:active {
  168. opacity: 0.8;
  169. }
  170. .feed-img {
  171. width: 100%;
  172. height: 200rpx;
  173. border-radius: 12rpx;
  174. background: #f0f0f0;
  175. display: block;
  176. margin-bottom: 12rpx;
  177. }
  178. .feed-img-placeholder {
  179. width: 100%;
  180. height: 200rpx;
  181. border-radius: 12rpx;
  182. background: linear-gradient(135deg, #f0f0f0 25%, #e8e8e8 50%, #f0f0f0 75%);
  183. background-size: 200% 100%;
  184. animation: shimmer 1.5s infinite;
  185. margin-bottom: 12rpx;
  186. }
  187. @keyframes shimmer {
  188. 0% { background-position: 200% 0; }
  189. 100% { background-position: -200% 0; }
  190. }
  191. .feed-info {
  192. display: flex;
  193. flex-direction: column;
  194. }
  195. .feed-tag-row {
  196. display: flex;
  197. align-items: center;
  198. gap: 8rpx;
  199. margin-bottom: 6rpx;
  200. }
  201. .feed-type-badge {
  202. font-size: 20rpx;
  203. padding: 2rpx 8rpx;
  204. border-radius: 6rpx;
  205. font-weight: 500;
  206. }
  207. .product-badge {
  208. background: #FFF0E6;
  209. color: #F97316;
  210. }
  211. .activity-badge {
  212. background: #FEF2E6;
  213. color: #EA580C;
  214. }
  215. .feed-status-badge {
  216. font-size: 18rpx;
  217. padding: 2rpx 8rpx;
  218. border-radius: 6rpx;
  219. }
  220. .status-upcoming { background: #DBEAFE; color: #2563EB; }
  221. .status-ongoing { background: #D1FAE5; color: #16A34A; }
  222. .status-ended { background: #F3F4F6; color: #9CA3AF; }
  223. .feed-title {
  224. font-size: 26rpx;
  225. color: #333;
  226. font-weight: 500;
  227. display: block;
  228. margin-bottom: 6rpx;
  229. min-height: 36rpx;
  230. line-height: 1.4;
  231. }
  232. .feed-title-placeholder {
  233. font-size: 26rpx;
  234. color: #ccc;
  235. display: block;
  236. margin-bottom: 6rpx;
  237. min-height: 36rpx;
  238. }
  239. .feed-price {
  240. font-size: 24rpx;
  241. color: #F97316;
  242. font-weight: 600;
  243. }
  244. .fee-free {
  245. color: #22C55E;
  246. }
  247. .feed-meta-row {
  248. display: flex;
  249. align-items: center;
  250. gap: 12rpx;
  251. margin-top: 4rpx;
  252. }
  253. .feed-date {
  254. font-size: 20rpx;
  255. color: #bbb;
  256. }
  257. .feed-sub-placeholder {
  258. font-size: 24rpx;
  259. color: #ccc;
  260. }
  261. .line-clamp-2 {
  262. overflow: hidden;
  263. text-overflow: ellipsis;
  264. display: -webkit-box;
  265. -webkit-line-clamp: 2;
  266. -webkit-box-orient: vertical;
  267. }
  268. </style>