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

265 lines
6.6 KiB
Vue

<template>
<div class="mobile-container">
<!-- Header -->
<header class="header">
<div class="back-btn" @click="handleBackClick">
<el-icon><ArrowLeft /></el-icon>
</div>
<h1>我的收藏</h1>
<div class="empty"></div>
</header>
<!-- Wishlist Items -->
<div class="wishlist-items">
<div v-for="item in wishlistItems" :key="item.id" class="wishlist-item">
<div class="item-image" @click="handleProductClick(item.product.id)">
<img v-if="getProductImages(item.product) && getProductImages(item.product).length > 0"
:src="getProductImages(item.product)[0]"
:alt="item.product.title" />
<div v-else class="image-placeholder" :style="{ backgroundColor: getRandomColor() }">
<span>{{ item.product.title ? item.product.title[0] : '商' }}</span>
</div>
</div>
<div class="item-details">
<h3 class="item-name" @click="handleProductClick(item.product.id)">{{ item.product.title }}</h3>
<p class="item-price">¥{{ item.product.price }}</p>
<div class="item-actions">
<el-button size="small" type="primary" @click="handleAddToCart(item.product.id)">
添加到购物车
</el-button>
<el-icon class="delete-icon" @click="handleRemoveItem(item.product.id)"><Delete /></el-icon>
</div>
</div>
</div>
</div>
<!-- Empty Wishlist Message -->
<div v-if="wishlistItems.length === 0" class="empty-wishlist">
<el-icon :size="60" :style="{ color: '#fadb14' }"><StarFilled /></el-icon>
<p>收藏列表为空</p>
<el-button type="primary" @click="handleBrowseProducts">
浏览作品
</el-button>
</div>
</div>
</template>
<script setup lang="ts">
import { ref, onMounted, inject } from 'vue'
import { ArrowLeft, Delete, StarFilled } from '@element-plus/icons-vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { wishlistApi, type WishlistItem } from '@/api/wishlist'
import { cartApi } from '@/api/cart'
import { fileApi } from '@/api/file'
import type { IdType } from '@/utils/id'
import { sameId } from '@/utils/id'
const router = useRouter()
const updateCartCount = inject<() => Promise<void>>('updateCartCount', async () => {})
const wishlistItems = ref<WishlistItem[]>([])
const imageIdCache = ref<Record<string, string>>({})
const imageUrls = ref<Record<string, string[]>>({})
const DEFAULT_PRODUCT_IMAGE = 'data:image/svg+xml;base64,PHN2ZyB3aW0dPSJodHRwOi8vd3d3Lm9yZy8yMDAwL3N2ZyI+Pjwvc3ZnPg=='
const handleBackClick = () => {
router.back()
}
const fetchWishlist = async () => {
try {
const response = await wishlistApi.getWishlist()
wishlistItems.value = (response as unknown as WishlistItem[])
await loadImageUrls(response as unknown as WishlistItem[])
} catch (error) {
console.error('获取收藏列表失败:', error)
ElMessage.error('获取收藏列表失败')
}
}
const loadImageUrls = async (items: WishlistItem[]) => {
for (const item of items) {
if (item.product.imageFileIds) {
const ids = item.product.imageFileIds.split(',')
const urls: string[] = []
for (const id of ids) {
if (id) {
try {
if (!imageIdCache.value[id]) {
const response = await fileApi.getSignedUrl(id.trim()) 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)
}
}
}
imageUrls.value[String(item.product.id)] = urls
} else {
imageUrls.value[String(item.product.id)] = [DEFAULT_PRODUCT_IMAGE]
}
}
}
const getProductImages = (product: any): string[] => {
return imageUrls.value[String(product.id)] || [DEFAULT_PRODUCT_IMAGE]
}
const handleAddToCart = async (productId: IdType) => {
try {
await cartApi.addToCart({ productId, quantity: 1 })
await updateCartCount()
ElMessage.success('已添加到购物车')
} catch (error) {
console.error('添加到购物车失败:', error)
ElMessage.error('添加到购物车失败')
}
}
const handleRemoveItem = async (productId: IdType) => {
try {
await wishlistApi.removeFromWishlist(productId)
wishlistItems.value = wishlistItems.value.filter(item => !sameId(item.productId, productId))
ElMessage.success('已从收藏中移除')
} catch (error) {
console.error('移除收藏失败:', error)
ElMessage.error('移除收藏失败')
}
}
const handleBrowseProducts = () => {
router.push('/products')
}
const handleProductClick = (productId: IdType) => {
router.push(`/product/${productId}`)
}
const getRandomColor = () => {
const colors = ['#ffe4d6', '#d6eaff', '#e2ffd6', '#fff2d6', '#d6ffe4', '#e4d6ff']
return colors[Math.floor(Math.random() * colors.length)]
}
onMounted(() => {
fetchWishlist()
})
</script>
<style scoped>
.header {
display: flex;
justify-content: space-between;
align-items: center;
padding: 15px 20px;
border-bottom: 1px solid #f0f0f0;
}
.back-btn {
width: 40px;
height: 40px;
display: flex;
align-items: center;
cursor: pointer;
}
.header h1 {
font-size: 18px;
font-weight: bold;
}
.empty {
width: 40px;
}
.wishlist-items {
padding: 20px;
padding-bottom: 20px;
}
.wishlist-item {
display: flex;
gap: 15px;
padding: 15px;
background: #f9f9f9;
border-radius: 15px;
margin-bottom: 15px;
}
.item-image {
width: 80px;
height: 80px;
border-radius: 10px;
display: flex;
justify-content: center;
align-items: center;
overflow: hidden;
cursor: pointer;
}
.item-image img {
width: 100%;
height: 100%;
object-fit: cover;
}
.image-placeholder {
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
font-size: 30px;
font-weight: bold;
color: rgba(0,0,0,0.1);
}
.item-details {
flex: 1;
display: flex;
flex-direction: column;
justify-content: space-between;
}
.item-name {
font-size: 16px;
font-weight: bold;
margin: 0 0 5px 0;
}
.item-price {
color: var(--primary-color);
font-weight: bold;
margin: 0 0 10px 0;
}
.item-actions {
display: flex;
justify-content: space-between;
align-items: center;
}
.delete-icon {
color: #ff4757;
cursor: pointer;
font-size: 20px;
}
.empty-wishlist {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 60px 20px;
color: #999;
}
.empty-wishlist p {
margin: 20px 0;
font-size: 16px;
}
</style>