DimensionProducts.vue 7.1 KB

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