DimensionProducts.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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="!loading && displayProducts.length === 0" class="empty-state">
  8. <text class="empty-tip">{{ emptyText }}</text>
  9. </view>
  10. <view class="product-list" v-if="displayProducts.length > 0">
  11. <view class="product-card" v-for="prod in displayProducts" :key="prod.id" @click="onProductClick(prod)">
  12. <image class="product-img" :src="getImageUrl(prod.coverImage) || '/static/default-product.png'" mode="aspectFill" />
  13. <text class="product-name line-clamp-2">{{ prod.name }}</text>
  14. <view class="price-row">
  15. <text class="product-price" v-if="prod.priceLabel">{{ prod.priceLabel }}</text>
  16. <text class="product-price" v-else-if="prod.price">{{ formatPriceWithSymbol(prod.price) }}</text>
  17. <text class="product-price" v-else>免费</text>
  18. <text class="product-reason" v-if="prod.reason">{{ prod.reason }}</text>
  19. </view>
  20. <view class="match-badge" v-if="prod.matchScore">
  21. <text class="match-score">{{ prod.matchScore }}分</text>
  22. </view>
  23. </view>
  24. </view>
  25. <view class="loading-state" v-if="loading">
  26. <text class="loading-text">加载中...</text>
  27. </view>
  28. </view>
  29. </template>
  30. <script>
  31. import { getDimensionProducts, clickRepurchaseReminder } from '../utils/api.js'
  32. import config from '@/config.js'
  33. export default {
  34. props: {
  35. dimensionCode: { type: String, default: '' },
  36. // 兼容模式:父组件直接传入商品数组(member-detail 等页面)
  37. products: { type: Array, default: function() { return [] } },
  38. // 自加载模式:传入 familyId 后组件内部调用 getDimensionProducts
  39. familyId: { type: [Number, String], default: null },
  40. isLoggedIn: { type: Boolean, default: false },
  41. title: { type: String, default: '推荐商品' },
  42. showMore: { type: Boolean, default: true }
  43. },
  44. data: function() {
  45. return {
  46. selfProducts: [],
  47. loading: false
  48. }
  49. },
  50. computed: {
  51. displayProducts: function() {
  52. return this.products && this.products.length > 0 ? this.products : this.selfProducts
  53. },
  54. emptyText: function() {
  55. // 有 familyId(已登录且选中孩子)但推荐接口无数据
  56. if (this.familyId) {
  57. return '暂无推荐商品'
  58. }
  59. return this.isLoggedIn ? '暂无商品' : '登录后查看更多商品'
  60. }
  61. },
  62. mounted: function() {
  63. // 自加载模式:有 familyId 且父组件未传入 products 时才请求推荐接口
  64. if (this.familyId && (!this.products || this.products.length === 0)) {
  65. this.loadDimensionProducts()
  66. }
  67. },
  68. watch: {
  69. familyId: function(val) {
  70. // 切换孩子/登录态变化时重新加载
  71. if (val && (!this.products || this.products.length === 0)) {
  72. this.loadDimensionProducts()
  73. }
  74. }
  75. },
  76. methods: {
  77. loadDimensionProducts: function() {
  78. var self = this
  79. self.loading = true
  80. var params = {
  81. dimensionCode: self.dimensionCode,
  82. familyId: self.familyId,
  83. limit: 6
  84. }
  85. getDimensionProducts(params).then(function(res) {
  86. self.loading = false
  87. if (res.code === 200 && res.data) {
  88. self.selfProducts = Array.isArray(res.data) ? res.data : (res.data.records || [])
  89. }
  90. }).catch(function() {
  91. self.loading = false
  92. self.selfProducts = []
  93. })
  94. },
  95. onProductClick: function(prod) {
  96. if (!prod || !prod.id) return
  97. // 复购提醒追踪(推荐商品点击行为上报)
  98. if (prod.source === 'repurchase' && prod.id) {
  99. clickRepurchaseReminder(prod.id).catch(function() {})
  100. }
  101. this.$emit('productClick', prod)
  102. },
  103. formatPriceWithSymbol: function(price) {
  104. if (price == null) return '免费'
  105. return '¥' + (Number(price) / 100).toFixed(2)
  106. },
  107. getImageUrl: function(path) {
  108. return config.API_BASE_URL + path
  109. }
  110. }
  111. }
  112. </script>
  113. <style scoped>
  114. .section {
  115. margin: 20rpx 20rpx;
  116. }
  117. .section-header {
  118. display: flex;
  119. justify-content: space-between;
  120. align-items: center;
  121. margin-bottom: 20rpx;
  122. }
  123. .section-title {
  124. font-size: 30rpx;
  125. font-weight: bold;
  126. color: #333;
  127. }
  128. .section-more {
  129. font-size: 24rpx;
  130. color: #999;
  131. }
  132. .product-list {
  133. display: flex;
  134. flex-wrap: wrap;
  135. }
  136. .product-card {
  137. width: calc(50% - 10rpx);
  138. background: #fff;
  139. border-radius: 16rpx;
  140. overflow: hidden;
  141. box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.06);
  142. margin-right: 20rpx;
  143. margin-bottom: 20rpx;
  144. }
  145. .product-card:nth-child(2n) {
  146. margin-right: 0;
  147. }
  148. .product-img {
  149. width: 100%;
  150. height: 240rpx;
  151. background: #f0f0f0;
  152. }
  153. .product-name {
  154. font-size: 26rpx;
  155. color: #333;
  156. padding: 12rpx 16rpx 0;
  157. }
  158. .price-row {
  159. display: flex;
  160. flex-direction: row;
  161. align-items: center;
  162. justify-content: space-between;
  163. padding: 8rpx 16rpx 16rpx;
  164. }
  165. .product-price {
  166. font-size: 28rpx;
  167. color: #F97316;
  168. font-weight: bold;
  169. }
  170. .product-reason {
  171. font-size: 20rpx;
  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 16rpx 16rpx;
  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>