Files
JayuCloud/Frontend/src/views/ProductDetailView.vue
T

562 lines
13 KiB
Vue
Raw Normal View History

<template>
<div class="mobile-container">
<!-- Header -->
<header class="header">
<div class="back-btn" @click="handleBackClick">
<el-icon><ArrowLeft /></el-icon>
</div>
2026-03-02 10:13:20 +08:00
<h1>{{ product.name || '商品详情' }}</h1>
<div class="actions">
2026-02-24 09:59:22 +08:00
<el-icon @click="handleFavoriteClick" :style="{ fontSize: '20px' }" :class="{ active: isFavorite }"><Star /></el-icon>
</div>
</header>
2026-02-26 11:16:24 +08:00
<!-- Product Image Carousel -->
2026-02-24 09:59:22 +08:00
<div class="product-image-container">
2026-02-26 11:16:24 +08:00
<div v-if="productImages.length > 0" class="image-carousel">
<div
2026-03-02 10:13:20 +08:00
v-for="(image, index) in productImages"
2026-02-26 11:16:24 +08:00
:key="index"
class="carousel-item"
:class="{ active: currentImageIndex === index }"
@click="handleImageClick(index)"
>
<img
2026-03-02 10:13:20 +08:00
:src="image"
:alt="product.name"
2026-02-26 11:16:24 +08:00
class="product-image"
/>
</div>
</div>
<div v-if="productImages.length > 1" class="carousel-indicators">
<div
2026-03-02 10:13:20 +08:00
v-for="(image, index) in productImages"
2026-02-26 11:16:24 +08:00
:key="index"
class="indicator"
:class="{ active: currentImageIndex === index }"
@click="handleImageClick(index)"
></div>
</div>
2026-02-24 09:59:22 +08:00
<div v-else class="product-image-placeholder">
2026-03-02 10:13:20 +08:00
<span class="product-letter">{{ product.name ? product.name[0] : '商' }}</span>
2026-02-24 09:59:22 +08:00
</div>
</div>
<!-- Product Info -->
<div class="product-info">
<div class="info-header">
2026-03-02 10:13:20 +08:00
<h2 class="product-name">{{ product.name }}</h2>
2026-02-24 09:59:22 +08:00
<div class="price-tag">¥{{ product.price }}</div>
</div>
2026-02-24 09:59:22 +08:00
<div class="shop-info">
2026-03-02 10:13:20 +08:00
<span class="shop-name">店铺</span>
2026-02-24 09:59:22 +08:00
</div>
<div class="product-meta">
<div class="duration">
<el-icon><Clock /></el-icon>
2026-03-05 15:15:59 +08:00
<span>{{ product.approximateDuration || '0分钟' }}</span>
2026-02-24 09:59:22 +08:00
</div>
<div class="stats">
2026-03-05 15:15:59 +08:00
<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 }} 赞
2026-02-24 09:59:22 +08:00
</span>
2026-03-05 15:15:59 +08:00
<span class="collect-count" @click="handleFavoriteClick" style="cursor: pointer;">
2026-02-24 09:59:22 +08:00
<el-icon><Collection /></el-icon>
2026-03-05 15:15:59 +08:00
{{ product.collectCount || 0 }} 收藏
2026-02-24 09:59:22 +08:00
</span>
</div>
</div>
<div class="tags">
<el-tag
v-for="tag in product.tagInfos"
:key="tag.id"
size="small"
effect="light"
:style="{
marginRight: '8px',
marginBottom: '8px',
color: tag.textColor || '#409eff',
backgroundColor: tag.bgColor || '#ecf5ff',
borderColor: tag.borderColor || '#409eff'
}"
>
{{ tag.tagName }}
</el-tag>
<el-tag
v-if="!product.tagInfos || product.tagInfos.length === 0"
size="small"
effect="light"
style="margin-right: 8px; margin-bottom: 8px"
>
2026-03-02 10:13:20 +08:00
{{ product.category }}
2026-02-24 09:59:22 +08:00
</el-tag>
</div>
<p class="description">{{ product.description }}</p>
</div>
<!-- Action Buttons -->
<div class="action-buttons">
<el-button class="add-cart-btn" @click="handleAddToCart">
2026-02-24 09:59:22 +08:00
<el-icon><ShoppingCart /></el-icon>
添加到购物车
</el-button>
<el-button class="buy-now-btn" type="primary" @click="handleBuyNow">
2026-02-24 09:59:22 +08:00
<el-icon><Right /></el-icon>
立即购买
</el-button>
</div>
</div>
</template>
<script setup lang="ts">
2026-02-28 17:09:45 +08:00
import { ref, onMounted, inject } from 'vue'
2026-03-05 15:15:59 +08:00
import { ArrowLeft, Star, Clock, Collection, ShoppingCart, Right, StarFilled } from '@element-plus/icons-vue'
import { Icon } from '@iconify/vue'
import { useRouter, useRoute } from 'vue-router'
2026-02-24 09:59:22 +08:00
import { ElMessage } from 'element-plus'
import { productApi, type ProductVO } from '@/api/product'
import { cartApi } from '@/api/cart'
import { wishlistApi } from '@/api/wishlist'
2026-03-05 15:15:59 +08:00
import { productLikeApi } from '@/api/productLike'
import { fileApi } from '@/api/file'
2026-02-28 17:09:45 +08:00
// 注入更新购物车数量的方法
const updateCartCount = inject<() => Promise<void>>('updateCartCount', async () => {})
const router = useRouter()
const route = useRoute()
2026-02-24 09:59:22 +08:00
const product = ref<ProductVO>({
id: 0,
2026-03-02 10:13:20 +08:00
shopId: null,
name: '',
2026-02-24 09:59:22 +08:00
description: '',
2026-03-02 10:13:20 +08:00
image: '',
2026-02-24 09:59:22 +08:00
price: 0,
2026-03-02 10:13:20 +08:00
estimatedTime: 0,
category: '',
status: 0,
2026-02-24 09:59:22 +08:00
createTime: '',
updateTime: ''
})
2026-02-24 09:59:22 +08:00
const isFavorite = ref(false)
2026-03-05 15:15:59 +08:00
const isLiked = ref(false)
2026-02-26 11:16:24 +08:00
const currentImageIndex = ref(0)
const productImages = ref<string[]>([])
const imageIdCache = ref<Record<string, string>>({})
const DEFAULT_PRODUCT_IMAGE = 'data:image/svg+xml;base64,PHN2ZyB3aW0dPSJodHRwOi8vd3d3Lm9yZy8yMDAwL3N2ZyI+Pjwvc3ZnPg=='
2026-02-24 09:59:22 +08:00
// 从后端获取商品详情
const fetchProductDetail = async () => {
const productId = route.params.id
2026-02-24 09:59:22 +08:00
try {
2026-03-02 10:13:20 +08:00
const response = await productApi.getProductById(Number(productId)) as any
product.value = response || {}
2026-02-24 09:59:22 +08:00
2026-02-26 11:16:24 +08:00
await loadProductImages()
await checkWishlistStatus()
2026-03-05 15:15:59 +08:00
await checkLikeStatus()
2026-02-24 09:59:22 +08:00
} catch (error) {
2026-02-24 10:57:00 +08:00
ElMessage.error('未登录,请先登录')
2026-02-24 09:59:22 +08:00
}
}
// 检查商品是否在收藏列表中
const checkWishlistStatus = async () => {
if (product.value.id) {
const token = localStorage.getItem('accessToken') || sessionStorage.getItem('accessToken')
if (!token) {
isFavorite.value = false
return
}
try {
const status = await wishlistApi.checkWishlist(product.value.id)
isFavorite.value = status
} catch (error) {
console.error('检查收藏状态失败:', error)
isFavorite.value = false
}
}
}
2026-03-05 15:15:59 +08:00
// 检查用户是否已点赞商品
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
}
}
}
2026-02-26 11:16:24 +08:00
// 加载商品图片
const loadProductImages = async () => {
if (product.value.imageFileIds) {
const ids = product.value.imageFileIds.split(',')
const urls: string[] = []
for (const id of ids) {
if (id) {
try {
if (!imageIdCache.value[id]) {
const response = await fileApi.getSignedUrl(Number(id)) as any
imageIdCache.value[id] = response
}
urls.push(imageIdCache.value[id])
} catch (error) {
console.error('获取签名链接失败:', error)
urls.push(DEFAULT_PRODUCT_IMAGE)
}
}
}
if (urls.length > 0) {
productImages.value = urls
currentImageIndex.value = 0
}
} else {
productImages.value = [DEFAULT_PRODUCT_IMAGE]
2026-02-26 11:16:24 +08:00
}
}
// 处理图片点击
const handleImageClick = (index: number) => {
currentImageIndex.value = index
}
const handleBackClick = () => {
router.back()
}
const handleFavoriteClick = async () => {
if (!product.value.id) return
try {
if (isFavorite.value) {
await wishlistApi.removeFromWishlist(product.value.id)
ElMessage.success('已取消收藏')
2026-03-05 15:15:59 +08:00
// 减少收藏数,确保不小于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('已收藏')
2026-03-05 15:15:59 +08:00
// 增加收藏数
if (product.value.collectCount) {
product.value.collectCount += 1
} else {
product.value.collectCount = 1
}
}
isFavorite.value = !isFavorite.value
} catch (error) {
console.error('操作收藏失败:', error)
ElMessage.error('操作失败,请重试')
}
}
2026-03-05 15:15:59 +08:00
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 })
2026-02-28 17:09:45 +08:00
await updateCartCount()
ElMessage.success(`已将 ${product.value.title || product.value.name || '商品'} 添加到购物车`)
} catch (error) {
console.error('添加到购物车失败:', error)
ElMessage.error('添加到购物车失败,请重试')
}
}
const handleBuyNow = async () => {
try {
await cartApi.addToCart({ productId: product.value.id, quantity: 1 })
2026-02-28 17:09:45 +08:00
await updateCartCount()
router.push('/cart')
} catch (error) {
console.error('立即购买失败:', error)
ElMessage.error('操作失败,请重试')
}
}
2026-02-24 09:59:22 +08:00
// 页面挂载时获取商品详情
onMounted(() => {
fetchProductDetail()
})
</script>
<style scoped>
2026-02-24 09:59:22 +08:00
.mobile-container {
min-height: 100vh;
background: #f5f5f5;
}
.header {
display: flex;
justify-content: space-between;
align-items: center;
padding: 15px 20px;
border-bottom: 1px solid #f0f0f0;
2026-02-24 09:59:22 +08:00
background: white;
position: sticky;
top: 0;
z-index: 100;
}
2026-02-24 09:59:22 +08:00
.back-btn {
width: 40px;
height: 40px;
display: flex;
align-items: center;
2026-03-05 15:15:59 +08:00
justify-content: center;
cursor: pointer;
2026-03-05 15:15:59 +08:00
border-radius: 50%;
transition: background 0.3s;
}
.back-btn:hover {
background: #f5f5f5;
}
2026-02-24 09:59:22 +08:00
.header h1 {
font-size: 18px;
font-weight: bold;
2026-02-24 09:59:22 +08:00
margin: 0;
2026-03-05 15:15:59 +08:00
flex: 1;
text-align: center;
}
2026-02-24 09:59:22 +08:00
.actions {
width: 40px;
display: flex;
2026-03-05 15:15:59 +08:00
justify-content: flex-end;
}
.actions .el-icon {
cursor: pointer;
2026-03-05 15:15:59 +08:00
transition: color 0.3s;
}
2026-03-05 15:15:59 +08:00
.actions .el-icon.active {
color: #f56c6c;
2026-02-24 09:59:22 +08:00
}
.product-image-container {
position: relative;
2026-03-05 15:15:59 +08:00
background: white;
padding: 20px;
2026-02-26 11:16:24 +08:00
}
.image-carousel {
display: flex;
2026-03-05 15:15:59 +08:00
overflow: hidden;
border-radius: 12px;
height: 300px;
2026-02-26 11:16:24 +08:00
}
.carousel-item {
2026-03-05 15:15:59 +08:00
min-width: 100%;
display: none;
2026-02-26 11:16:24 +08:00
}
.carousel-item.active {
2026-03-05 15:15:59 +08:00
display: block;
2026-02-24 09:59:22 +08:00
}
.product-image {
width: 100%;
height: 100%;
object-fit: cover;
}
2026-02-26 11:16:24 +08:00
.carousel-indicators {
display: flex;
2026-03-05 15:15:59 +08:00
justify-content: center;
2026-02-26 11:16:24 +08:00
gap: 8px;
2026-03-05 15:15:59 +08:00
margin-top: 15px;
2026-02-26 11:16:24 +08:00
}
.indicator {
width: 8px;
height: 8px;
border-radius: 50%;
2026-03-05 15:15:59 +08:00
background: #ddd;
2026-02-26 11:16:24 +08:00
cursor: pointer;
2026-03-05 15:15:59 +08:00
transition: background 0.3s;
2026-02-26 11:16:24 +08:00
}
.indicator.active {
2026-03-05 15:15:59 +08:00
background: #409eff;
2026-02-26 11:16:24 +08:00
}
2026-02-24 09:59:22 +08:00
.product-image-placeholder {
2026-03-05 15:15:59 +08:00
height: 300px;
display: flex;
align-items: center;
2026-03-05 15:15:59 +08:00
justify-content: center;
background: #f5f5f5;
border-radius: 12px;
}
.product-letter {
font-size: 80px;
font-weight: bold;
2026-03-05 15:15:59 +08:00
color: #ddd;
}
.product-info {
2026-02-24 09:59:22 +08:00
background: white;
2026-03-05 15:15:59 +08:00
padding: 20px;
margin-top: 10px;
}
2026-02-24 09:59:22 +08:00
.info-header {
display: flex;
justify-content: space-between;
2026-02-24 09:59:22 +08:00
align-items: flex-start;
2026-03-05 15:15:59 +08:00
margin-bottom: 10px;
}
2026-02-24 09:59:22 +08:00
.product-name {
2026-02-24 09:59:22 +08:00
font-size: 20px;
font-weight: bold;
margin: 0;
2026-02-24 09:59:22 +08:00
flex: 1;
2026-03-05 15:15:59 +08:00
margin-right: 10px;
}
2026-02-24 09:59:22 +08:00
.price-tag {
2026-03-05 15:15:59 +08:00
font-size: 24px;
font-weight: bold;
2026-03-05 15:15:59 +08:00
color: #f56c6c;
}
2026-02-24 09:59:22 +08:00
.shop-info {
2026-03-05 15:15:59 +08:00
margin-bottom: 15px;
}
2026-02-24 09:59:22 +08:00
.shop-name {
font-size: 14px;
color: #666;
}
.product-meta {
display: flex;
justify-content: space-between;
align-items: center;
2026-03-05 15:15:59 +08:00
margin-bottom: 15px;
padding: 10px 0;
border-top: 1px solid #f0f0f0;
border-bottom: 1px solid #f0f0f0;
2026-02-24 09:59:22 +08:00
}
.duration {
display: flex;
align-items: center;
2026-03-05 15:15:59 +08:00
gap: 5px;
2026-02-24 09:59:22 +08:00
font-size: 14px;
color: #666;
}
.stats {
display: flex;
2026-03-05 15:15:59 +08:00
gap: 15px;
2026-02-24 09:59:22 +08:00
}
2026-03-05 15:15:59 +08:00
.like-count,
.collect-count {
2026-02-24 09:59:22 +08:00
display: flex;
align-items: center;
2026-03-05 15:15:59 +08:00
gap: 5px;
2026-02-24 09:59:22 +08:00
font-size: 14px;
color: #666;
}
2026-03-05 15:15:59 +08:00
.like-count .el-icon.active {
color: #f56c6c;
}
2026-02-24 09:59:22 +08:00
.tags {
2026-03-05 15:15:59 +08:00
margin-bottom: 15px;
2026-02-24 09:59:22 +08:00
}
.description {
2026-03-05 15:15:59 +08:00
font-size: 14px;
color: #666;
line-height: 1.6;
2026-02-24 09:59:22 +08:00
margin: 0;
}
.action-buttons {
2026-03-05 15:15:59 +08:00
position: fixed;
2026-02-24 09:59:22 +08:00
bottom: 0;
2026-03-05 15:15:59 +08:00
left: 0;
right: 0;
display: flex;
gap: 10px;
padding: 15px 20px;
background: white;
2026-02-24 09:59:22 +08:00
border-top: 1px solid #f0f0f0;
}
2026-02-24 09:59:22 +08:00
.add-cart-btn {
2026-03-05 15:15:59 +08:00
flex: 1;
height: 44px;
border-radius: 22px;
}
2026-02-24 09:59:22 +08:00
.buy-now-btn {
2026-03-05 15:15:59 +08:00
flex: 1;
height: 44px;
border-radius: 22px;
}
2026-03-05 15:15:59 +08:00
</style>