DimensionProducts.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <template>
  2. <view>
  3. <view v-if="!loading && displayProducts.length === 0" class="empty-state">
  4. <text class="empty-tip">{{ emptyText }}</text>
  5. </view>
  6. <view v-else class="product-list">
  7. <view class="product-card" v-for="prod in displayProducts" :key="prod.id" @click="onProductClick(prod)">
  8. <image class="product-img" :src="getImageUrl(prod.coverImage) || '/static/default-product.png'" mode="aspectFill" />
  9. <view class="product-body">
  10. <text class="product-name">{{ prod.name }}</text>
  11. <view class="price-row">
  12. <text class="product-price" v-if="prod.priceLabel">{{ prod.priceLabel }}</text>
  13. <text class="product-price" v-else-if="prod.price">{{ formatPriceWithSymbol(prod.price) }}</text>
  14. <text class="product-price" v-else>免费</text>
  15. </view>
  16. </view>
  17. </view>
  18. </view>
  19. <view class="loading-state" v-if="loading">
  20. <text class="loading-text">加载中...</text>
  21. </view>
  22. </view>
  23. </template>
  24. <script>
  25. import { getDimensionProducts, clickRepurchaseReminder } from '../utils/api.js'
  26. import config from '@/config.js'
  27. export default {
  28. props: {
  29. dimensionCode: { type: String, default: '' },
  30. // 兼容模式:父组件直接传入商品数组(member-detail 等页面)
  31. products: { type: Array, default: function() { return [] } },
  32. // 自加载模式:传入 familyId 后组件内部调用 getDimensionProducts
  33. familyId: { type: [Number, String], default: null },
  34. isLoggedIn: { type: Boolean, default: false },
  35. title: { type: String, default: '推荐商品' },
  36. showMore: { type: Boolean, default: true }
  37. },
  38. data: function() {
  39. return {
  40. selfProducts: [],
  41. loading: false
  42. }
  43. },
  44. computed: {
  45. displayProducts: function() {
  46. return (this.products && this.products.length > 0 ? this.products : this.selfProducts).slice(0, 4)
  47. },
  48. emptyText: function() {
  49. // 有 familyId(已登录且选中孩子)但推荐接口无数据
  50. if (this.familyId) {
  51. return '暂无推荐商品'
  52. }
  53. return this.isLoggedIn ? '暂无商品' : '登录后查看更多商品'
  54. }
  55. },
  56. mounted: function() {
  57. // 自加载模式:有 familyId 且父组件未传入 products 时才请求推荐接口
  58. if (this.familyId && (!this.products || this.products.length === 0)) {
  59. this.loadDimensionProducts()
  60. }
  61. },
  62. watch: {
  63. familyId: function(val) {
  64. // 切换孩子/登录态变化时重新加载
  65. if (val && (!this.products || this.products.length === 0)) {
  66. this.loadDimensionProducts()
  67. }
  68. }
  69. },
  70. methods: {
  71. loadDimensionProducts: function() {
  72. var self = this
  73. self.loading = true
  74. var params = {
  75. dimensionCode: self.dimensionCode,
  76. limit: 4
  77. }
  78. getDimensionProducts(params).then(function(res) {
  79. self.loading = false
  80. if (res.code === 200 && res.data) {
  81. self.selfProducts = Array.isArray(res.data) ? res.data : (res.data.records || [])
  82. }
  83. }).catch(function() {
  84. self.loading = false
  85. self.selfProducts = []
  86. })
  87. },
  88. onProductClick: function(prod) {
  89. if (!prod || !prod.id) return
  90. // 复购提醒追踪(推荐商品点击行为上报)
  91. if (prod.source === 'repurchase' && prod.id) {
  92. clickRepurchaseReminder(prod.id).catch(function() {})
  93. }
  94. this.$emit('productClick', prod)
  95. },
  96. formatPriceWithSymbol: function(price) {
  97. if (price == null) return '免费'
  98. return '¥' + (Number(price) / 100).toFixed(2)
  99. },
  100. getImageUrl: function(path) {
  101. return config.API_BASE_URL + path
  102. }
  103. }
  104. }
  105. </script>
  106. <style scoped>
  107. .product-list {
  108. display: flex;
  109. flex-wrap: wrap;
  110. }
  111. .product-card {
  112. width: calc(25% - 12rpx);
  113. background: #fff;
  114. border-radius: 12rpx;
  115. overflow: hidden;
  116. box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.06);
  117. margin-right: 16rpx;
  118. margin-bottom: 16rpx;
  119. }
  120. .product-card:nth-child(4n) {
  121. margin-right: 0;
  122. }
  123. .product-img {
  124. width: 100%;
  125. height: 160rpx;
  126. background: #f0f0f0;
  127. }
  128. .product-body {
  129. padding: 8rpx 10rpx 12rpx;
  130. }
  131. .product-name {
  132. display: block;
  133. font-size: 22rpx;
  134. color: #333;
  135. line-height: 1.4;
  136. margin-bottom: 4rpx;
  137. overflow: hidden;
  138. text-overflow: ellipsis;
  139. white-space: nowrap;
  140. }
  141. .price-row {
  142. display: flex;
  143. align-items: baseline;
  144. }
  145. .product-price {
  146. font-size: 24rpx;
  147. color: #F97316;
  148. font-weight: bold;
  149. }
  150. .empty-state {
  151. display: flex;
  152. justify-content: center;
  153. padding: 40rpx 0;
  154. }
  155. .empty-tip {
  156. font-size: 24rpx;
  157. color: #999;
  158. }
  159. .loading-state {
  160. display: flex;
  161. justify-content: center;
  162. padding: 40rpx 0;
  163. }
  164. .loading-text {
  165. font-size: 24rpx;
  166. color: #999;
  167. }
  168. </style>