标签功能初版实现

This commit is contained in:
xiaowang
2026-02-24 09:59:22 +08:00
parent c5018b14a5
commit aa0aca2ac0
56 changed files with 2273 additions and 305 deletions
+198 -44
View File
@@ -40,47 +40,94 @@
@click="handleProductClick(item)"
>
<!-- 商品图片 -->
<div class="product-img" :style="{ backgroundColor: item.bgColor }">
<span>{{ item.name[0] }}</span>
<div class="product-img">
<img
v-if="item.imageUrls && item.imageUrls.length > 0"
:src="item.imageUrls[0]"
:alt="item.title"
class="product-image"
/>
<div v-else class="product-img-placeholder">
<span>{{ item.title ? item.title[0] : '商' }}</span>
</div>
<a-badge
v-if="item.tag"
:text="item.tag"
v-if="item.tagList && item.tagList.length > 0"
:text="item.tagList[0]"
color="#ff4d4f"
class="product-tag"
/>
</div>
<!-- 商品信息 -->
<div class="product-info">
<p class="name">{{ item.title }}</p>
<div class="product-meta">
<span class="duration">{{ item.approximateDuration }}</span>
<div class="stats">
<span class="like-count">{{ item.likeCount }} 赞</span>
<span class="collect-count">{{ item.collectCount }} 收藏</span>
</div>
</div>
<!-- 标签展示 -->
<div class="product-tags">
<template v-if="item.tags">
<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',
borderWidth: '1px',
borderStyle: 'solid',
borderRadius: '8px'
}"
>
{{ getTagById(Number(tagIdStr))?.tagName || `标签${tagIdStr}` }}
</a-tag>
</template>
<a-tag v-else size="small" color="gray" style="font-size: 10px; padding: 2px 6px">
无标签
</a-tag>
</div>
<div class="price-row">
<p class="price">¥{{ item.price }}</p>
<a-button
type="primary"
size="small"
shape="circle"
@click.stop="handleAddToCart(item)"
>
<ShoppingCartOutlined />
</a-button>
</div>
</div>
</a-card>
</div>
<!-- 商品信息 -->
<div class="product-info">
<p class="name">{{ item.name }}</p>
<div class="price-row">
<p class="price">${{ item.price }}</p>
<a-button
type="primary"
size="small"
shape="circle"
@click.stop="handleAddToCart(item)"
>
<ShoppingCartOutlined />
</a-button>
</div>
</div>
</a-card>
</div>
<!-- 加载更多 -->
<div class="load-more">
<a-button v-if="hasMore" type="link" @click="handleLoadMore">
加载更多 <DownOutlined />
</a-button>
<a-typography-text v-else type="secondary">没有更多商品了</a-typography-text>
</div>
<!-- 空状态 -->
<div v-if="products.length === 0 && !loading" class="empty-state">
<a-typography-text type="secondary">商家暂未上架更多商品</a-typography-text>
</div>
<!-- 加载更多 -->
<div v-else-if="products.length > 0" class="load-more">
<a-button v-if="hasMore" type="link" @click="handleLoadMore">
加载更多 <DownOutlined />
</a-button>
<a-typography-text v-else type="secondary">没有更多商品了</a-typography-text>
</div>
</div>
</div>
</div>
</template>
<script setup lang="ts">
import { ref } from 'vue'
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import {
LeftOutlined,
@@ -89,28 +136,77 @@ import {
DownOutlined
} 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'
const router = useRouter()
// 标签列表
const allTags = ref<TagVO[]>([])
// 当前选中的分类
const selectedCategory = ref('全部')
// 分类列表
const categories = ['全部', '电子产品', '服装', '配饰', '家居']
const categories = ['全部', '美甲', '美睫', '手足护理', '纹绣']
// 商品数据
const products = ref([
{ id: 1, name: 'Watch', price: 40, bgColor: '#ffe4d6', tag: '热销' },
{ id: 2, name: 'LG TV', price: 330, bgColor: '#f0d6ff', tag: '新品' },
{ id: 3, name: 'Jacket', price: 50, bgColor: '#ffd6e2' },
{ id: 4, name: 'Nike Shoes', price: 430, bgColor: '#d6eaff', tag: '限时' },
{ id: 5, name: 'Airpods', price: 333, bgColor: '#e2ffd6' },
{ id: 6, name: 'Hoodie', price: 50, bgColor: '#d6fff0' }
])
const products = ref<ProductVO[]>([])
// 是否还有更多数据
const hasMore = ref(true)
// 加载状态
const loading = ref(false)
// 从后端获取标签列表
const fetchTags = async () => {
try {
const tags = await tagApi.getAllTags()
allTags.value = tags as unknown as TagVO[]
} catch (error) {
console.error('获取标签失败:', error)
}
}
// 根据标签ID获取标签信息
const getTagById = (tagId: number): TagVO | undefined => {
return allTags.value.find((tag: TagVO) => tag.id === tagId)
}
// 从后端获取商品数据
const fetchProducts = async () => {
if (loading.value) return
loading.value = true
try {
let response: any
if (selectedCategory.value === '全部') {
response = await productApi.getAllProducts()
} else {
response = await productApi.getProductsByTag(selectedCategory.value)
}
// 处理返回的数据
console.log('Products response:', response)
products.value = (response as ProductVO[]).map(product => ({
...product,
imageUrls: product.imageUrls || [],
tagList: product.tagList || []
}))
console.log('Processed products:', products.value)
hasMore.value = false
} catch (error) {
console.error('获取商品失败:', error)
message.error('获取商品失败,请稍后重试')
} finally {
loading.value = false
}
}
// 处理返回按钮点击
const handleBackClick = () => {
console.log('返回按钮被点击')
@@ -122,18 +218,19 @@ const handleCategoryClick = (category: string) => {
console.log('选择分类:', category)
selectedCategory.value = category
// 这里可以添加筛选逻辑
fetchProducts()
}
// 处理商品卡片点击
const handleProductClick = (item: any) => {
console.log('商品被点击:', item.name)
console.log('商品被点击:', item.title)
router.push(`/product/${item.id}`)
}
// 处理添加到购物车
const handleAddToCart = (item: any) => {
console.log('添加到购物车:', item.name)
message.success(`已将 ${item.name} 添加到购物车`)
console.log('添加到购物车:', item.title)
message.success(`已将 ${item.title} 添加到购物车`)
}
// 处理加载更多
@@ -142,6 +239,12 @@ const handleLoadMore = () => {
// 这里可以添加加载更多逻辑
hasMore.value = false
}
// 页面挂载时获取商品数据
onMounted(() => {
fetchTags()
fetchProducts()
})
</script>
<style scoped>
@@ -252,15 +355,27 @@ const handleLoadMore = () => {
width: 100%;
aspect-ratio: 1;
border-radius: 12px;
position: relative;
overflow: hidden;
margin-bottom: 10px;
background: #f8f8f8;
}
.product-image {
width: 100%;
height: 100%;
object-fit: cover;
}
.product-img-placeholder {
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
font-size: 40px;
font-weight: bold;
color: rgba(0,0,0,0.1);
margin-bottom: 10px;
position: relative;
overflow: hidden;
}
.product-tag {
@@ -285,6 +400,32 @@ const handleLoadMore = () => {
text-overflow: ellipsis;
}
.product-meta {
font-size: 12px;
color: #999;
margin-bottom: 6px;
display: flex;
justify-content: space-between;
align-items: center;
}
.duration {
background: #f0f0f0;
padding: 2px 8px;
border-radius: 10px;
}
.stats {
display: flex;
gap: 8px;
}
.like-count,
.collect-count {
display: flex;
align-items: center;
}
.price-row {
display: flex;
justify-content: space-between;
@@ -298,8 +439,21 @@ const handleLoadMore = () => {
font-size: 16px;
}
.product-tags {
margin: 8px 0;
display: flex;
flex-wrap: wrap;
}
.load-more {
padding: 20px;
text-align: center;
}
.empty-state {
padding: 60px 20px;
text-align: center;
color: #999;
font-size: 14px;
}
</style>