DimensionProducts.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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, default: '' },
  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. formatPriceWithSymbol: function(price) {
  53. if (price == null) return '免费'
  54. return '¥' + (Number(price) / 100).toFixed(2)
  55. },
  56. parseWeights: function(str) {
  57. if (!str) return []
  58. try {
  59. var obj = JSON.parse(str)
  60. var result = []
  61. for (var key in obj) {
  62. if (obj[key] > 0 && DIMENSION_MAP[key]) {
  63. result.push({ name: DIMENSION_MAP[key].name, color: DIMENSION_MAP[key].color, weight: obj[key] })
  64. }
  65. }
  66. return result
  67. } catch (e) {
  68. return []
  69. }
  70. }
  71. }
  72. }
  73. </script>
  74. <style scoped>
  75. .section {
  76. margin: 20rpx 20rpx;
  77. }
  78. .section-header {
  79. display: flex;
  80. justify-content: space-between;
  81. align-items: center;
  82. margin-bottom: 20rpx;
  83. }
  84. .section-title {
  85. font-size: 30rpx;
  86. font-weight: bold;
  87. color: #333;
  88. }
  89. .section-more {
  90. font-size: 24rpx;
  91. color: #999;
  92. }
  93. .product-list {
  94. display: flex;
  95. flex-wrap: wrap;
  96. }
  97. .product-card {
  98. width: calc(50% - 10rpx);
  99. background: #fff;
  100. border-radius: 16rpx;
  101. overflow: hidden;
  102. box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.06);
  103. margin-right: 20rpx;
  104. margin-bottom: 20rpx;
  105. }
  106. .product-card:nth-child(2n) {
  107. margin-right: 0;
  108. }
  109. .product-img {
  110. width: 100%;
  111. height: 240rpx;
  112. background: #f0f0f0;
  113. }
  114. .product-name {
  115. font-size: 26rpx;
  116. color: #333;
  117. padding: 12rpx 16rpx 0;
  118. }
  119. .weight-badges {
  120. display: flex;
  121. flex-direction: row;
  122. flex-wrap: wrap;
  123. gap: 4rpx;
  124. padding: 4rpx 16rpx 0;
  125. }
  126. .weight-badge {
  127. font-size: 16rpx;
  128. padding: 1rpx 8rpx;
  129. border-radius: 6rpx;
  130. font-weight: 500;
  131. }
  132. .product-price {
  133. font-size: 28rpx;
  134. color: #F97316;
  135. font-weight: bold;
  136. padding: 8rpx 16rpx 16rpx;
  137. display: block;
  138. }
  139. .line-clamp-2 {
  140. overflow: hidden;
  141. text-overflow: ellipsis;
  142. display: -webkit-box;
  143. -webkit-line-clamp: 2;
  144. -webkit-box-orient: vertical;
  145. }
  146. .empty-state {
  147. display: flex;
  148. justify-content: center;
  149. padding: 40rpx 0;
  150. }
  151. .empty-tip {
  152. font-size: 24rpx;
  153. color: #999;
  154. }
  155. </style>