DimensionProducts.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. limit: 6
  83. }
  84. getDimensionProducts(params).then(function(res) {
  85. self.loading = false
  86. if (res.code === 200 && res.data) {
  87. self.selfProducts = Array.isArray(res.data) ? res.data : (res.data.records || [])
  88. }
  89. }).catch(function() {
  90. self.loading = false
  91. self.selfProducts = []
  92. })
  93. },
  94. onProductClick: function(prod) {
  95. if (!prod || !prod.id) return
  96. // 复购提醒追踪(推荐商品点击行为上报)
  97. if (prod.source === 'repurchase' && prod.id) {
  98. clickRepurchaseReminder(prod.id).catch(function() {})
  99. }
  100. this.$emit('productClick', prod)
  101. },
  102. formatPriceWithSymbol: function(price) {
  103. if (price == null) return '免费'
  104. return '¥' + (Number(price) / 100).toFixed(2)
  105. },
  106. getImageUrl: function(path) {
  107. return config.API_BASE_URL + path
  108. }
  109. }
  110. }
  111. </script>
  112. <style scoped>
  113. .section {
  114. margin: 20rpx 20rpx;
  115. }
  116. .section-header {
  117. display: flex;
  118. justify-content: space-between;
  119. align-items: center;
  120. margin-bottom: 20rpx;
  121. }
  122. .section-title {
  123. font-size: 30rpx;
  124. font-weight: bold;
  125. color: #333;
  126. }
  127. .section-more {
  128. font-size: 24rpx;
  129. color: #999;
  130. }
  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: 16rpx;
  139. overflow: hidden;
  140. box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.06);
  141. margin-right: 20rpx;
  142. margin-bottom: 20rpx;
  143. }
  144. .product-card:nth-child(2n) {
  145. margin-right: 0;
  146. }
  147. .product-img {
  148. width: 100%;
  149. height: 240rpx;
  150. background: #f0f0f0;
  151. }
  152. .product-name {
  153. font-size: 26rpx;
  154. color: #333;
  155. padding: 12rpx 16rpx 0;
  156. }
  157. .price-row {
  158. display: flex;
  159. flex-direction: row;
  160. align-items: center;
  161. justify-content: space-between;
  162. padding: 8rpx 16rpx 16rpx;
  163. }
  164. .product-price {
  165. font-size: 28rpx;
  166. color: #F97316;
  167. font-weight: bold;
  168. }
  169. .product-reason {
  170. font-size: 20rpx;
  171. color: #999;
  172. max-width: 60%;
  173. overflow: hidden;
  174. text-overflow: ellipsis;
  175. white-space: nowrap;
  176. }
  177. .match-badge {
  178. display: inline-block;
  179. background: linear-gradient(135deg, #6366F1, #818CF8);
  180. padding: 4rpx 16rpx;
  181. border-radius: 20rpx;
  182. margin: 0 16rpx 16rpx;
  183. }
  184. .match-score {
  185. font-size: 22rpx;
  186. color: #fff;
  187. font-weight: 500;
  188. }
  189. .line-clamp-2 {
  190. overflow: hidden;
  191. text-overflow: ellipsis;
  192. display: -webkit-box;
  193. -webkit-line-clamp: 2;
  194. -webkit-box-orient: vertical;
  195. }
  196. .empty-state {
  197. display: flex;
  198. justify-content: center;
  199. padding: 40rpx 0;
  200. }
  201. .empty-tip {
  202. font-size: 24rpx;
  203. color: #999;
  204. }
  205. .loading-state {
  206. display: flex;
  207. justify-content: center;
  208. padding: 40rpx 0;
  209. }
  210. .loading-text {
  211. font-size: 24rpx;
  212. color: #999;
  213. }
  214. </style>