DimensionProducts.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <template>
  2. <view class="section">
  3. <view class="section-header">
  4. <text class="section-title">🛍️ 推荐商品</text>
  5. <text class="section-more" @click="$emit('moreProducts')">更多 ›</text>
  6. </view>
  7. <view v-if="!displayProducts || displayProducts.length === 0" class="empty-state">
  8. <text class="empty-tip">{{ isLoggedIn ? '暂无商品' : '登录后查看更多商品' }}</text>
  9. </view>
  10. <view class="product-list" v-if="displayProducts && displayProducts.length > 0">
  11. <view class="product-card" v-for="prod in displayProducts" :key="prod.id" @click="$emit('productClick', prod)">
  12. <image class="product-img" :src="prod.coverImage || '/static/default-product.png'" mode="aspectFill" />
  13. <text class="product-name line-clamp-2">{{ prod.name }}</text>
  14. <view class="weight-badges" v-if="prod._badges && prod._badges.length">
  15. <text class="weight-badge" v-for="badge in prod._badges" :key="badge.name" :style="{ background: badge.color + '20', color: badge.color }">{{ badge.name }}{{ badge.weight }}%</text>
  16. </view>
  17. <text class="product-price" v-if="prod.priceLabel">{{ prod.priceLabel }}</text>
  18. <text class="product-price" v-else-if="prod.price">{{ formatPriceWithSymbol(prod.price) }}</text>
  19. <text class="product-price" v-else>免费</text>
  20. </view>
  21. </view>
  22. </view>
  23. </template>
  24. <script>
  25. var DIMENSION_MAP = {
  26. body: { name: '身', color: '#FF8C42' },
  27. mind: { name: '心', color: '#FF6B9D' },
  28. wisdom: { name: '智', color: '#6366F1' },
  29. action: { name: '行', color: '#10B981' },
  30. wealth: { name: '富', color: '#F59E0B' }
  31. }
  32. export default {
  33. props: {
  34. dimensionCode: { type: String, required: true },
  35. products: { type: Array, default: function() { return [] } },
  36. isLoggedIn: { type: Boolean, default: false }
  37. },
  38. data: function() {
  39. return {
  40. }
  41. },
  42. computed: {
  43. displayProducts: function() {
  44. var self = this
  45. return (this.products || []).map(function(item) {
  46. item._badges = self.parseWeights(item.dimensionWeights)
  47. return item
  48. })
  49. }
  50. },
  51. methods: {
  52. parseWeights: function(str) {
  53. if (!str) return []
  54. try {
  55. var obj = JSON.parse(str)
  56. var result = []
  57. for (var key in obj) {
  58. if (obj[key] > 0 && DIMENSION_MAP[key]) {
  59. result.push({ name: DIMENSION_MAP[key].name, color: DIMENSION_MAP[key].color, weight: obj[key] })
  60. }
  61. }
  62. return result
  63. } catch (e) {
  64. return []
  65. }
  66. }
  67. }
  68. }
  69. </script>
  70. <style scoped>
  71. .section {
  72. margin: 20rpx 20rpx;
  73. }
  74. .section-header {
  75. display: flex;
  76. justify-content: space-between;
  77. align-items: center;
  78. margin-bottom: 20rpx;
  79. }
  80. .section-title {
  81. font-size: 30rpx;
  82. font-weight: bold;
  83. color: #333;
  84. }
  85. .section-more {
  86. font-size: 24rpx;
  87. color: #999;
  88. }
  89. .product-list {
  90. display: flex;
  91. flex-wrap: wrap;
  92. }
  93. .product-card {
  94. width: calc(50% - 10rpx);
  95. background: #fff;
  96. border-radius: 16rpx;
  97. overflow: hidden;
  98. box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.06);
  99. margin-right: 20rpx;
  100. margin-bottom: 20rpx;
  101. }
  102. .product-card:nth-child(2n) {
  103. margin-right: 0;
  104. }
  105. .product-img {
  106. width: 100%;
  107. height: 240rpx;
  108. background: #f0f0f0;
  109. }
  110. .product-name {
  111. font-size: 26rpx;
  112. color: #333;
  113. padding: 12rpx 16rpx 0;
  114. }
  115. .weight-badges {
  116. display: flex;
  117. flex-direction: row;
  118. flex-wrap: wrap;
  119. gap: 4rpx;
  120. padding: 4rpx 16rpx 0;
  121. }
  122. .weight-badge {
  123. font-size: 16rpx;
  124. padding: 1rpx 8rpx;
  125. border-radius: 6rpx;
  126. font-weight: 500;
  127. }
  128. .product-price {
  129. font-size: 28rpx;
  130. color: #F97316;
  131. font-weight: bold;
  132. padding: 8rpx 16rpx 16rpx;
  133. display: block;
  134. }
  135. .line-clamp-2 {
  136. overflow: hidden;
  137. text-overflow: ellipsis;
  138. display: -webkit-box;
  139. -webkit-line-clamp: 2;
  140. -webkit-box-orient: vertical;
  141. }
  142. .empty-state {
  143. display: flex;
  144. justify-content: center;
  145. padding: 40rpx 0;
  146. }
  147. .empty-tip {
  148. font-size: 24rpx;
  149. color: #999;
  150. }
  151. </style>