DimensionProducts.vue 5.2 KB

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