564 lines
13 KiB
Vue
564 lines
13 KiB
Vue
<template>
|
|
<div class="mobile-container">
|
|
|
|
<!-- Header -->
|
|
<header class="header">
|
|
<div class="back-btn" @click="handleBackClick">
|
|
<el-icon><ArrowLeft /></el-icon>
|
|
</div>
|
|
<h1>{{ product.name || '作品详情' }}</h1>
|
|
<div class="actions">
|
|
<el-icon @click="handleFavoriteClick" :style="{ fontSize: '20px' }" :class="{ active: isFavorite }"><Star /></el-icon>
|
|
</div>
|
|
</header>
|
|
|
|
<!-- Product Image Carousel -->
|
|
<div class="product-image-container">
|
|
<div v-if="productImages.length > 0" class="image-carousel">
|
|
<div
|
|
v-for="(image, index) in productImages"
|
|
:key="index"
|
|
class="carousel-item"
|
|
:class="{ active: currentImageIndex === index }"
|
|
@click="handleImageClick(index)"
|
|
>
|
|
<img
|
|
:src="image"
|
|
:alt="product.name"
|
|
class="product-image"
|
|
/>
|
|
</div>
|
|
</div>
|
|
<div v-if="productImages.length > 1" class="carousel-indicators">
|
|
<div
|
|
v-for="(_, index) in productImages"
|
|
:key="index"
|
|
class="indicator"
|
|
:class="{ active: currentImageIndex === index }"
|
|
@click="handleImageClick(index)"
|
|
></div>
|
|
</div>
|
|
<div v-else class="product-image-placeholder">
|
|
<span class="product-letter">{{ product.name ? product.name[0] : '商' }}</span>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Product Info -->
|
|
<div class="product-info">
|
|
<div class="info-header">
|
|
<h2 class="product-name">{{ product.name }}</h2>
|
|
<div class="price-tag">¥{{ product.price }}</div>
|
|
</div>
|
|
<div class="shop-info">
|
|
<span class="shop-name">店铺</span>
|
|
</div>
|
|
<div class="product-meta">
|
|
<div class="duration">
|
|
<el-icon><Clock /></el-icon>
|
|
<span>{{ product.approximateDuration || '0分钟' }}</span>
|
|
</div>
|
|
<div class="stats">
|
|
<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" @click="handleFavoriteClick" style="cursor: pointer;">
|
|
<el-icon><Collection /></el-icon>
|
|
{{ product.collectCount || 0 }} 收藏
|
|
</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"
|
|
>
|
|
{{ product.category }}
|
|
</el-tag>
|
|
</div>
|
|
<p class="description">{{ product.description }}</p>
|
|
</div>
|
|
|
|
<!-- Action Buttons -->
|
|
<div class="action-buttons">
|
|
<el-button class="add-cart-btn" @click="handleAddToCart">
|
|
<el-icon><ShoppingCart /></el-icon>
|
|
添加到购物车
|
|
</el-button>
|
|
<el-button class="buy-now-btn" type="primary" @click="handleBuyNow">
|
|
<el-icon><Right /></el-icon>
|
|
立即购买
|
|
</el-button>
|
|
</div>
|
|
</div>
|
|
</template>
|
|
|
|
<script setup lang="ts">
|
|
import { ref, onMounted, inject } from 'vue'
|
|
import { ArrowLeft, Star, Clock, Collection, ShoppingCart, Right } 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'
|
|
|
|
// 注入更新购物车数量的方法
|
|
const updateCartCount = inject<() => Promise<void>>('updateCartCount', async () => {})
|
|
|
|
const router = useRouter()
|
|
const route = useRoute()
|
|
|
|
const product = ref<ProductVO>({
|
|
id: 0,
|
|
shopId: null,
|
|
name: '',
|
|
description: '',
|
|
image: '',
|
|
price: 0,
|
|
estimatedTime: 0,
|
|
category: '',
|
|
status: 0,
|
|
createTime: '',
|
|
updateTime: ''
|
|
})
|
|
|
|
const isFavorite = ref(false)
|
|
const isLiked = ref(false)
|
|
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=='
|
|
|
|
// 从后端获取作品详情
|
|
const fetchProductDetail = async () => {
|
|
const productId = route.params.id
|
|
|
|
try {
|
|
const response = await productApi.getProductById(Number(productId)) as any
|
|
product.value = response || {}
|
|
|
|
await loadProductImages()
|
|
await checkWishlistStatus()
|
|
await checkLikeStatus()
|
|
} catch (error) {
|
|
ElMessage.error('未登录,请先登录')
|
|
}
|
|
}
|
|
|
|
// 检查作品是否在收藏列表中
|
|
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 as unknown as boolean)
|
|
} catch (error) {
|
|
console.error('检查收藏状态失败:', error)
|
|
isFavorite.value = false
|
|
}
|
|
}
|
|
}
|
|
|
|
// 检查用户是否已点赞作品
|
|
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 as unknown as boolean)
|
|
} catch (error) {
|
|
console.error('检查点赞状态失败:', error)
|
|
isLiked.value = false
|
|
}
|
|
}
|
|
}
|
|
|
|
// 加载作品图片
|
|
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
|
|
}
|
|
if (imageIdCache.value[id]) {
|
|
urls.push(imageIdCache.value[id] as string)
|
|
}
|
|
} 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]
|
|
}
|
|
}
|
|
|
|
// 处理图片点击
|
|
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('已取消收藏')
|
|
// 减少收藏数,确保不小于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) {
|
|
console.error('操作收藏失败:', error)
|
|
ElMessage.error('操作失败,请重试')
|
|
}
|
|
}
|
|
|
|
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 })
|
|
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 })
|
|
await updateCartCount()
|
|
router.push('/cart')
|
|
} catch (error) {
|
|
console.error('立即购买失败:', error)
|
|
ElMessage.error('操作失败,请重试')
|
|
}
|
|
}
|
|
|
|
// 页面挂载时获取作品详情
|
|
onMounted(() => {
|
|
fetchProductDetail()
|
|
})
|
|
</script>
|
|
|
|
<style scoped>
|
|
.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;
|
|
background: white;
|
|
position: sticky;
|
|
top: 0;
|
|
z-index: 100;
|
|
}
|
|
|
|
.back-btn {
|
|
width: 40px;
|
|
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: flex-end;
|
|
}
|
|
|
|
.actions .el-icon {
|
|
cursor: pointer;
|
|
transition: color 0.3s;
|
|
}
|
|
|
|
.actions .el-icon.active {
|
|
color: #f56c6c;
|
|
}
|
|
|
|
.product-image-container {
|
|
position: relative;
|
|
background: white;
|
|
padding: 20px;
|
|
}
|
|
|
|
.image-carousel {
|
|
display: flex;
|
|
overflow: hidden;
|
|
border-radius: 12px;
|
|
height: 300px;
|
|
}
|
|
|
|
.carousel-item {
|
|
min-width: 100%;
|
|
display: none;
|
|
}
|
|
|
|
.carousel-item.active {
|
|
display: block;
|
|
}
|
|
|
|
.product-image {
|
|
width: 100%;
|
|
height: 100%;
|
|
object-fit: cover;
|
|
}
|
|
|
|
.carousel-indicators {
|
|
display: flex;
|
|
justify-content: center;
|
|
gap: 8px;
|
|
margin-top: 15px;
|
|
}
|
|
|
|
.indicator {
|
|
width: 8px;
|
|
height: 8px;
|
|
border-radius: 50%;
|
|
background: #ddd;
|
|
cursor: pointer;
|
|
transition: background 0.3s;
|
|
}
|
|
|
|
.indicator.active {
|
|
background: #409eff;
|
|
}
|
|
|
|
.product-image-placeholder {
|
|
height: 300px;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
background: #f5f5f5;
|
|
border-radius: 12px;
|
|
}
|
|
|
|
.product-letter {
|
|
font-size: 80px;
|
|
font-weight: bold;
|
|
color: #ddd;
|
|
}
|
|
|
|
.product-info {
|
|
background: white;
|
|
padding: 20px;
|
|
margin-top: 10px;
|
|
}
|
|
|
|
.info-header {
|
|
display: flex;
|
|
justify-content: space-between;
|
|
align-items: flex-start;
|
|
margin-bottom: 10px;
|
|
}
|
|
|
|
.product-name {
|
|
font-size: 20px;
|
|
font-weight: bold;
|
|
margin: 0;
|
|
flex: 1;
|
|
margin-right: 10px;
|
|
}
|
|
|
|
.price-tag {
|
|
font-size: 24px;
|
|
font-weight: bold;
|
|
color: #f56c6c;
|
|
}
|
|
|
|
.shop-info {
|
|
margin-bottom: 15px;
|
|
}
|
|
|
|
.shop-name {
|
|
font-size: 14px;
|
|
color: #666;
|
|
}
|
|
|
|
.product-meta {
|
|
display: flex;
|
|
justify-content: space-between;
|
|
align-items: center;
|
|
margin-bottom: 15px;
|
|
padding: 10px 0;
|
|
border-top: 1px solid #f0f0f0;
|
|
border-bottom: 1px solid #f0f0f0;
|
|
}
|
|
|
|
.duration {
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 5px;
|
|
font-size: 14px;
|
|
color: #666;
|
|
}
|
|
|
|
.stats {
|
|
display: flex;
|
|
gap: 15px;
|
|
}
|
|
|
|
.like-count,
|
|
.collect-count {
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 5px;
|
|
font-size: 14px;
|
|
color: #666;
|
|
}
|
|
|
|
.like-count .el-icon.active {
|
|
color: #f56c6c;
|
|
}
|
|
|
|
.tags {
|
|
margin-bottom: 15px;
|
|
}
|
|
|
|
.description {
|
|
font-size: 14px;
|
|
color: #666;
|
|
line-height: 1.6;
|
|
margin: 0;
|
|
}
|
|
|
|
.action-buttons {
|
|
position: fixed;
|
|
bottom: 0;
|
|
left: 0;
|
|
right: 0;
|
|
display: flex;
|
|
gap: 10px;
|
|
padding: 15px 20px;
|
|
background: white;
|
|
border-top: 1px solid #f0f0f0;
|
|
}
|
|
|
|
.add-cart-btn {
|
|
flex: 1;
|
|
height: 44px;
|
|
border-radius: 22px;
|
|
}
|
|
|
|
.buy-now-btn {
|
|
flex: 1;
|
|
height: 44px;
|
|
border-radius: 22px;
|
|
}
|
|
</style>
|