RecommendedFeed.vue 7.4 KB

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