RecommendedFeed.vue 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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" lazy-load />
  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" lazy-load />
  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. if (!path || path === 'null' || path === 'undefined' || path === '/null') return ''
  130. var s = String(path).trim()
  131. if (!s || s === 'null' || s === '/null') return ''
  132. if (s.indexOf('http://') === 0 || s.indexOf('https://') === 0) return s
  133. return config.API_BASE_URL + (s.charAt(0) === '/' ? s : '/' + s)
  134. }
  135. }
  136. }
  137. </script>
  138. <style scoped lang="scss">
  139. .section {
  140. margin: 20rpx 20rpx;
  141. }
  142. .section-header {
  143. display: flex;
  144. justify-content: space-between;
  145. align-items: center;
  146. margin-bottom: 20rpx;
  147. }
  148. .section-title {
  149. font-size: 30rpx;
  150. font-weight: bold;
  151. color: $facade-text-strong;
  152. }
  153. .section-more {
  154. font-size: 24rpx;
  155. color: $facade-text-muted;
  156. }
  157. .feed-list {
  158. display: flex;
  159. flex-direction: row;
  160. flex-wrap: wrap;
  161. gap: 16rpx;
  162. }
  163. .feed-card {
  164. width: calc(50% - 8rpx);
  165. background: $app-surface;
  166. border-radius: 16rpx;
  167. padding: 16rpx;
  168. box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.06);
  169. box-sizing: border-box;
  170. }
  171. .feed-card:active {
  172. opacity: 0.8;
  173. }
  174. .feed-img {
  175. width: 100%;
  176. height: 200rpx;
  177. border-radius: 12rpx;
  178. background: $facade-fill;
  179. display: block;
  180. margin-bottom: 12rpx;
  181. }
  182. .feed-img-placeholder {
  183. width: 100%;
  184. height: 200rpx;
  185. border-radius: 12rpx;
  186. background: linear-gradient(135deg, $facade-fill 25%, $facade-fill-4 50%, $facade-fill 75%);
  187. background-size: 200% 100%;
  188. animation: shimmer 1.5s infinite;
  189. margin-bottom: 12rpx;
  190. }
  191. @keyframes shimmer {
  192. 0% { background-position: 200% 0; }
  193. 100% { background-position: -200% 0; }
  194. }
  195. .feed-info {
  196. display: flex;
  197. flex-direction: column;
  198. }
  199. .feed-tag-row {
  200. display: flex;
  201. align-items: center;
  202. gap: 8rpx;
  203. margin-bottom: 6rpx;
  204. }
  205. .feed-type-badge {
  206. font-size: 20rpx;
  207. padding: 2rpx 8rpx;
  208. border-radius: 6rpx;
  209. font-weight: 500;
  210. }
  211. .product-badge {
  212. background: #FFF0E6;
  213. color: $brand-primary;
  214. }
  215. .activity-badge {
  216. background: #FEF2E6;
  217. color: $brand-primary-dark;
  218. }
  219. .feed-status-badge {
  220. font-size: 18rpx;
  221. padding: 2rpx 8rpx;
  222. border-radius: 6rpx;
  223. }
  224. .status-upcoming { background: $status-info-soft; color: #2563EB; }
  225. .status-ongoing { background: $status-success-soft; color: $status-success-strong-2; }
  226. .status-ended { background: $facade-fill-gray-100; color: $facade-text-gray-400; }
  227. .feed-title {
  228. font-size: 26rpx;
  229. color: $facade-text-strong;
  230. font-weight: 500;
  231. display: block;
  232. margin-bottom: 6rpx;
  233. min-height: 36rpx;
  234. line-height: 1.4;
  235. }
  236. .feed-title-placeholder {
  237. font-size: 26rpx;
  238. color: $facade-text-ghost;
  239. display: block;
  240. margin-bottom: 6rpx;
  241. min-height: 36rpx;
  242. }
  243. .feed-price {
  244. font-size: 24rpx;
  245. color: $brand-primary;
  246. font-weight: 600;
  247. }
  248. .fee-free {
  249. color: $status-success-2;
  250. }
  251. .feed-meta-row {
  252. display: flex;
  253. align-items: center;
  254. gap: 12rpx;
  255. margin-top: 4rpx;
  256. }
  257. .feed-date {
  258. font-size: 20rpx;
  259. color: $facade-text-ghost-light;
  260. }
  261. .feed-sub-placeholder {
  262. font-size: 24rpx;
  263. color: $facade-text-ghost;
  264. }
  265. .line-clamp-2 {
  266. overflow: hidden;
  267. text-overflow: ellipsis;
  268. display: -webkit-box;
  269. -webkit-line-clamp: 2;
  270. -webkit-box-orient: vertical;
  271. }
  272. </style>