378 lines
12 KiB
Vue
378 lines
12 KiB
Vue
<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: 'var(--theme-primary)' }"><StarFilled /></el-icon>
|
|
<p>收藏列表为空</p>
|
|
<el-button type="primary" @click="handleBrowseProducts">
|
|
浏览作品
|
|
</el-button>
|
|
</div>
|
|
</div>
|
|
</template>
|
|
|
|
<script setup lang="ts">
|
|
/**
|
|
* WishlistView page script.
|
|
* Call guide: the template binds the reactive state below, and the methods are triggered by button clicks, lifecycle hooks, or router events.
|
|
*/
|
|
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 'element-plus/es/components/message/style/css'
|
|
/**
|
|
* Purpose: WishlistItem supports WishlistView.vue by holding or executing the wishlist item page or API role.
|
|
* Usage: import, call, read, or update this type from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
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'
|
|
|
|
/**
|
|
* Purpose: router supports WishlistView.vue by holding or executing the router page or API role.
|
|
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
const router = useRouter()
|
|
/**
|
|
* Purpose: updateCartCount supports WishlistView.vue by holding or executing the update cart count page or API role.
|
|
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
const updateCartCount = inject<() => Promise<void>>('updateCartCount', async () => {})
|
|
|
|
/**
|
|
* Purpose: wishlistItems supports WishlistView.vue by holding or executing the wishlist items page or API role.
|
|
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
const wishlistItems = ref<WishlistItem[]>([])
|
|
/**
|
|
* Purpose: imageIdCache supports WishlistView.vue by holding or executing the image id cache page or API role.
|
|
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
const imageIdCache = ref<Record<string, string>>({})
|
|
/**
|
|
* Purpose: imageUrls supports WishlistView.vue by holding or executing the image urls page or API role.
|
|
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
const imageUrls = ref<Record<string, string[]>>({})
|
|
/** cartProductIdSet is a reactive state variable used by the template and event handlers. */
|
|
const cartProductIdSet = ref(new Set<string>())
|
|
/**
|
|
* Purpose: DEFAULT_PRODUCT_IMAGE supports WishlistView.vue by holding or executing the default product image page or API role.
|
|
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
const DEFAULT_PRODUCT_IMAGE = 'data:image/svg+xml;base64,PHN2ZyB3aW0dPSJodHRwOi8vd3d3Lm9yZy8yMDAwL3N2ZyI+Pjwvc3ZnPg=='
|
|
|
|
/** handleBackClick is a local helper used by this page script. */
|
|
const handleBackClick = () => {
|
|
router.back()
|
|
}
|
|
|
|
/** fetchWishlist is an async page action. Trigger it from the template or lifecycle hook, then await the result. */
|
|
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 || '获取收藏列表失败')
|
|
}
|
|
}
|
|
|
|
/** loadImageUrls is an async page action. Trigger it from the template or lifecycle hook, then await the result. */
|
|
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]
|
|
}
|
|
}
|
|
}
|
|
|
|
/** getProductImages is a local helper used by this page script. */
|
|
const getProductImages = (product: any): string[] => {
|
|
return imageUrls.value[String(product.id)] || [DEFAULT_PRODUCT_IMAGE]
|
|
}
|
|
|
|
/** isProductInCart is a local helper used by this page script. */
|
|
const isProductInCart = (productId: IdType) => hasCartProduct(cartProductIdSet.value, productId)
|
|
|
|
/** handleAddToCart is an async page action. Trigger it from the template or lifecycle hook, then await the result. */
|
|
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 || '添加到购物车失败')
|
|
}
|
|
}
|
|
|
|
/** handleRemoveItem is an async page action. Trigger it from the template or lifecycle hook, then await the result. */
|
|
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 || '移除收藏失败')
|
|
}
|
|
}
|
|
|
|
/** handleBrowseProducts is a local helper used by this page script. */
|
|
const handleBrowseProducts = () => {
|
|
router.push('/products')
|
|
}
|
|
|
|
/** handleProductClick is a local helper used by this page script. */
|
|
const handleProductClick = (productId: IdType) => {
|
|
router.push(`/product/${productId}`)
|
|
}
|
|
|
|
/** getRandomColor is a local helper used by this page script. */
|
|
const getRandomColor = () => {
|
|
return 'var(--theme-primary-soft)'
|
|
}
|
|
|
|
onMounted(() => {
|
|
fetchWishlist()
|
|
})
|
|
</script>
|
|
|
|
<style scoped>
|
|
.mobile-container {
|
|
min-height: 100vh;
|
|
padding-top: 60px;
|
|
background:
|
|
linear-gradient(rgba(255, 255, 255, 0.68), rgba(255, 255, 255, 0.92)),
|
|
var(--theme-background-image),
|
|
var(--theme-page-bg);
|
|
background-size: cover, cover, auto;
|
|
background-position: center top;
|
|
}
|
|
|
|
.header {
|
|
position: fixed;
|
|
top: 0;
|
|
left: 0;
|
|
right: 0;
|
|
z-index: 100;
|
|
display: flex;
|
|
justify-content: space-between;
|
|
align-items: center;
|
|
height: 60px;
|
|
padding: 10px 15px;
|
|
background: rgba(255, 255, 255, 0.92);
|
|
border-bottom: 1px solid var(--theme-border);
|
|
backdrop-filter: blur(14px);
|
|
}
|
|
|
|
.back-btn {
|
|
width: 40px;
|
|
height: 40px;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
cursor: pointer;
|
|
color: var(--text-primary);
|
|
}
|
|
|
|
.header h1 {
|
|
margin: 0;
|
|
font-size: 18px;
|
|
font-weight: bold;
|
|
color: var(--text-primary);
|
|
}
|
|
|
|
.empty {
|
|
width: 40px;
|
|
}
|
|
|
|
.wishlist-items {
|
|
padding: 16px;
|
|
padding-bottom: 28px;
|
|
}
|
|
|
|
.wishlist-item {
|
|
display: flex;
|
|
gap: 15px;
|
|
padding: 15px;
|
|
background: var(--theme-card-bg);
|
|
border: 1px solid var(--theme-border);
|
|
border-radius: 16px;
|
|
margin-bottom: 15px;
|
|
box-shadow: 0 14px 34px rgba(122, 67, 88, 0.08);
|
|
}
|
|
|
|
.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);
|
|
border: 1px solid var(--theme-border);
|
|
}
|
|
|
|
.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;
|
|
color: var(--text-primary);
|
|
}
|
|
|
|
.item-price {
|
|
color: var(--theme-primary);
|
|
font-weight: bold;
|
|
margin: 0 0 10px 0;
|
|
}
|
|
|
|
.item-actions {
|
|
display: flex;
|
|
justify-content: space-between;
|
|
align-items: center;
|
|
gap: 12px;
|
|
}
|
|
|
|
.delete-icon {
|
|
color: var(--theme-accent);
|
|
cursor: pointer;
|
|
font-size: 20px;
|
|
}
|
|
|
|
.empty-wishlist {
|
|
display: flex;
|
|
flex-direction: column;
|
|
align-items: center;
|
|
justify-content: center;
|
|
margin: 16px;
|
|
padding: 64px 20px;
|
|
color: var(--text-secondary);
|
|
background: var(--theme-card-bg);
|
|
border: 1px solid var(--theme-border);
|
|
border-radius: 18px;
|
|
box-shadow: 0 14px 34px rgba(122, 67, 88, 0.08);
|
|
}
|
|
|
|
.empty-wishlist p {
|
|
margin: 20px 0;
|
|
font-size: 16px;
|
|
}
|
|
|
|
:deep(.el-button--primary) {
|
|
--el-button-bg-color: var(--theme-primary);
|
|
--el-button-border-color: var(--theme-primary);
|
|
--el-button-hover-bg-color: var(--theme-accent);
|
|
--el-button-hover-border-color: var(--theme-accent);
|
|
}
|
|
|
|
:deep(.el-button--info) {
|
|
--el-button-bg-color: var(--theme-primary-soft);
|
|
--el-button-border-color: var(--theme-border);
|
|
--el-button-text-color: var(--theme-accent);
|
|
--el-button-hover-bg-color: var(--theme-primary-soft);
|
|
--el-button-hover-border-color: var(--theme-border);
|
|
--el-button-hover-text-color: var(--theme-accent);
|
|
}
|
|
</style>
|