DimensionProducts.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <template>
  2. <view class="section" v-if="displayProducts && displayProducts.length > 0">
  3. <view class="section-header">
  4. <text class="section-title">🛍️ 推荐商品</text>
  5. <text class="section-more" @click="$emit('moreProducts')">更多 ›</text>
  6. </view>
  7. <view class="product-list">
  8. <view class="product-card" v-for="prod in displayProducts" :key="prod.id" @click="$emit('productClick', prod)">
  9. <image class="product-img" :src="prod.coverImage || '/static/default-product.png'" mode="aspectFill" />
  10. <text class="product-name line-clamp-2">{{ prod.name }}</text>
  11. <text class="product-price" v-if="prod.priceLabel">{{ prod.priceLabel }}</text>
  12. <text class="product-price" v-else-if="prod.price">¥{{ prod.price }}</text>
  13. <text class="product-price" v-else>免费</text>
  14. </view>
  15. </view>
  16. </view>
  17. </template>
  18. <script>
  19. export default {
  20. props: {
  21. dimensionCode: { type: String, required: true },
  22. products: { type: Array, default: function() { return [] } }
  23. },
  24. data: function() {
  25. return {
  26. productsData: []
  27. }
  28. },
  29. computed: {
  30. displayProducts: function() {
  31. return this.productsData.length > 0 ? this.productsData : this.products
  32. }
  33. },
  34. created: function() {
  35. this.loadProducts()
  36. },
  37. methods: {
  38. loadProducts: function() {
  39. var self = this
  40. import('@/utils/api.js').then(function(api) {
  41. api.getProductsByDomain(self.dimensionCode, 1, 6).then(function(res) {
  42. var records = (res.data && res.data.records) ? res.data.records : (res.data || [])
  43. self.productsData = records
  44. }).catch(function() {
  45. // silently fail, use props as fallback
  46. })
  47. })
  48. }
  49. }
  50. }
  51. </script>
  52. <style scoped>
  53. .section {
  54. margin: 20rpx 20rpx;
  55. }
  56. .section-header {
  57. display: flex;
  58. justify-content: space-between;
  59. align-items: center;
  60. margin-bottom: 20rpx;
  61. }
  62. .section-title {
  63. font-size: 30rpx;
  64. font-weight: bold;
  65. color: #333;
  66. }
  67. .section-more {
  68. font-size: 24rpx;
  69. color: #999;
  70. }
  71. .product-list {
  72. display: flex;
  73. flex-wrap: wrap;
  74. }
  75. .product-card {
  76. width: calc(50% - 10rpx);
  77. background: #fff;
  78. border-radius: 16rpx;
  79. overflow: hidden;
  80. box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.06);
  81. margin-right: 20rpx;
  82. margin-bottom: 20rpx;
  83. }
  84. .product-card:nth-child(2n) {
  85. margin-right: 0;
  86. }
  87. .product-img {
  88. width: 100%;
  89. height: 240rpx;
  90. background: #f0f0f0;
  91. }
  92. .product-name {
  93. font-size: 26rpx;
  94. color: #333;
  95. padding: 12rpx 16rpx 0;
  96. }
  97. .product-price {
  98. font-size: 28rpx;
  99. color: #F97316;
  100. font-weight: bold;
  101. padding: 8rpx 16rpx 16rpx;
  102. display: block;
  103. }
  104. .line-clamp-2 {
  105. overflow: hidden;
  106. text-overflow: ellipsis;
  107. display: -webkit-box;
  108. -webkit-line-clamp: 2;
  109. -webkit-box-orient: vertical;
  110. }
  111. </style>