| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- <template>
- <view class="section">
- <view class="section-header">
- <text class="section-title">🛍️ 推荐商品</text>
- <text class="section-more" @click="$emit('moreProducts')">更多 ›</text>
- </view>
- <view v-if="!displayProducts || displayProducts.length === 0" class="empty-state">
- <text class="empty-tip">{{ isLoggedIn ? '暂无商品' : '登录后查看更多商品' }}</text>
- </view>
- <view class="product-list" v-if="displayProducts && displayProducts.length > 0">
- <view class="product-card" v-for="prod in displayProducts" :key="prod.id" @click="$emit('productClick', prod)">
- <image class="product-img" :src="prod.coverImage || '/static/default-product.png'" mode="aspectFill" />
- <text class="product-name line-clamp-2">{{ prod.name }}</text>
- <view class="weight-badges" v-if="prod._badges && prod._badges.length">
- <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>
- </view>
- <text class="product-price" v-if="prod.priceLabel">{{ prod.priceLabel }}</text>
- <text class="product-price" v-else-if="prod.price">{{ formatPriceWithSymbol(prod.price) }}</text>
- <text class="product-price" v-else>免费</text>
- </view>
- </view>
- </view>
- </template>
- <script>
- var DIMENSION_MAP = {
- body: { name: '身', color: '#FF8C42' },
- mind: { name: '心', color: '#FF6B9D' },
- wisdom: { name: '智', color: '#6366F1' },
- action: { name: '行', color: '#10B981' },
- wealth: { name: '富', color: '#F59E0B' }
- }
- export default {
- props: {
- dimensionCode: { type: String, required: true },
- products: { type: Array, default: function() { return [] } },
- isLoggedIn: { type: Boolean, default: false }
- },
- data: function() {
- return {
- }
- },
- computed: {
- displayProducts: function() {
- var self = this
- return (this.products || []).map(function(item) {
- item._badges = self.parseWeights(item.dimensionWeights)
- return item
- })
- }
- },
- methods: {
- parseWeights: function(str) {
- if (!str) return []
- try {
- var obj = JSON.parse(str)
- var result = []
- for (var key in obj) {
- if (obj[key] > 0 && DIMENSION_MAP[key]) {
- result.push({ name: DIMENSION_MAP[key].name, color: DIMENSION_MAP[key].color, weight: obj[key] })
- }
- }
- return result
- } catch (e) {
- return []
- }
- }
- }
- }
- </script>
- <style scoped>
- .section {
- margin: 20rpx 20rpx;
- }
- .section-header {
- display: flex;
- justify-content: space-between;
- align-items: center;
- margin-bottom: 20rpx;
- }
- .section-title {
- font-size: 30rpx;
- font-weight: bold;
- color: #333;
- }
- .section-more {
- font-size: 24rpx;
- color: #999;
- }
- .product-list {
- display: flex;
- flex-wrap: wrap;
- }
- .product-card {
- width: calc(50% - 10rpx);
- background: #fff;
- border-radius: 16rpx;
- overflow: hidden;
- box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.06);
- margin-right: 20rpx;
- margin-bottom: 20rpx;
- }
- .product-card:nth-child(2n) {
- margin-right: 0;
- }
- .product-img {
- width: 100%;
- height: 240rpx;
- background: #f0f0f0;
- }
- .product-name {
- font-size: 26rpx;
- color: #333;
- padding: 12rpx 16rpx 0;
- }
- .weight-badges {
- display: flex;
- flex-direction: row;
- flex-wrap: wrap;
- gap: 4rpx;
- padding: 4rpx 16rpx 0;
- }
- .weight-badge {
- font-size: 16rpx;
- padding: 1rpx 8rpx;
- border-radius: 6rpx;
- font-weight: 500;
- }
- .product-price {
- font-size: 28rpx;
- color: #F97316;
- font-weight: bold;
- padding: 8rpx 16rpx 16rpx;
- display: block;
- }
- .line-clamp-2 {
- overflow: hidden;
- text-overflow: ellipsis;
- display: -webkit-box;
- -webkit-line-clamp: 2;
- -webkit-box-orient: vertical;
- }
- .empty-state {
- display: flex;
- justify-content: center;
- padding: 40rpx 0;
- }
- .empty-tip {
- font-size: 24rpx;
- color: #999;
- }
- </style>
|