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

518 lines
11 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-02 10:13:20 +08:00
<span>{{ product.estimatedTime }} 分钟</span>
2026-02-24 09:59:22 +08:00
</div>
<div class="stats">
<span class="like-count">
<el-icon><Star /></el-icon>
2026-03-02 10:13:20 +08:00
0 赞
2026-02-24 09:59:22 +08:00
</span>
<span class="collect-count">
<el-icon><Collection /></el-icon>
2026-03-02 10:13:20 +08:00
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-02-24 09:59:22 +08:00
import { ArrowLeft, Star, Clock, Collection, ShoppingCart, Right } from '@element-plus/icons-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'
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-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-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-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('已取消收藏')
} else {
await wishlistApi.addToWishlist(product.value.id)
ElMessage.success('已收藏')
}
isFavorite.value = !isFavorite.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;
cursor: pointer;
}
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-02-24 09:59:22 +08:00
.actions {
width: 40px;
display: flex;
justify-content: center;
align-items: center;
cursor: pointer;
}
2026-02-24 09:59:22 +08:00
.actions .active {
color: #FFD700;
fill: #FFD700;
}
.actions .active svg {
fill: #FFD700;
2026-02-24 09:59:22 +08:00
}
.product-image-container {
width: 100%;
height: 300px;
2026-02-24 09:59:22 +08:00
background: white;
position: relative;
2026-02-26 11:16:24 +08:00
overflow: hidden;
}
.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;
}
.carousel-item {
flex: 0 0 auto;
width: 100%;
height: 100%;
scroll-snap-align: start;
cursor: pointer;
transition: opacity 0.3s;
}
.carousel-item.active {
opacity: 1;
}
.carousel-item:not(.active) {
opacity: 0.6;
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 {
position: absolute;
bottom: 12px;
left: 50%;
transform: translateX(-50%);
display: flex;
gap: 8px;
}
.indicator {
width: 8px;
height: 8px;
border-radius: 50%;
background: rgba(255, 255, 255, 0.5);
cursor: pointer;
transition: all 0.3s;
}
.indicator.active {
background: white;
width: 24px;
border-radius: 4px;
}
2026-02-24 09:59:22 +08:00
.product-image-placeholder {
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
font-size: 80px;
font-weight: bold;
color: rgba(0,0,0,0.1);
2026-02-24 09:59:22 +08:00
background: #f8f8f8;
}
.product-info {
padding: 20px;
2026-02-24 09:59:22 +08:00
background: white;
margin-bottom: 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;
margin-bottom: 12px;
}
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;
margin-right: 15px;
}
2026-02-24 09:59:22 +08:00
.price-tag {
2026-02-24 09:59:22 +08:00
font-size: 22px;
color: #ff4d4f;
font-weight: bold;
2026-02-24 09:59:22 +08:00
white-space: nowrap;
}
2026-02-24 09:59:22 +08:00
.shop-info {
margin-bottom: 12px;
}
2026-02-24 09:59:22 +08:00
.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;
}
.duration {
display: flex;
align-items: center;
gap: 6px;
font-size: 14px;
color: #666;
}
.stats {
display: flex;
gap: 16px;
}
.like-count, .collect-count {
display: flex;
align-items: center;
gap: 4px;
font-size: 14px;
color: #666;
}
.tags {
margin-bottom: 16px;
display: flex;
flex-wrap: wrap;
gap: 8px;
}
.description {
color: #666;
line-height: 1.6;
2026-02-24 09:59:22 +08:00
font-size: 14px;
margin: 0;
}
.action-buttons {
padding: 20px;
display: flex;
gap: 15px;
2026-02-24 09:59:22 +08:00
background: white;
margin-top: 10px;
position: sticky;
bottom: 0;
z-index: 90;
border-top: 1px solid #f0f0f0;
}
2026-02-24 09:59:22 +08:00
.add-cart-btn, .buy-now-btn {
flex: 1;
height: 50px;
border-radius: 25px;
font-size: 16px;
font-weight: bold;
2026-02-24 09:59:22 +08:00
display: flex;
align-items: center;
justify-content: center;
gap: 8px;
}
2026-02-24 09:59:22 +08:00
.add-cart-btn {
background-color: #f5f5f5;
2026-02-24 09:59:22 +08:00
border: 1px solid #e8e8e8;
color: #333;
}
2026-02-24 09:59:22 +08:00
.buy-now-btn {
2026-02-24 09:59:22 +08:00
background-color: #ff4d4f;
border: none;
2026-02-24 09:59:22 +08:00
color: white;
}
</style>