DimensionProducts.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. if (!path || path === 'null' || path === 'undefined' || path === '/null') return ''
  107. var s = String(path).trim()
  108. if (!s || s === 'null' || s === '/null') return ''
  109. if (s.indexOf('http://') === 0 || s.indexOf('https://') === 0) return s
  110. return config.API_BASE_URL + (s.charAt(0) === '/' ? s : '/' + s)
  111. }
  112. }
  113. }
  114. </script>
  115. <style scoped>
  116. .product-list {
  117. display: flex;
  118. flex-wrap: wrap;
  119. }
  120. .product-card {
  121. width: calc(25% - 12rpx);
  122. background: #fff;
  123. border-radius: 12rpx;
  124. overflow: hidden;
  125. box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.06);
  126. margin-right: 16rpx;
  127. margin-bottom: 16rpx;
  128. }
  129. .product-card:nth-child(4n) {
  130. margin-right: 0;
  131. }
  132. .product-img {
  133. width: 100%;
  134. height: 160rpx;
  135. background: #f0f0f0;
  136. }
  137. .product-body {
  138. padding: 8rpx 10rpx 12rpx;
  139. }
  140. .product-name {
  141. display: block;
  142. font-size: 22rpx;
  143. color: #333;
  144. line-height: 1.4;
  145. margin-bottom: 4rpx;
  146. overflow: hidden;
  147. text-overflow: ellipsis;
  148. white-space: nowrap;
  149. }
  150. .price-row {
  151. display: flex;
  152. align-items: baseline;
  153. }
  154. .product-price {
  155. font-size: 24rpx;
  156. color: #F97316;
  157. font-weight: bold;
  158. }
  159. .empty-state {
  160. display: flex;
  161. justify-content: center;
  162. padding: 40rpx 0;
  163. }
  164. .empty-tip {
  165. font-size: 24rpx;
  166. color: #999;
  167. }
  168. .loading-state {
  169. display: flex;
  170. justify-content: center;
  171. padding: 40rpx 0;
  172. }
  173. .loading-text {
  174. font-size: 24rpx;
  175. color: #999;
  176. }
  177. </style>