DimensionProducts.vue 4.6 KB

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