DimensionProducts.vue 6.7 KB

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