DimensionProducts.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <template>
  2. <view>
  3. <view v-if="hasFamily">
  4. <view v-if="!loading && displayProducts.length === 0" class="empty-state">
  5. <text class="empty-tip">{{ emptyText }}</text>
  6. </view>
  7. <view v-else class="product-list">
  8. <view class="product-card" v-for="prod in displayProducts" :key="prod.id" @click="onProductClick(prod)">
  9. <image class="product-img" :src="getImageUrl(prod.coverImage) || '/static/default-product.png'" mode="aspectFill" />
  10. <text class="product-name line-clamp-2">{{ 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. <text class="product-reason" v-if="prod.reason">{{ prod.reason }}</text>
  16. </view>
  17. <view class="match-badge" v-if="prod.matchScore">
  18. <text class="match-score">{{ prod.matchScore }}分</text>
  19. </view>
  20. </view>
  21. </view>
  22. <view class="loading-state" v-if="loading">
  23. <text class="loading-text">加载中...</text>
  24. </view>
  25. </view>
  26. <view v-else>
  27. <FamilyEmptyState type="card" />
  28. </view>
  29. </view>
  30. </template>
  31. <script>
  32. import { getDimensionProducts, clickRepurchaseReminder } from '../utils/api.js'
  33. import config from '@/config.js'
  34. export default {
  35. props: {
  36. dimensionCode: { type: String, default: '' },
  37. // 兼容模式:父组件直接传入商品数组(member-detail 等页面)
  38. products: { type: Array, default: function() { return [] } },
  39. // 自加载模式:传入 familyId 后组件内部调用 getDimensionProducts
  40. familyId: { type: [Number, String], default: null },
  41. isLoggedIn: { type: Boolean, default: false },
  42. title: { type: String, default: '推荐商品' },
  43. showMore: { type: Boolean, default: true }
  44. },
  45. data: function() {
  46. return {
  47. selfProducts: [],
  48. loading: false
  49. }
  50. },
  51. computed: {
  52. hasFamily: function() {
  53. // 应急修复(2026-08-07,对齐 body/index.vue):store.familyId 在 familyId 后端派生改造后
  54. // 不再被写入,hasFamily 恒为 false 导致商品区被 v-else 空态吞掉。
  55. // 改为基于组件自身数据判断:有 familyId(自加载模式)或父组件传入商品(兼容模式)
  56. // 即认为有家庭,store getter 仅作兜底。
  57. if (this.familyId) {
  58. return true
  59. }
  60. if (this.products && this.products.length > 0) {
  61. return true
  62. }
  63. // 父组件已控制整体可见性(如 index-home 的行页面),信任父组件
  64. if (this.isLoggedIn) {
  65. return true
  66. }
  67. return this.$store.getters.hasFamily
  68. },
  69. displayProducts: function() {
  70. return (this.products && this.products.length > 0 ? this.products : this.selfProducts).slice(0, 2)
  71. },
  72. emptyText: function() {
  73. // 有 familyId(已登录且选中孩子)但推荐接口无数据
  74. if (this.familyId) {
  75. return '暂无推荐商品'
  76. }
  77. return this.isLoggedIn ? '暂无商品' : '登录后查看更多商品'
  78. }
  79. },
  80. mounted: function() {
  81. // 自加载模式:有 familyId 且父组件未传入 products 时才请求推荐接口
  82. if (this.familyId && (!this.products || this.products.length === 0)) {
  83. this.loadDimensionProducts()
  84. }
  85. },
  86. watch: {
  87. familyId: function(val) {
  88. // 切换孩子/登录态变化时重新加载
  89. if (val && (!this.products || this.products.length === 0)) {
  90. this.loadDimensionProducts()
  91. }
  92. }
  93. },
  94. methods: {
  95. loadDimensionProducts: function() {
  96. var self = this
  97. self.loading = true
  98. var params = {
  99. dimensionCode: self.dimensionCode,
  100. limit: 4
  101. }
  102. getDimensionProducts(params).then(function(res) {
  103. self.loading = false
  104. if (res.code === 200 && res.data) {
  105. self.selfProducts = Array.isArray(res.data) ? res.data : (res.data.records || [])
  106. }
  107. }).catch(function() {
  108. self.loading = false
  109. self.selfProducts = []
  110. })
  111. },
  112. onProductClick: function(prod) {
  113. if (!prod || !prod.id) return
  114. // 复购提醒追踪(推荐商品点击行为上报)
  115. if (prod.source === 'repurchase' && prod.id) {
  116. clickRepurchaseReminder(prod.id).catch(function() {})
  117. }
  118. this.$emit('productClick', prod)
  119. },
  120. formatPriceWithSymbol: function(price) {
  121. if (price == null) return '免费'
  122. return '¥' + (Number(price) / 100).toFixed(2)
  123. },
  124. getImageUrl: function(path) {
  125. return config.API_BASE_URL + path
  126. }
  127. }
  128. }
  129. </script>
  130. <style scoped>
  131. .product-list {
  132. display: flex;
  133. flex-wrap: wrap;
  134. }
  135. .product-card {
  136. width: calc(50% - 10rpx);
  137. background: #fff;
  138. border-radius: 14rpx;
  139. overflow: hidden;
  140. box-shadow: 0 2rpx 10rpx rgba(0,0,0,0.06);
  141. margin-right: 14rpx;
  142. margin-bottom: 14rpx;
  143. }
  144. .product-card:nth-child(2n) {
  145. margin-right: 0;
  146. }
  147. .product-img {
  148. width: 100%;
  149. height: 180rpx;
  150. background: #f0f0f0;
  151. }
  152. .product-name {
  153. font-size: 24rpx;
  154. color: #333;
  155. padding: 8rpx 14rpx 0;
  156. line-height: 1.3;
  157. }
  158. .price-row {
  159. display: flex;
  160. flex-direction: row;
  161. align-items: center;
  162. justify-content: space-between;
  163. padding: 4rpx 14rpx 12rpx;
  164. }
  165. .product-price {
  166. font-size: 26rpx;
  167. color: #F97316;
  168. font-weight: bold;
  169. }
  170. .product-reason {
  171. font-size: 18rpx;
  172. color: #999;
  173. max-width: 60%;
  174. overflow: hidden;
  175. text-overflow: ellipsis;
  176. white-space: nowrap;
  177. }
  178. .match-badge {
  179. display: inline-block;
  180. background: linear-gradient(135deg, #6366F1, #818CF8);
  181. padding: 4rpx 16rpx;
  182. border-radius: 20rpx;
  183. margin: 0 10rpx 10rpx;
  184. }
  185. .match-score {
  186. font-size: 22rpx;
  187. color: #fff;
  188. font-weight: 500;
  189. }
  190. .line-clamp-2 {
  191. overflow: hidden;
  192. text-overflow: ellipsis;
  193. display: -webkit-box;
  194. -webkit-line-clamp: 2;
  195. -webkit-box-orient: vertical;
  196. }
  197. .empty-state {
  198. display: flex;
  199. justify-content: center;
  200. padding: 40rpx 0;
  201. }
  202. .empty-tip {
  203. font-size: 24rpx;
  204. color: #999;
  205. }
  206. .loading-state {
  207. display: flex;
  208. justify-content: center;
  209. padding: 40rpx 0;
  210. }
  211. .loading-text {
  212. font-size: 24rpx;
  213. color: #999;
  214. }
  215. </style>