点赞收藏功能细化,问题修复

This commit is contained in:
xiaowang
2026-03-05 15:15:59 +08:00
parent bea4b63e3e
commit 560921c0e9
13 changed files with 560 additions and 131 deletions
+37 -20
View File
@@ -39,22 +39,24 @@
</div>
<!-- Banner 轮播区域 -->
<div class="banner-carousel">
<a-carousel v-if="bannerImages.length > 0" autoplay :autoplay-speed="3000" effect="fade">
<div v-for="(image, index) in bannerImages" :key="index" class="banner-slide" @click="handleBannerClick">
<img :src="image" alt="Banner" class="banner-img" />
</div>
</a-carousel>
<div v-else class="banner" @click="handleBannerClick">
<div class="banner-content">
<a-tag color="red" class="banner-tag">限时优惠</a-tag>
<h3>Get Winter Discount</h3>
<h2>20% Off</h2>
<p>For Children</p>
<a-button type="primary" size="small" class="banner-btn">立即购买</a-button>
</div>
<div class="banner-image">
<div class="img-placeholder">Banner</div>
<div class="banner-container">
<div class="banner-carousel">
<a-carousel v-if="bannerImages.length > 0" autoplay :autoplay-speed="3000" effect="fade">
<div v-for="(image, index) in bannerImages" :key="index" class="banner-slide" @click="handleBannerClick">
<img :src="image" alt="Banner" class="banner-img" />
</div>
</a-carousel>
<div v-else class="banner" @click="handleBannerClick">
<div class="banner-content">
<a-tag color="red" class="banner-tag">限时优惠</a-tag>
<h3>Get Winter Discount</h3>
<h2>20% Off</h2>
<p>For Children</p>
<a-button type="primary" size="small" class="banner-btn">立即购买</a-button>
</div>
<div class="banner-image">
<div class="img-placeholder">Banner</div>
</div>
</div>
</div>
</div>
@@ -418,20 +420,34 @@ const handleProductClick = (item: any) => {
}
/* Banner轮播区域 */
.banner-container {
display: flex;
justify-content: center;
margin: 15px 0;
max-width: 1200px;
width: 100%;
margin-left: auto;
margin-right: auto;
padding: 0 20px;
}
.banner-carousel {
margin: 15px 20px;
width: 100%;
border-radius: 16px;
overflow: hidden;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
aspect-ratio: 4/3;
max-height: 450px;
}
.banner-carousel :deep(.ant-carousel) {
border-radius: 16px;
height: 100%;
}
.banner-slide {
width: 100%;
height: 180px;
height: 100%;
cursor: pointer;
}
@@ -440,6 +456,7 @@ const handleProductClick = (item: any) => {
height: 100%;
object-fit: cover;
border-radius: 16px;
object-fit: stretch;
}
/* 轮播指示器样式 */
@@ -465,10 +482,10 @@ const handleProductClick = (item: any) => {
}
.banner {
margin: 15px 20px;
width: 100%;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
border-radius: 20px;
height: 160px;
height: 100%;
padding: 20px;
display: flex;
justify-content: space-between;
+148 -105
View File
@@ -55,16 +55,19 @@
<div class="product-meta">
<div class="duration">
<el-icon><Clock /></el-icon>
<span>{{ product.estimatedTime }} 分钟</span>
<span>{{ product.approximateDuration || '0分钟' }}</span>
</div>
<div class="stats">
<span class="like-count">
<el-icon><Star /></el-icon>
0 赞
<span class="like-count" @click="handleLikeClick" style="cursor: pointer;">
<Icon
:icon="isLiked ? 'fa:heart' : 'fa:heart-o'"
:style="{ color: isLiked ? '#f56c6c' : 'inherit', fontSize: '16px' }"
/>
{{ product.likeCount || 0 }} 赞
</span>
<span class="collect-count">
<span class="collect-count" @click="handleFavoriteClick" style="cursor: pointer;">
<el-icon><Collection /></el-icon>
0 收藏
{{ product.collectCount || 0 }} 收藏
</span>
</div>
</div>
@@ -112,12 +115,14 @@
<script setup lang="ts">
import { ref, onMounted, inject } from 'vue'
import { ArrowLeft, Star, Clock, Collection, ShoppingCart, Right } from '@element-plus/icons-vue'
import { ArrowLeft, Star, Clock, Collection, ShoppingCart, Right, StarFilled } from '@element-plus/icons-vue'
import { Icon } from '@iconify/vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
import { productApi, type ProductVO } from '@/api/product'
import { cartApi } from '@/api/cart'
import { wishlistApi } from '@/api/wishlist'
import { productLikeApi } from '@/api/productLike'
import { fileApi } from '@/api/file'
// 注入更新购物车数量的方法
@@ -141,6 +146,7 @@ const product = ref<ProductVO>({
})
const isFavorite = ref(false)
const isLiked = ref(false)
const currentImageIndex = ref(0)
const productImages = ref<string[]>([])
const imageIdCache = ref<Record<string, string>>({})
@@ -156,6 +162,7 @@ const fetchProductDetail = async () => {
await loadProductImages()
await checkWishlistStatus()
await checkLikeStatus()
} catch (error) {
ElMessage.error('未登录,请先登录')
}
@@ -180,6 +187,25 @@ const checkWishlistStatus = async () => {
}
}
// 检查用户是否已点赞商品
const checkLikeStatus = async () => {
if (product.value.id) {
const token = localStorage.getItem('accessToken') || sessionStorage.getItem('accessToken')
if (!token) {
isLiked.value = false
return
}
try {
const status = await productLikeApi.checkLikeStatus(product.value.id)
isLiked.value = status
} catch (error) {
console.error('检查点赞状态失败:', error)
isLiked.value = false
}
}
}
// 加载商品图片
const loadProductImages = async () => {
if (product.value.imageFileIds) {
@@ -226,9 +252,21 @@ const handleFavoriteClick = async () => {
if (isFavorite.value) {
await wishlistApi.removeFromWishlist(product.value.id)
ElMessage.success('已取消收藏')
// 减少收藏数,确保不小于0
if (product.value.collectCount) {
product.value.collectCount = Math.max(0, product.value.collectCount - 1)
} else {
product.value.collectCount = 0
}
} else {
await wishlistApi.addToWishlist(product.value.id)
ElMessage.success('已收藏')
// 增加收藏数
if (product.value.collectCount) {
product.value.collectCount += 1
} else {
product.value.collectCount = 1
}
}
isFavorite.value = !isFavorite.value
} catch (error) {
@@ -237,6 +275,36 @@ const handleFavoriteClick = async () => {
}
}
const handleLikeClick = async () => {
if (!product.value.id) return
try {
if (isLiked.value) {
await productLikeApi.unlikeProduct(product.value.id)
ElMessage.success('已取消点赞')
// 减少点赞数,确保不小于0
if (product.value.likeCount) {
product.value.likeCount = Math.max(0, product.value.likeCount - 1)
} else {
product.value.likeCount = 0
}
} else {
await productLikeApi.likeProduct(product.value.id)
ElMessage.success('已点赞')
// 增加点赞数
if (product.value.likeCount) {
product.value.likeCount += 1
} else {
product.value.likeCount = 1
}
}
isLiked.value = !isLiked.value
} catch (error) {
console.error('操作点赞失败:', error)
ElMessage.error('操作失败,请重试')
}
}
const handleAddToCart = async () => {
try {
await cartApi.addToCart({ productId: product.value.id, quantity: 1 })
@@ -288,69 +356,59 @@ onMounted(() => {
height: 40px;
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
border-radius: 50%;
transition: background 0.3s;
}
.back-btn:hover {
background: #f5f5f5;
}
.header h1 {
font-size: 18px;
font-weight: bold;
margin: 0;
flex: 1;
text-align: center;
}
.actions {
width: 40px;
display: flex;
justify-content: center;
align-items: center;
justify-content: flex-end;
}
.actions .el-icon {
cursor: pointer;
transition: color 0.3s;
}
.actions .active {
color: #FFD700;
fill: #FFD700;
}
.actions .active svg {
fill: #FFD700;
.actions .el-icon.active {
color: #f56c6c;
}
.product-image-container {
width: 100%;
height: 300px;
background: white;
position: relative;
overflow: hidden;
background: white;
padding: 20px;
}
.image-carousel {
width: 100%;
height: 100%;
position: relative;
display: flex;
overflow-x: auto;
scroll-snap-type: x mandatory;
scrollbar-width: none;
}
.image-carousel::-webkit-scrollbar {
display: none;
overflow: hidden;
border-radius: 12px;
height: 300px;
}
.carousel-item {
flex: 0 0 auto;
width: 100%;
height: 100%;
scroll-snap-align: start;
cursor: pointer;
transition: opacity 0.3s;
min-width: 100%;
display: none;
}
.carousel-item.active {
opacity: 1;
}
.carousel-item:not(.active) {
opacity: 0.6;
display: block;
}
.product-image {
@@ -360,52 +418,51 @@ onMounted(() => {
}
.carousel-indicators {
position: absolute;
bottom: 12px;
left: 50%;
transform: translateX(-50%);
display: flex;
justify-content: center;
gap: 8px;
margin-top: 15px;
}
.indicator {
width: 8px;
height: 8px;
border-radius: 50%;
background: rgba(255, 255, 255, 0.5);
background: #ddd;
cursor: pointer;
transition: all 0.3s;
transition: background 0.3s;
}
.indicator.active {
background: white;
width: 24px;
border-radius: 4px;
background: #409eff;
}
.product-image-placeholder {
width: 100%;
height: 100%;
height: 300px;
display: flex;
justify-content: center;
align-items: center;
justify-content: center;
background: #f5f5f5;
border-radius: 12px;
}
.product-letter {
font-size: 80px;
font-weight: bold;
color: rgba(0,0,0,0.1);
background: #f8f8f8;
color: #ddd;
}
.product-info {
padding: 20px;
background: white;
margin-bottom: 10px;
padding: 20px;
margin-top: 10px;
}
.info-header {
display: flex;
justify-content: space-between;
align-items: flex-start;
margin-bottom: 12px;
margin-bottom: 10px;
}
.product-name {
@@ -413,106 +470,92 @@ onMounted(() => {
font-weight: bold;
margin: 0;
flex: 1;
margin-right: 15px;
margin-right: 10px;
}
.price-tag {
font-size: 22px;
color: #ff4d4f;
font-size: 24px;
font-weight: bold;
white-space: nowrap;
color: #f56c6c;
}
.shop-info {
margin-bottom: 12px;
margin-bottom: 15px;
}
.shop-name {
font-size: 14px;
color: #666;
background: #f8f8f8;
padding: 4px 12px;
border-radius: 12px;
}
.product-meta {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 16px;
padding: 12px;
background: #f8f8f8;
border-radius: 8px;
margin-bottom: 15px;
padding: 10px 0;
border-top: 1px solid #f0f0f0;
border-bottom: 1px solid #f0f0f0;
}
.duration {
display: flex;
align-items: center;
gap: 6px;
gap: 5px;
font-size: 14px;
color: #666;
}
.stats {
display: flex;
gap: 16px;
gap: 15px;
}
.like-count, .collect-count {
.like-count,
.collect-count {
display: flex;
align-items: center;
gap: 4px;
gap: 5px;
font-size: 14px;
color: #666;
}
.like-count .el-icon.active {
color: #f56c6c;
}
.tags {
margin-bottom: 16px;
display: flex;
flex-wrap: wrap;
gap: 8px;
margin-bottom: 15px;
}
.description {
font-size: 14px;
color: #666;
line-height: 1.6;
font-size: 14px;
margin: 0;
}
.action-buttons {
padding: 20px;
display: flex;
gap: 15px;
background: white;
margin-top: 10px;
position: sticky;
position: fixed;
bottom: 0;
z-index: 90;
left: 0;
right: 0;
display: flex;
gap: 10px;
padding: 15px 20px;
background: white;
border-top: 1px solid #f0f0f0;
}
.add-cart-btn, .buy-now-btn {
flex: 1;
height: 50px;
border-radius: 25px;
font-size: 16px;
font-weight: bold;
display: flex;
align-items: center;
justify-content: center;
gap: 8px;
}
.add-cart-btn {
background-color: #f5f5f5;
border: 1px solid #e8e8e8;
color: #333;
flex: 1;
height: 44px;
border-radius: 22px;
}
.buy-now-btn {
background-color: #ff4d4f;
border: none;
color: white;
flex: 1;
height: 44px;
border-radius: 22px;
}
</style>
</style>
+19 -4
View File
@@ -61,10 +61,10 @@
<div class="product-info">
<p class="name">{{ item.title || item.name }}</p>
<div class="product-meta">
<span class="duration">{{ item.estimatedTime }} 分钟</span>
<span class="duration">{{ item.approximateDuration || '0分钟' }}</span>
<div class="stats">
<span class="like-count">0 赞</span>
<span class="collect-count">0 收藏</span>
<span class="like-count">{{ item.likeCount || 0 }} 赞</span>
<span class="collect-count">{{ item.collectCount || 0 }} 收藏</span>
</div>
</div>
<!-- 标签展示 -->
@@ -146,7 +146,7 @@
</template>
<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { ref, onMounted, computed } from 'vue'
import { useRouter } from 'vue-router'
import {
LeftOutlined,
@@ -160,6 +160,21 @@ import { tagApi, type TagVO } from '@/api/tag'
import { cartApi } from '@/api/cart'
import { fileApi } from '@/api/file'
// 格式化分钟数显示
const formatDuration = (minutes: number): string => {
if (minutes < 60) {
return `${minutes} 分钟`
} else {
const hours = Math.floor(minutes / 60)
const remainingMinutes = minutes % 60
if (remainingMinutes === 0) {
return `${hours} 小时`
} else {
return `${hours} 小时 ${remainingMinutes} 分钟`
}
}
}
const router = useRouter()
// 标签列表