功能:更新消息中心和 YumiNail 模块
This commit is contained in:
@@ -0,0 +1,574 @@
|
||||
<template>
|
||||
<div class="mobile-container">
|
||||
<header class="header">
|
||||
<a-button class="back-btn" type="text" @click="handleBackClick">
|
||||
<LeftOutlined :style="{ fontSize: '20px' }" />
|
||||
</a-button>
|
||||
<h1 v-if="!showSearch">作品列表</h1>
|
||||
<div v-else class="search-input-container">
|
||||
<a-input-search
|
||||
v-model:value="searchKeyword"
|
||||
placeholder="搜索标题、标签或描述"
|
||||
class="search-input"
|
||||
enter-button
|
||||
@search="handleSearch"
|
||||
/>
|
||||
</div>
|
||||
<a-button class="search-btn" type="text" @click="toggleSearch">
|
||||
<SearchOutlined :style="{ fontSize: '20px' }" />
|
||||
</a-button>
|
||||
</header>
|
||||
|
||||
<div class="main-content">
|
||||
<div class="category-sidebar">
|
||||
<div
|
||||
v-for="category in categories"
|
||||
:key="category"
|
||||
class="category-item"
|
||||
:class="{ active: selectedCategory === category }"
|
||||
@click="handleCategoryClick(category)"
|
||||
>
|
||||
{{ category }}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="product-section">
|
||||
<a-alert
|
||||
v-if="activeBannerText"
|
||||
class="page-banner"
|
||||
type="info"
|
||||
show-icon
|
||||
:message="activeBannerText"
|
||||
/>
|
||||
|
||||
<div class="product-grid">
|
||||
<a-card
|
||||
v-for="item in products"
|
||||
:key="item.id"
|
||||
class="product-card"
|
||||
:body-style="{ padding: '12px' }"
|
||||
hoverable
|
||||
@click="handleProductClick(item)"
|
||||
>
|
||||
<div class="product-img">
|
||||
<img
|
||||
v-if="getProductImages(item).length > 0"
|
||||
:src="getProductImages(item)[0]"
|
||||
:alt="item.title || item.name"
|
||||
class="product-image"
|
||||
/>
|
||||
<div v-else class="product-img-placeholder">
|
||||
<span>{{ (item.title || item.name)?.[0] || '作' }}</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="product-info">
|
||||
<p class="name">{{ item.title || item.name }}</p>
|
||||
<div class="product-meta">
|
||||
<span class="duration">{{ item.approximateDuration || '时长待定' }}</span>
|
||||
<div class="stats">
|
||||
<span>{{ item.likeCount || 0 }} 点赞</span>
|
||||
<span>{{ item.collectCount || 0 }} 收藏</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="product-tags">
|
||||
<template v-if="item.tagInfos?.length">
|
||||
<a-tag
|
||||
v-for="tag in item.tagInfos"
|
||||
:key="tag.id"
|
||||
size="small"
|
||||
:style="{
|
||||
marginRight: '4px',
|
||||
marginBottom: '4px',
|
||||
color: tag.textColor || '#333',
|
||||
backgroundColor: tag.bgColor || '#f0f0f0',
|
||||
borderColor: tag.borderColor || '#f0f0f0'
|
||||
}"
|
||||
>
|
||||
{{ tag.tagName }}
|
||||
</a-tag>
|
||||
</template>
|
||||
<a-tag v-else size="small" color="default">暂无标签</a-tag>
|
||||
</div>
|
||||
|
||||
<div class="price-row">
|
||||
<p class="price">¥{{ Number(item.price || 0).toFixed(2) }}</p>
|
||||
<div class="cart-action">
|
||||
<span v-if="item.inCart" class="cart-note">购物车已有</span>
|
||||
<a-button
|
||||
:type="item.inCart ? 'default' : 'primary'"
|
||||
size="small"
|
||||
shape="circle"
|
||||
class="cart-btn"
|
||||
:class="{ 'cart-btn-in-cart': item.inCart }"
|
||||
@click.stop="handleAddToCart(item)"
|
||||
>
|
||||
<CheckOutlined v-if="item.inCart" />
|
||||
<ShoppingCartOutlined v-else />
|
||||
</a-button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</a-card>
|
||||
</div>
|
||||
|
||||
<div v-if="products.length === 0 && !loading" class="empty-state">
|
||||
<a-empty description="暂时没有匹配的作品">
|
||||
<a-button type="primary" @click="resetFilters">查看全部作品</a-button>
|
||||
</a-empty>
|
||||
</div>
|
||||
|
||||
<div v-if="loading" class="loading-state">
|
||||
<a-spin />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { onMounted, ref, watch, inject, computed } from 'vue'
|
||||
import { useRouter, useRoute } from 'vue-router'
|
||||
import { CheckOutlined, LeftOutlined, SearchOutlined, ShoppingCartOutlined } from '@ant-design/icons-vue'
|
||||
import { message } from 'ant-design-vue'
|
||||
import { productApi, type ProductVO } from '@/api/product'
|
||||
import { tagApi, type TagVO } from '@/api/tag'
|
||||
import { cartApi } from '@/api/cart'
|
||||
import { fileApi } from '@/api/file'
|
||||
import { runWithGlobalLoading } from '@/utils/action-loading'
|
||||
import { fetchCartProductIdSet, hasCartProduct, withAddedCartProduct } from '@/utils/cart-state'
|
||||
|
||||
type ProductListItem = ProductVO & {
|
||||
inCart?: boolean
|
||||
}
|
||||
|
||||
const router = useRouter()
|
||||
const route = useRoute()
|
||||
const updateCartCount = inject<() => Promise<void>>('updateCartCount', async () => {})
|
||||
|
||||
const allTags = ref<TagVO[]>([])
|
||||
const categories = ref<string[]>(['全部'])
|
||||
const selectedCategory = ref('全部')
|
||||
const products = ref<ProductListItem[]>([])
|
||||
const loading = ref(false)
|
||||
const showSearch = ref(false)
|
||||
const searchKeyword = ref('')
|
||||
const imageIdCache = ref<Record<string, string>>({})
|
||||
const imageUrls = ref<Record<string, string[]>>({})
|
||||
const cartProductIdSet = ref(new Set<string>())
|
||||
let useGlobalLoadingForNextRouteSync = false
|
||||
|
||||
const isLoggedIn = computed(() => Boolean(localStorage.getItem('accessToken') || sessionStorage.getItem('accessToken')))
|
||||
const activeBannerText = computed(() => {
|
||||
if (searchKeyword.value.trim()) {
|
||||
return `当前显示“${searchKeyword.value.trim()}”的匹配结果`
|
||||
}
|
||||
if (selectedCategory.value !== '全部') {
|
||||
return `当前按标签“${selectedCategory.value}”筛选`
|
||||
}
|
||||
return ''
|
||||
})
|
||||
|
||||
const DEFAULT_PRODUCT_IMAGE = 'data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMjAwIiBoZWlnaHQ9IjIwMCIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj48cmVjdCBmaWxsPSIjZjVmNWY1IiB3aWR0aD0iMjAwIiBoZWlnaHQ9IjIwMCIvPjx0ZXh0IHg9IjUwJSIgeT0iNTAlIiBmaWxsPSIjYmZiZmJmIiBmb250LXNpemU9IjI0IiB0ZXh0LWFuY2hvcj0ibWlkZGxlIiBkeT0iLjNlbSI+YWFhPC90ZXh0Pjwvc3ZnPg=='
|
||||
|
||||
const ensureLoginForAction = (actionText: string) => {
|
||||
if (isLoggedIn.value) {
|
||||
return true
|
||||
}
|
||||
message.info(`${actionText}前请先登录账号`)
|
||||
router.push('/')
|
||||
return false
|
||||
}
|
||||
|
||||
const loadImageUrls = async (productList: ProductVO[]) => {
|
||||
for (const product of productList) {
|
||||
if (!product.imageFileIds) {
|
||||
imageUrls.value[String(product.id)] = [DEFAULT_PRODUCT_IMAGE]
|
||||
continue
|
||||
}
|
||||
|
||||
const ids = product.imageFileIds.split(',').map(id => id.trim()).filter(Boolean)
|
||||
const urls: string[] = []
|
||||
|
||||
for (const id of ids) {
|
||||
try {
|
||||
if (!imageIdCache.value[id]) {
|
||||
imageIdCache.value[id] = await fileApi.getSignedUrl(id) as unknown as string
|
||||
}
|
||||
if (imageIdCache.value[id]) {
|
||||
urls.push(imageIdCache.value[id])
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('获取作品图片失败:', error)
|
||||
}
|
||||
}
|
||||
|
||||
imageUrls.value[String(product.id)] = urls.length ? urls : [DEFAULT_PRODUCT_IMAGE]
|
||||
}
|
||||
}
|
||||
|
||||
const getProductImages = (product: ProductVO): string[] => imageUrls.value[String(product.id)] || [DEFAULT_PRODUCT_IMAGE]
|
||||
|
||||
const markProductsInCart = (productList: ProductVO[]) => {
|
||||
return (Array.isArray(productList) ? productList : []).map(item => ({
|
||||
...item,
|
||||
inCart: hasCartProduct(cartProductIdSet.value, item.id)
|
||||
}))
|
||||
}
|
||||
|
||||
const syncCartProductState = async () => {
|
||||
if (!isLoggedIn.value) {
|
||||
cartProductIdSet.value = new Set()
|
||||
return
|
||||
}
|
||||
cartProductIdSet.value = await fetchCartProductIdSet()
|
||||
}
|
||||
|
||||
const fetchTags = async () => {
|
||||
try {
|
||||
const response = await tagApi.getAllTags() as unknown as TagVO[]
|
||||
allTags.value = Array.isArray(response) ? response : []
|
||||
categories.value = ['全部', ...allTags.value.map(tag => tag.tagName)]
|
||||
} catch (error: any) {
|
||||
console.error('获取标签失败:', error)
|
||||
categories.value = ['全部']
|
||||
}
|
||||
}
|
||||
|
||||
const fetchProducts = async () => {
|
||||
loading.value = true
|
||||
try {
|
||||
let response: ProductVO[] = []
|
||||
const keyword = searchKeyword.value.trim()
|
||||
|
||||
if (keyword) {
|
||||
response = await productApi.searchProducts(keyword)
|
||||
} else if (selectedCategory.value === '全部') {
|
||||
response = await productApi.getAllProducts()
|
||||
} else {
|
||||
response = await productApi.getProductsByTag(selectedCategory.value)
|
||||
}
|
||||
|
||||
products.value = markProductsInCart(Array.isArray(response) ? response : [])
|
||||
await loadImageUrls(products.value)
|
||||
} catch (error: any) {
|
||||
console.error('获取作品列表失败:', error)
|
||||
message.error(error?.message || '加载作品列表失败,请稍后重试')
|
||||
products.value = []
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
const syncRouteFilters = async () => {
|
||||
await syncCartProductState()
|
||||
const keyword = typeof route.query.keyword === 'string' ? route.query.keyword.trim() : ''
|
||||
const tag = typeof route.query.tag === 'string' ? route.query.tag.trim() : ''
|
||||
|
||||
searchKeyword.value = keyword
|
||||
showSearch.value = Boolean(keyword)
|
||||
selectedCategory.value = tag && categories.value.includes(tag) ? tag : '全部'
|
||||
if (useGlobalLoadingForNextRouteSync) {
|
||||
useGlobalLoadingForNextRouteSync = false
|
||||
await runWithGlobalLoading(() => fetchProducts())
|
||||
return
|
||||
}
|
||||
await fetchProducts()
|
||||
}
|
||||
|
||||
const handleBackClick = () => {
|
||||
router.push('/home')
|
||||
}
|
||||
|
||||
const handleCategoryClick = (category: string) => {
|
||||
selectedCategory.value = category
|
||||
searchKeyword.value = ''
|
||||
showSearch.value = false
|
||||
useGlobalLoadingForNextRouteSync = true
|
||||
router.replace({
|
||||
path: '/products',
|
||||
query: category === '全部' ? {} : { tag: category }
|
||||
})
|
||||
}
|
||||
|
||||
const handleProductClick = (item: ProductVO) => {
|
||||
router.push(`/product/${item.id}`)
|
||||
}
|
||||
|
||||
const handleAddToCart = async (item: ProductListItem) => {
|
||||
if (!ensureLoginForAction('加入购物车')) {
|
||||
return
|
||||
}
|
||||
if (hasCartProduct(cartProductIdSet.value, item.id)) {
|
||||
message.info('购物车已有')
|
||||
return
|
||||
}
|
||||
try {
|
||||
await runWithGlobalLoading(() => cartApi.addToCart({ productId: item.id, quantity: 1 }))
|
||||
cartProductIdSet.value = withAddedCartProduct(cartProductIdSet.value, item.id)
|
||||
products.value = markProductsInCart(products.value)
|
||||
await updateCartCount()
|
||||
message.success(`已将 ${item.title || item.name || '作品'} 加入购物车`)
|
||||
} catch (error: any) {
|
||||
console.error('加入购物车失败:', error)
|
||||
message.error(error?.message || '加入购物车失败,请稍后重试')
|
||||
}
|
||||
}
|
||||
|
||||
const handleSearch = () => {
|
||||
const keyword = searchKeyword.value.trim()
|
||||
useGlobalLoadingForNextRouteSync = true
|
||||
router.replace({
|
||||
path: '/products',
|
||||
query: keyword ? { keyword } : {}
|
||||
})
|
||||
}
|
||||
|
||||
const toggleSearch = () => {
|
||||
showSearch.value = !showSearch.value
|
||||
if (!showSearch.value) {
|
||||
searchKeyword.value = ''
|
||||
useGlobalLoadingForNextRouteSync = true
|
||||
router.replace({ path: '/products', query: selectedCategory.value === '全部' ? {} : { tag: selectedCategory.value } })
|
||||
}
|
||||
}
|
||||
|
||||
const resetFilters = () => {
|
||||
selectedCategory.value = '全部'
|
||||
searchKeyword.value = ''
|
||||
showSearch.value = false
|
||||
useGlobalLoadingForNextRouteSync = true
|
||||
router.replace('/products')
|
||||
}
|
||||
|
||||
watch(
|
||||
() => route.query,
|
||||
async () => {
|
||||
await syncRouteFilters()
|
||||
},
|
||||
{ deep: true }
|
||||
)
|
||||
|
||||
onMounted(async () => {
|
||||
await fetchTags()
|
||||
await syncRouteFilters()
|
||||
})
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.mobile-container {
|
||||
padding-top: 60px;
|
||||
min-height: 100vh;
|
||||
background: #f5f5f5;
|
||||
}
|
||||
|
||||
.header {
|
||||
position: fixed;
|
||||
top: 0;
|
||||
left: 0;
|
||||
right: 0;
|
||||
z-index: 100;
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
padding: 10px 15px;
|
||||
border-bottom: 1px solid #f0f0f0;
|
||||
background: white;
|
||||
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
|
||||
}
|
||||
|
||||
.header h1 {
|
||||
margin: 0;
|
||||
font-size: 18px;
|
||||
font-weight: 700;
|
||||
}
|
||||
|
||||
.back-btn,
|
||||
.search-btn {
|
||||
width: 40px;
|
||||
height: 40px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.search-input-container {
|
||||
flex: 1;
|
||||
margin: 0 10px;
|
||||
}
|
||||
|
||||
.main-content {
|
||||
display: flex;
|
||||
min-height: calc(100vh - 60px);
|
||||
}
|
||||
|
||||
.category-sidebar {
|
||||
width: 92px;
|
||||
background: #fafafa;
|
||||
border-right: 1px solid #ececec;
|
||||
overflow-y: auto;
|
||||
}
|
||||
|
||||
.category-item {
|
||||
padding: 16px 12px;
|
||||
font-size: 13px;
|
||||
color: #666;
|
||||
text-align: center;
|
||||
cursor: pointer;
|
||||
transition: all 0.2s ease;
|
||||
border-left: 3px solid transparent;
|
||||
}
|
||||
|
||||
.category-item.active {
|
||||
color: #1677ff;
|
||||
background: white;
|
||||
border-left-color: #1677ff;
|
||||
font-weight: 700;
|
||||
}
|
||||
|
||||
.product-section {
|
||||
flex: 1;
|
||||
padding: 16px;
|
||||
overflow-y: auto;
|
||||
}
|
||||
|
||||
.page-banner {
|
||||
margin-bottom: 16px;
|
||||
border-radius: 14px;
|
||||
}
|
||||
|
||||
.product-grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(2, minmax(0, 1fr));
|
||||
gap: 14px;
|
||||
}
|
||||
|
||||
.product-card {
|
||||
border-radius: 18px;
|
||||
}
|
||||
|
||||
.product-img {
|
||||
width: 100%;
|
||||
aspect-ratio: 1;
|
||||
overflow: hidden;
|
||||
border-radius: 14px;
|
||||
background: #eef3ff;
|
||||
}
|
||||
|
||||
.product-image {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
object-fit: cover;
|
||||
}
|
||||
|
||||
.product-img-placeholder {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
font-size: 40px;
|
||||
font-weight: 700;
|
||||
color: rgba(0, 0, 0, 0.18);
|
||||
}
|
||||
|
||||
.name {
|
||||
margin: 12px 0 6px;
|
||||
font-size: 14px;
|
||||
font-weight: 700;
|
||||
color: #1f1f1f;
|
||||
white-space: nowrap;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
}
|
||||
|
||||
.product-meta {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
gap: 8px;
|
||||
font-size: 12px;
|
||||
color: #8b8b9a;
|
||||
}
|
||||
|
||||
.duration {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
padding: 2px 8px;
|
||||
border-radius: 999px;
|
||||
background: #f4f6fb;
|
||||
}
|
||||
|
||||
.stats {
|
||||
display: flex;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.product-tags {
|
||||
margin: 10px 0;
|
||||
min-height: 26px;
|
||||
}
|
||||
|
||||
.price-row {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
gap: 12px;
|
||||
}
|
||||
|
||||
.price {
|
||||
margin: 0;
|
||||
font-size: 18px;
|
||||
font-weight: 700;
|
||||
color: #ff4d4f;
|
||||
}
|
||||
|
||||
.cart-action {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
justify-content: flex-end;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.cart-note {
|
||||
font-size: 12px;
|
||||
color: #8c8c8c;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.cart-btn {
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.cart-btn-in-cart {
|
||||
color: #52c41a;
|
||||
border-color: #b7eb8f;
|
||||
background: #f6ffed;
|
||||
}
|
||||
|
||||
.empty-state,
|
||||
.loading-state {
|
||||
padding: 56px 0;
|
||||
}
|
||||
|
||||
@media (min-width: 992px) {
|
||||
.main-content {
|
||||
max-width: var(--frontend-content-max-width);
|
||||
margin: 0 auto;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.category-sidebar {
|
||||
width: 128px;
|
||||
border-radius: 20px 0 0 20px;
|
||||
}
|
||||
|
||||
.product-section {
|
||||
padding: 24px;
|
||||
}
|
||||
|
||||
.product-grid {
|
||||
grid-template-columns: repeat(4, minmax(0, 1fr));
|
||||
}
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user