DimensionProducts.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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="prod.coverImage || '/static/default-product.png'" mode="aspectFill" />
  13. <text class="product-name line-clamp-2">{{ prod.name }}</text>
  14. <view class="weight-badges" v-if="prod._badges && prod._badges.length">
  15. <text class="weight-badge" v-for="badge in prod._badges" :key="badge.name" :style="{ background: badge.color + '20', color: badge.color }">{{ badge.name }}{{ badge.weight }}%</text>
  16. </view>
  17. <view class="price-row">
  18. <text class="product-price" v-if="prod.priceLabel">{{ prod.priceLabel }}</text>
  19. <text class="product-price" v-else-if="prod.price">{{ formatPriceWithSymbol(prod.price) }}</text>
  20. <text class="product-price" v-else>免费</text>
  21. <text class="product-reason" v-if="prod.reason">{{ prod.reason }}</text>
  22. </view>
  23. <view class="match-badge" v-if="prod.matchScore">
  24. <text class="match-score">{{ prod.matchScore }}分</text>
  25. </view>
  26. </view>
  27. </view>
  28. <view class="loading-state" v-if="loading">
  29. <text class="loading-text">加载中...</text>
  30. </view>
  31. </view>
  32. </template>
  33. <script>
  34. import { getDimensionProducts, clickRepurchaseReminder } from '../utils/api.js'
  35. var DIMENSION_MAP = {
  36. body: { name: '身', color: '#FF8C42' },
  37. mind: { name: '心', color: '#FF6B9D' },
  38. wisdom: { name: '智', color: '#6366F1' },
  39. action: { name: '行', color: '#10B981' },
  40. wealth: { name: '富', color: '#F59E0B' }
  41. }
  42. export default {
  43. props: {
  44. dimensionCode: { type: String, default: '' },
  45. // 兼容模式:父组件直接传入商品数组(member-detail 等页面)
  46. products: { type: Array, default: function() { return [] } },
  47. // 自加载模式:传入 familyId 后组件内部调用 getDimensionProducts
  48. familyId: { type: [Number, String], default: null },
  49. isLoggedIn: { type: Boolean, default: false },
  50. title: { type: String, default: '推荐商品' },
  51. showMore: { type: Boolean, default: true }
  52. },
  53. data: function() {
  54. return {
  55. selfProducts: [],
  56. loading: false
  57. }
  58. },
  59. computed: {
  60. displayProducts: function() {
  61. var list = this.products && this.products.length > 0 ? this.products : this.selfProducts
  62. var self = this
  63. return (list || []).map(function(item) {
  64. item._badges = self.parseWeights(item.dimensionWeights)
  65. return item
  66. })
  67. },
  68. emptyText: function() {
  69. // 有 familyId(已登录且选中孩子)但推荐接口无数据
  70. if (this.familyId) {
  71. return '暂无推荐商品'
  72. }
  73. return this.isLoggedIn ? '暂无商品' : '登录后查看更多商品'
  74. }
  75. },
  76. mounted: function() {
  77. // 自加载模式:有 familyId 且父组件未传入 products 时才请求推荐接口
  78. if (this.familyId && (!this.products || this.products.length === 0)) {
  79. this.loadDimensionProducts()
  80. }
  81. },
  82. watch: {
  83. familyId: function(val) {
  84. // 切换孩子/登录态变化时重新加载
  85. if (val && (!this.products || this.products.length === 0)) {
  86. this.loadDimensionProducts()
  87. }
  88. }
  89. },
  90. methods: {
  91. loadDimensionProducts: function() {
  92. var self = this
  93. self.loading = true
  94. var params = {
  95. dimensionCode: self.dimensionCode,
  96. familyId: self.familyId,
  97. limit: 6
  98. }
  99. getDimensionProducts(params).then(function(res) {
  100. self.loading = false
  101. if (res.code === 200 && res.data) {
  102. self.selfProducts = Array.isArray(res.data) ? res.data : (res.data.records || [])
  103. }
  104. }).catch(function() {
  105. self.loading = false
  106. self.selfProducts = []
  107. })
  108. },
  109. onProductClick: function(prod) {
  110. if (!prod || !prod.id) return
  111. // 复购提醒追踪(推荐商品点击行为上报)
  112. if (prod.source === 'repurchase' && prod.id) {
  113. clickRepurchaseReminder(prod.id).catch(function() {})
  114. }
  115. this.$emit('productClick', prod)
  116. },
  117. formatPriceWithSymbol: function(price) {
  118. if (price == null) return '免费'
  119. return '¥' + (Number(price) / 100).toFixed(2)
  120. },
  121. parseWeights: function(str) {
  122. if (!str) return []
  123. try {
  124. var obj = JSON.parse(str)
  125. var result = []
  126. for (var key in obj) {
  127. if (obj[key] > 0 && DIMENSION_MAP[key]) {
  128. result.push({ name: DIMENSION_MAP[key].name, color: DIMENSION_MAP[key].color, weight: obj[key] })
  129. }
  130. }
  131. return result
  132. } catch (e) {
  133. return []
  134. }
  135. }
  136. }
  137. }
  138. </script>
  139. <style scoped>
  140. .section {
  141. margin: 20rpx 20rpx;
  142. }
  143. .section-header {
  144. display: flex;
  145. justify-content: space-between;
  146. align-items: center;
  147. margin-bottom: 20rpx;
  148. }
  149. .section-title {
  150. font-size: 30rpx;
  151. font-weight: bold;
  152. color: #333;
  153. }
  154. .section-more {
  155. font-size: 24rpx;
  156. color: #999;
  157. }
  158. .product-list {
  159. display: flex;
  160. flex-wrap: wrap;
  161. }
  162. .product-card {
  163. width: calc(50% - 10rpx);
  164. background: #fff;
  165. border-radius: 16rpx;
  166. overflow: hidden;
  167. box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.06);
  168. margin-right: 20rpx;
  169. margin-bottom: 20rpx;
  170. }
  171. .product-card:nth-child(2n) {
  172. margin-right: 0;
  173. }
  174. .product-img {
  175. width: 100%;
  176. height: 240rpx;
  177. background: #f0f0f0;
  178. }
  179. .product-name {
  180. font-size: 26rpx;
  181. color: #333;
  182. padding: 12rpx 16rpx 0;
  183. }
  184. .weight-badges {
  185. display: flex;
  186. flex-direction: row;
  187. flex-wrap: wrap;
  188. gap: 4rpx;
  189. padding: 4rpx 16rpx 0;
  190. }
  191. .weight-badge {
  192. font-size: 16rpx;
  193. padding: 1rpx 8rpx;
  194. border-radius: 6rpx;
  195. font-weight: 500;
  196. }
  197. .price-row {
  198. display: flex;
  199. flex-direction: row;
  200. align-items: center;
  201. justify-content: space-between;
  202. padding: 8rpx 16rpx 16rpx;
  203. }
  204. .product-price {
  205. font-size: 28rpx;
  206. color: #F97316;
  207. font-weight: bold;
  208. }
  209. .product-reason {
  210. font-size: 20rpx;
  211. color: #999;
  212. max-width: 60%;
  213. overflow: hidden;
  214. text-overflow: ellipsis;
  215. white-space: nowrap;
  216. }
  217. .match-badge {
  218. display: inline-block;
  219. background: linear-gradient(135deg, #6366F1, #818CF8);
  220. padding: 4rpx 16rpx;
  221. border-radius: 20rpx;
  222. margin: 0 16rpx 16rpx;
  223. }
  224. .match-score {
  225. font-size: 22rpx;
  226. color: #fff;
  227. font-weight: 500;
  228. }
  229. .line-clamp-2 {
  230. overflow: hidden;
  231. text-overflow: ellipsis;
  232. display: -webkit-box;
  233. -webkit-line-clamp: 2;
  234. -webkit-box-orient: vertical;
  235. }
  236. .empty-state {
  237. display: flex;
  238. justify-content: center;
  239. padding: 40rpx 0;
  240. }
  241. .empty-tip {
  242. font-size: 24rpx;
  243. color: #999;
  244. }
  245. .loading-state {
  246. display: flex;
  247. justify-content: center;
  248. padding: 40rpx 0;
  249. }
  250. .loading-text {
  251. font-size: 24rpx;
  252. color: #999;
  253. }
  254. </style>