DimensionProducts.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. return this.$store.getters.hasFamily
  58. },
  59. displayProducts: function() {
  60. return this.products && this.products.length > 0 ? this.products : this.selfProducts
  61. },
  62. emptyText: function() {
  63. // 有 familyId(已登录且选中孩子)但推荐接口无数据
  64. if (this.familyId) {
  65. return '暂无推荐商品'
  66. }
  67. return this.isLoggedIn ? '暂无商品' : '登录后查看更多商品'
  68. }
  69. },
  70. mounted: function() {
  71. // 自加载模式:有 familyId 且父组件未传入 products 时才请求推荐接口
  72. if (this.familyId && (!this.products || this.products.length === 0)) {
  73. this.loadDimensionProducts()
  74. }
  75. },
  76. watch: {
  77. familyId: function(val) {
  78. // 切换孩子/登录态变化时重新加载
  79. if (val && (!this.products || this.products.length === 0)) {
  80. this.loadDimensionProducts()
  81. }
  82. }
  83. },
  84. methods: {
  85. loadDimensionProducts: function() {
  86. var self = this
  87. self.loading = true
  88. var params = {
  89. dimensionCode: self.dimensionCode,
  90. limit: 6
  91. }
  92. getDimensionProducts(params).then(function(res) {
  93. self.loading = false
  94. if (res.code === 200 && res.data) {
  95. self.selfProducts = Array.isArray(res.data) ? res.data : (res.data.records || [])
  96. }
  97. }).catch(function() {
  98. self.loading = false
  99. self.selfProducts = []
  100. })
  101. },
  102. onProductClick: function(prod) {
  103. if (!prod || !prod.id) return
  104. // 复购提醒追踪(推荐商品点击行为上报)
  105. if (prod.source === 'repurchase' && prod.id) {
  106. clickRepurchaseReminder(prod.id).catch(function() {})
  107. }
  108. this.$emit('productClick', prod)
  109. },
  110. formatPriceWithSymbol: function(price) {
  111. if (price == null) return '免费'
  112. return '¥' + (Number(price) / 100).toFixed(2)
  113. },
  114. getImageUrl: function(path) {
  115. return config.API_BASE_URL + path
  116. }
  117. }
  118. }
  119. </script>
  120. <style scoped>
  121. .section {
  122. margin: 20rpx 20rpx;
  123. }
  124. .section-header {
  125. display: flex;
  126. justify-content: space-between;
  127. align-items: center;
  128. margin-bottom: 20rpx;
  129. }
  130. .section-title {
  131. font-size: 30rpx;
  132. font-weight: bold;
  133. color: #333;
  134. }
  135. .section-more {
  136. font-size: 24rpx;
  137. color: #999;
  138. }
  139. .product-list {
  140. display: flex;
  141. flex-wrap: wrap;
  142. }
  143. .product-card {
  144. width: calc(50% - 10rpx);
  145. background: #fff;
  146. border-radius: 16rpx;
  147. overflow: hidden;
  148. box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.06);
  149. margin-right: 20rpx;
  150. margin-bottom: 20rpx;
  151. }
  152. .product-card:nth-child(2n) {
  153. margin-right: 0;
  154. }
  155. .product-img {
  156. width: 100%;
  157. height: 240rpx;
  158. background: #f0f0f0;
  159. }
  160. .product-name {
  161. font-size: 26rpx;
  162. color: #333;
  163. padding: 12rpx 16rpx 0;
  164. }
  165. .price-row {
  166. display: flex;
  167. flex-direction: row;
  168. align-items: center;
  169. justify-content: space-between;
  170. padding: 8rpx 16rpx 16rpx;
  171. }
  172. .product-price {
  173. font-size: 28rpx;
  174. color: #F97316;
  175. font-weight: bold;
  176. }
  177. .product-reason {
  178. font-size: 20rpx;
  179. color: #999;
  180. max-width: 60%;
  181. overflow: hidden;
  182. text-overflow: ellipsis;
  183. white-space: nowrap;
  184. }
  185. .match-badge {
  186. display: inline-block;
  187. background: linear-gradient(135deg, #6366F1, #818CF8);
  188. padding: 4rpx 16rpx;
  189. border-radius: 20rpx;
  190. margin: 0 16rpx 16rpx;
  191. }
  192. .match-score {
  193. font-size: 22rpx;
  194. color: #fff;
  195. font-weight: 500;
  196. }
  197. .line-clamp-2 {
  198. overflow: hidden;
  199. text-overflow: ellipsis;
  200. display: -webkit-box;
  201. -webkit-line-clamp: 2;
  202. -webkit-box-orient: vertical;
  203. }
  204. .empty-state {
  205. display: flex;
  206. justify-content: center;
  207. padding: 40rpx 0;
  208. }
  209. .empty-tip {
  210. font-size: 24rpx;
  211. color: #999;
  212. }
  213. .loading-state {
  214. display: flex;
  215. justify-content: center;
  216. padding: 40rpx 0;
  217. }
  218. .loading-text {
  219. font-size: 24rpx;
  220. color: #999;
  221. }
  222. </style>