DimensionProducts.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. return this.$store.getters.hasFamily
  68. },
  69. displayProducts: function() {
  70. return this.products && this.products.length > 0 ? this.products : this.selfProducts
  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: 6
  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. .section {
  132. margin: 20rpx 20rpx;
  133. }
  134. .section-header {
  135. display: flex;
  136. justify-content: space-between;
  137. align-items: center;
  138. margin-bottom: 20rpx;
  139. }
  140. .section-title {
  141. font-size: 30rpx;
  142. font-weight: bold;
  143. color: #333;
  144. }
  145. .section-more {
  146. font-size: 24rpx;
  147. color: #999;
  148. }
  149. .product-list {
  150. display: flex;
  151. flex-wrap: wrap;
  152. }
  153. .product-card {
  154. width: calc(50% - 10rpx);
  155. background: #fff;
  156. border-radius: 16rpx;
  157. overflow: hidden;
  158. box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.06);
  159. margin-right: 20rpx;
  160. margin-bottom: 20rpx;
  161. }
  162. .product-card:nth-child(2n) {
  163. margin-right: 0;
  164. }
  165. .product-img {
  166. width: 100%;
  167. height: 240rpx;
  168. background: #f0f0f0;
  169. }
  170. .product-name {
  171. font-size: 26rpx;
  172. color: #333;
  173. padding: 12rpx 16rpx 0;
  174. }
  175. .price-row {
  176. display: flex;
  177. flex-direction: row;
  178. align-items: center;
  179. justify-content: space-between;
  180. padding: 8rpx 16rpx 16rpx;
  181. }
  182. .product-price {
  183. font-size: 28rpx;
  184. color: #F97316;
  185. font-weight: bold;
  186. }
  187. .product-reason {
  188. font-size: 20rpx;
  189. color: #999;
  190. max-width: 60%;
  191. overflow: hidden;
  192. text-overflow: ellipsis;
  193. white-space: nowrap;
  194. }
  195. .match-badge {
  196. display: inline-block;
  197. background: linear-gradient(135deg, #6366F1, #818CF8);
  198. padding: 4rpx 16rpx;
  199. border-radius: 20rpx;
  200. margin: 0 16rpx 16rpx;
  201. }
  202. .match-score {
  203. font-size: 22rpx;
  204. color: #fff;
  205. font-weight: 500;
  206. }
  207. .line-clamp-2 {
  208. overflow: hidden;
  209. text-overflow: ellipsis;
  210. display: -webkit-box;
  211. -webkit-line-clamp: 2;
  212. -webkit-box-orient: vertical;
  213. }
  214. .empty-state {
  215. display: flex;
  216. justify-content: center;
  217. padding: 40rpx 0;
  218. }
  219. .empty-tip {
  220. font-size: 24rpx;
  221. color: #999;
  222. }
  223. .loading-state {
  224. display: flex;
  225. justify-content: center;
  226. padding: 40rpx 0;
  227. }
  228. .loading-text {
  229. font-size: 24rpx;
  230. color: #999;
  231. }
  232. </style>