Files
JayuCloud/Frontend/src/views/ProductListView.vue
T
2026-03-02 10:13:20 +08:00

462 lines
10 KiB
Vue

<template>
<div class="mobile-container">
<!-- 头部区域 -->
<header class="header">
<a-button class="back-btn" type="text" @click="handleBackClick">
<LeftOutlined :style="{ fontSize: '20px' }" />
</a-button>
<h1>商品列表</h1>
<a-button class="search-btn" type="text">
<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">
<!-- 商品网格 -->
<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="item.image"
:src="item.image"
:alt="item.name"
class="product-image"
/>
<div v-else class="product-img-placeholder">
<span>{{ item.name ? item.name[0] : '商' }}</span>
</div>
<a-badge
v-if="item.category"
:text="item.category"
color="#ff4d4f"
class="product-tag"
/>
</div>
<!-- 商品信息 -->
<div class="product-info">
<p class="name">{{ item.name }}</p>
<div class="product-meta">
<span class="duration">{{ item.estimatedTime }} 分钟</span>
<div class="stats">
<span class="like-count">0 赞</span>
<span class="collect-count">0 收藏</span>
</div>
</div>
<!-- 标签展示 -->
<div class="product-tags">
<template v-if="item.category">
<a-tag
size="small"
:style="{
marginRight: '4px',
marginBottom: '4px',
fontSize: '9px',
padding: '1px 6px',
color: '#333333',
backgroundColor: '#F0F0F0',
borderColor: '#F0F0F0',
borderWidth: '1px',
borderStyle: 'solid',
borderRadius: '8px'
}"
>
{{ item.category }}
</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 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, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import {
LeftOutlined,
SearchOutlined,
ShoppingCartOutlined,
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'
import { cartApi } from '@/api/cart'
const router = useRouter()
// 标签列表
const allTags = ref<TagVO[]>([])
// 当前选中的分类
const selectedCategory = ref('全部')
// 分类列表(动态从标签获取)
const categories = ref<string[]>(['全部'])
// 商品数据
const products = ref<ProductVO[]>([])
// 是否还有更多数据
const hasMore = ref(true)
// 加载状态
const loading = ref(false)
// 从后端获取标签列表
const fetchTags = async () => {
try {
const response = await tagApi.getAllTags() as any
allTags.value = response || []
categories.value = ['全部', ...allTags.value.map(tag => tag.tagName)]
} catch (error) {
console.error('获取标签失败:', error)
}
}
// 从后端获取商品数据
const fetchProducts = async () => {
if (loading.value) return
loading.value = true
try {
let response: any
if (selectedCategory.value === '全部') {
response = await productApi.getAllProducts() as any
} else {
response = await productApi.getProductsByTag(selectedCategory.value) as any
}
console.log('Products response:', response)
products.value = response || []
console.log('Processed products:', products.value)
hasMore.value = false
} catch (error) {
console.error('获取商品失败:', error)
message.error('未登录,请先登录')
} finally {
loading.value = false
}
}
// 处理返回按钮点击
const handleBackClick = () => {
console.log('返回按钮被点击')
router.push('/')
}
// 处理分类点击
const handleCategoryClick = (category: string) => {
console.log('选择分类:', category)
selectedCategory.value = category
// 这里可以添加筛选逻辑
fetchProducts()
}
// 处理商品卡片点击
const handleProductClick = (item: any) => {
console.log('商品被点击:', item.name)
router.push(`/product/${item.id}`)
}
// 处理添加到购物车
const handleAddToCart = async (item: any) => {
console.log('添加到购物车:', item.name)
try {
await cartApi.addToCart({ productId: item.id, quantity: 1 })
message.success(`已将 ${item.name} 添加到购物车`)
} catch (error) {
console.error('添加到购物车失败:', error)
message.error('添加到购物车失败,请重试')
}
}
// 处理加载更多
const handleLoadMore = () => {
console.log('加载更多商品')
// 这里可以添加加载更多逻辑
hasMore.value = false
}
// 页面挂载时获取标签和商品数据
onMounted(async () => {
await fetchTags()
fetchProducts()
})
</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 {
font-size: 18px;
font-weight: bold;
margin: 0;
}
.back-btn,
.search-btn {
width: 40px;
height: 40px;
display: flex;
align-items: center;
justify-content: center;
}
/* 主内容区域 */
.main-content {
display: flex;
height: calc(100vh - 60px);
overflow: hidden;
}
/* 左侧分类边栏 */
.category-sidebar {
width: 90px;
background: #f8f8f8;
overflow-y: auto;
border-right: 1px solid #e8e8e8;
}
.category-item {
padding: 16px 12px;
font-size: 13px;
color: #666;
text-align: center;
cursor: pointer;
transition: all 0.3s;
border-left: 3px solid transparent;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.category-item:hover {
background: #e8e8e8;
color: #1890ff;
}
.category-item.active {
background: white;
color: #1890ff;
border-left-color: #1890ff;
font-weight: 600;
}
/* 右侧商品区域 */
.product-section {
flex: 1;
overflow-y: auto;
background: #f5f5f5;
}
.product-grid {
display: grid;
grid-template-columns: repeat(2, 1fr);
padding: 0 20px;
gap: 15px;
}
.product-card {
cursor: pointer;
transition: transform 0.3s, box-shadow 0.3s;
}
.product-card:hover {
transform: translateY(-4px);
}
.product-card :deep(.ant-card-body) {
padding: 12px;
}
.product-img {
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);
}
.product-tag {
position: absolute;
top: 8px;
left: 8px;
font-size: 11px;
padding: 2px 8px;
border-radius: 8px;
}
.product-info {
padding: 0;
}
.name {
font-size: 14px;
font-weight: 500;
margin: 0 0 4px 0;
white-space: nowrap;
overflow: hidden;
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;
align-items: center;
}
.price {
color: #ff4d4f;
font-weight: bold;
margin: 0;
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>