功能:更新消息中心和 YumiNail 模块
This commit is contained in:
@@ -0,0 +1,292 @@
|
||||
<template>
|
||||
<div class="mobile-container">
|
||||
<header class="header">
|
||||
<div class="back-btn" @click="handleBackClick">
|
||||
<el-icon><ArrowLeft /></el-icon>
|
||||
</div>
|
||||
<h1>我的收藏</h1>
|
||||
<div class="empty"></div>
|
||||
</header>
|
||||
|
||||
<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).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">¥{{ Number(item.product.price || 0).toFixed(2) }}</p>
|
||||
<div class="item-actions">
|
||||
<el-button
|
||||
size="small"
|
||||
:type="isProductInCart(item.product.id) ? 'info' : 'primary'"
|
||||
@click="handleAddToCart(item.product.id)"
|
||||
>
|
||||
{{ isProductInCart(item.product.id) ? '购物车已有' : '加入购物车' }}
|
||||
</el-button>
|
||||
<el-icon class="delete-icon" @click="handleRemoveItem(item.product.id)"><Delete /></el-icon>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<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'
|
||||
import { runWithGlobalLoading } from '@/utils/action-loading'
|
||||
import { fetchCartProductIdSet, hasCartProduct, withAddedCartProduct } from '@/utils/cart-state'
|
||||
|
||||
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 cartProductIdSet = ref(new Set<string>())
|
||||
const DEFAULT_PRODUCT_IMAGE = 'data:image/svg+xml;base64,PHN2ZyB3aW0dPSJodHRwOi8vd3d3Lm9yZy8yMDAwL3N2ZyI+Pjwvc3ZnPg=='
|
||||
|
||||
const handleBackClick = () => {
|
||||
router.back()
|
||||
}
|
||||
|
||||
const fetchWishlist = async () => {
|
||||
try {
|
||||
const [response, nextCartProductIdSet] = await Promise.all([
|
||||
wishlistApi.getWishlist(),
|
||||
fetchCartProductIdSet()
|
||||
])
|
||||
cartProductIdSet.value = nextCartProductIdSet
|
||||
wishlistItems.value = response as unknown as WishlistItem[]
|
||||
await loadImageUrls(response as unknown as WishlistItem[])
|
||||
} catch (error: any) {
|
||||
console.error('获取收藏列表失败:', error)
|
||||
ElMessage.error(error?.message || '获取收藏列表失败')
|
||||
}
|
||||
}
|
||||
|
||||
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.length ? urls : [DEFAULT_PRODUCT_IMAGE]
|
||||
} 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 isProductInCart = (productId: IdType) => hasCartProduct(cartProductIdSet.value, productId)
|
||||
|
||||
const handleAddToCart = async (productId: IdType) => {
|
||||
if (isProductInCart(productId)) {
|
||||
ElMessage.info('购物车已有')
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
await runWithGlobalLoading(() => cartApi.addToCart({ productId, quantity: 1 }))
|
||||
cartProductIdSet.value = withAddedCartProduct(cartProductIdSet.value, productId)
|
||||
await updateCartCount()
|
||||
ElMessage.success('已添加到购物车')
|
||||
} catch (error: any) {
|
||||
console.error('添加到购物车失败:', error)
|
||||
ElMessage.error(error?.message || '添加到购物车失败')
|
||||
}
|
||||
}
|
||||
|
||||
const handleRemoveItem = async (productId: IdType) => {
|
||||
try {
|
||||
await runWithGlobalLoading(() => wishlistApi.removeFromWishlist(productId))
|
||||
wishlistItems.value = wishlistItems.value.filter(item => !sameId(item.productId, productId))
|
||||
ElMessage.success('已从收藏中移除')
|
||||
} catch (error: any) {
|
||||
console.error('移除收藏失败:', error)
|
||||
ElMessage.error(error?.message || '移除收藏失败')
|
||||
}
|
||||
}
|
||||
|
||||
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>
|
||||
.mobile-container {
|
||||
min-height: 100vh;
|
||||
background: #fff;
|
||||
}
|
||||
|
||||
.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 {
|
||||
margin: 0;
|
||||
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;
|
||||
gap: 12px;
|
||||
}
|
||||
|
||||
.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>
|
||||
Reference in New Issue
Block a user