优化一些问题

This commit is contained in:
xiaowang
2026-03-02 10:13:20 +08:00
parent c95394260d
commit b9599b3ece
14 changed files with 256 additions and 331 deletions
+26 -74
View File
@@ -42,52 +42,50 @@
<!-- 商品图片 -->
<div class="product-img">
<img
v-if="getProductImages(item).length > 0"
:src="getProductImages(item)[0]"
:alt="item.title"
v-if="item.image"
:src="item.image"
:alt="item.name"
class="product-image"
/>
<div v-else class="product-img-placeholder">
<span>{{ item.title ? item.title[0] : '商' }}</span>
<span>{{ item.name ? item.name[0] : '商' }}</span>
</div>
<a-badge
v-if="item.tagList && item.tagList.length > 0"
:text="item.tagList[0]"
v-if="item.category"
:text="item.category"
color="#ff4d4f"
class="product-tag"
/>
</div>
<!-- 商品信息 -->
<div class="product-info">
<p class="name">{{ item.title }}</p>
<p class="name">{{ item.name }}</p>
<div class="product-meta">
<span class="duration">{{ item.approximateDuration }}</span>
<span class="duration">{{ item.estimatedTime }} 分钟</span>
<div class="stats">
<span class="like-count">{{ item.likeCount }} 赞</span>
<span class="collect-count">{{ item.collectCount }} 收藏</span>
<span class="like-count">0 赞</span>
<span class="collect-count">0 收藏</span>
</div>
</div>
<!-- 标签展示 -->
<div class="product-tags">
<template v-if="item.tags">
<template v-if="item.category">
<a-tag
v-for="(tagIdStr, index) in item.tags.split(';')"
:key="index"
size="small"
:style="{
marginRight: '4px',
marginBottom: '4px',
fontSize: '9px',
padding: '1px 6px',
color: getTagById(Number(tagIdStr))?.textColor || '#333333',
backgroundColor: getTagById(Number(tagIdStr))?.bgColor || '#F0F0F0',
borderColor: getTagById(Number(tagIdStr))?.borderColor || '#F0F0F0',
color: '#333333',
backgroundColor: '#F0F0F0',
borderColor: '#F0F0F0',
borderWidth: '1px',
borderStyle: 'solid',
borderRadius: '8px'
}"
>
{{ getTagById(Number(tagIdStr))?.tagName || `标签${tagIdStr}` }}
{{ item.category }}
</a-tag>
</template>
<a-tag v-else size="small" color="gray" style="font-size: 10px; padding: 2px 6px">
@@ -138,7 +136,7 @@ import {
import { message } from 'ant-design-vue'
import { productApi, type ProductVO } from '@/api/product'
import { tagApi, type TagVO } from '@/api/tag'
import { fileApi } from '@/api/index'
import { cartApi } from '@/api/cart'
const router = useRouter()
@@ -161,26 +159,20 @@ const hasMore = ref(true)
// 加载状态
const loading = ref(false)
// 图片缓存
const imageIdCache = ref<Record<string, string>>({})
const productImagesCache = ref<Record<number, string[]>>({})
// 从后端获取标签列表
const fetchTags = async () => {
try {
const tags = await tagApi.getAllTags()
allTags.value = tags as unknown as TagVO[]
// 动态生成分类列表
const response = await tagApi.getAllTags() as any
allTags.value = response || []
categories.value = ['全部', ...allTags.value.map(tag => tag.tagName)]
} catch (error) {
console.error('获取标签失败:', error)
}
}
// 根据标签ID获取标签信息
const getTagById = (tagId: number): TagVO | undefined => {
return allTags.value.find((tag: TagVO) => tag.id === tagId)
}
// 从后端获取商品数据
const fetchProducts = async () => {
@@ -191,24 +183,16 @@ const fetchProducts = async () => {
let response: any
if (selectedCategory.value === '全部') {
response = await productApi.getAllProducts()
response = await productApi.getAllProducts() as any
} else {
response = await productApi.getProductsByTag(selectedCategory.value)
response = await productApi.getProductsByTag(selectedCategory.value) as any
}
// 处理返回的数据
console.log('Products response:', response)
products.value = (response as ProductVO[]).map(product => ({
...product,
imageUrls: product.imageUrls || [],
tagList: product.tagList || []
}))
products.value = response || []
console.log('Processed products:', products.value)
// 预加载所有商品的图片
await loadProductImages(products.value)
hasMore.value = false
} catch (error) {
console.error('获取商品失败:', error)
@@ -218,39 +202,7 @@ const fetchProducts = async () => {
}
}
// 加载商品图片
const loadProductImages = async (products: ProductVO[]) => {
for (const product of products) {
if (product.imageFileIds) {
const ids = product.imageFileIds.split(',')
const urls: string[] = []
for (const id of ids) {
if (id) {
try {
// 检查缓存
if (!imageIdCache.value[id]) {
const signedUrl = await fileApi.getSignedUrl(Number(id))
imageIdCache.value[id] = signedUrl
}
urls.push(imageIdCache.value[id])
} catch (error) {
console.error('获取签名链接失败:', error)
}
}
}
productImagesCache.value[product.id] = urls
} else {
productImagesCache.value[product.id] = []
}
}
}
// 获取商品图片
const getProductImages = (product: ProductVO): string[] => {
return productImagesCache.value[product.id] || []
}
// 处理返回按钮点击
const handleBackClick = () => {
@@ -268,16 +220,16 @@ const handleCategoryClick = (category: string) => {
// 处理商品卡片点击
const handleProductClick = (item: any) => {
console.log('商品被点击:', item.title)
console.log('商品被点击:', item.name)
router.push(`/product/${item.id}`)
}
// 处理添加到购物车
const handleAddToCart = async (item: any) => {
console.log('添加到购物车:', item.title)
console.log('添加到购物车:', item.name)
try {
await cartApi.addToCart({ productId: item.id, quantity: 1 })
message.success(`已将 ${item.title} 添加到购物车`)
message.success(`已将 ${item.name} 添加到购物车`)
} catch (error) {
console.error('添加到购物车失败:', error)
message.error('添加到购物车失败,请重试')