DimensionProducts.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. // 自加载模式:已登录且父组件未传入 products 时才请求推荐接口
  57. // (familyId 由后端从 JWT 派生,前端无需传入)
  58. if (this.isLoggedIn && (!this.products || this.products.length === 0)) {
  59. this.loadDimensionProducts()
  60. }
  61. },
  62. watch: {
  63. familyId: function() {
  64. // 切换孩子/登录态变化时重新加载
  65. if (this.isLoggedIn && (!this.products || this.products.length === 0)) {
  66. this.loadDimensionProducts()
  67. }
  68. },
  69. isLoggedIn: function(val) {
  70. // 登录后触发加载
  71. if (val && (!this.products || this.products.length === 0)) {
  72. this.loadDimensionProducts()
  73. }
  74. }
  75. },
  76. methods: {
  77. loadDimensionProducts: function() {
  78. var self = this
  79. self.loading = true
  80. var params = {
  81. limit: 4
  82. }
  83. getDimensionProducts(params).then(function(res) {
  84. self.loading = false
  85. if (res.code === 200 && res.data) {
  86. self.selfProducts = Array.isArray(res.data) ? res.data : (res.data.records || [])
  87. }
  88. }).catch(function() {
  89. self.loading = false
  90. self.selfProducts = []
  91. })
  92. },
  93. onProductClick: function(prod) {
  94. if (!prod || !prod.id) return
  95. // 复购提醒追踪(推荐商品点击行为上报)
  96. if (prod.source === 'repurchase' && prod.id) {
  97. clickRepurchaseReminder(prod.id).catch(function() {})
  98. }
  99. this.$emit('productClick', prod)
  100. },
  101. formatPriceWithSymbol: function(price) {
  102. if (price == null) return '免费'
  103. return '¥' + (Number(price) / 100).toFixed(2)
  104. },
  105. getImageUrl: function(path) {
  106. return config.API_BASE_URL + path
  107. }
  108. }
  109. }
  110. </script>
  111. <style scoped>
  112. .product-list {
  113. display: flex;
  114. flex-wrap: wrap;
  115. }
  116. .product-card {
  117. width: calc(25% - 12rpx);
  118. background: #fff;
  119. border-radius: 12rpx;
  120. overflow: hidden;
  121. box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.06);
  122. margin-right: 16rpx;
  123. margin-bottom: 16rpx;
  124. }
  125. .product-card:nth-child(4n) {
  126. margin-right: 0;
  127. }
  128. .product-img {
  129. width: 100%;
  130. height: 160rpx;
  131. background: #f0f0f0;
  132. }
  133. .product-body {
  134. padding: 8rpx 10rpx 12rpx;
  135. }
  136. .product-name {
  137. display: block;
  138. font-size: 22rpx;
  139. color: #333;
  140. line-height: 1.4;
  141. margin-bottom: 4rpx;
  142. overflow: hidden;
  143. text-overflow: ellipsis;
  144. white-space: nowrap;
  145. }
  146. .price-row {
  147. display: flex;
  148. align-items: baseline;
  149. }
  150. .product-price {
  151. font-size: 24rpx;
  152. color: #F97316;
  153. font-weight: bold;
  154. }
  155. .empty-state {
  156. display: flex;
  157. justify-content: center;
  158. padding: 40rpx 0;
  159. }
  160. .empty-tip {
  161. font-size: 24rpx;
  162. color: #999;
  163. }
  164. .loading-state {
  165. display: flex;
  166. justify-content: center;
  167. padding: 40rpx 0;
  168. }
  169. .loading-text {
  170. font-size: 24rpx;
  171. color: #999;
  172. }
  173. </style>