682 lines
15 KiB
Vue
682 lines
15 KiB
Vue
<template>
|
||
<div class="mobile-container">
|
||
|
||
<!-- 头部区域 -->
|
||
<header class="header">
|
||
<!-- 用户信息区域 -->
|
||
<div class="user-info" @click="handleUserClick">
|
||
<div class="avatar">
|
||
<a-avatar :size="40" :src="userInfo?.avatar || 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png'" />
|
||
</div>
|
||
<div class="welcome">
|
||
<p class="greeting">Hello!</p>
|
||
<p class="username">{{ userInfo?.nickname || '用户' }}</p>
|
||
</div>
|
||
</div>
|
||
<!-- 通知按钮 -->
|
||
<div class="actions">
|
||
<a-badge :count="3" :offset="[-5, 5]">
|
||
<BellOutlined class="notification-icon" @click="handleNotificationClick" />
|
||
</a-badge>
|
||
</div>
|
||
</header>
|
||
|
||
<!-- 搜索区域 -->
|
||
<div class="search-section">
|
||
<a-input
|
||
v-model:value="searchQuery"
|
||
placeholder="搜索商品..."
|
||
size="large"
|
||
@input="handleSearch"
|
||
>
|
||
<template #prefix>
|
||
<SearchOutlined style="color: #999" />
|
||
</template>
|
||
</a-input>
|
||
<a-button class="filter-btn" type="primary" shape="circle" @click="handleFilterClick">
|
||
<FilterOutlined />
|
||
</a-button>
|
||
</div>
|
||
|
||
<!-- Banner 轮播区域 -->
|
||
<div class="banner-carousel">
|
||
<a-carousel v-if="bannerImages.length > 0" autoplay :autoplay-speed="3000" effect="fade">
|
||
<div v-for="(image, index) in bannerImages" :key="index" class="banner-slide" @click="handleBannerClick">
|
||
<img :src="image" alt="Banner" class="banner-img" />
|
||
</div>
|
||
</a-carousel>
|
||
<div v-else class="banner" @click="handleBannerClick">
|
||
<div class="banner-content">
|
||
<a-tag color="red" class="banner-tag">限时优惠</a-tag>
|
||
<h3>Get Winter Discount</h3>
|
||
<h2>20% Off</h2>
|
||
<p>For Children</p>
|
||
<a-button type="primary" size="small" class="banner-btn">立即购买</a-button>
|
||
</div>
|
||
<div class="banner-image">
|
||
<div class="img-placeholder">Banner</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- 精选商品区域 -->
|
||
<section class="section">
|
||
<div class="section-header">
|
||
<h3>精选商品</h3>
|
||
<a class="see-all" @click="handleSeeAll('featured')">查看全部 <RightOutlined /></a>
|
||
</div>
|
||
<div class="horizontal-scroll">
|
||
<a-card
|
||
v-for="item in featuredItems"
|
||
:key="item.id"
|
||
class="product-card"
|
||
:body-style="{ padding: '12px' }"
|
||
hoverable
|
||
@click="handleProductClick(item)"
|
||
>
|
||
<div class="product-img" :style="{ backgroundColor: item.bgColor }">
|
||
<span>{{ item.name[0] }}</span>
|
||
</div>
|
||
<div class="product-info">
|
||
<p class="name">{{ item.name }}</p>
|
||
<p class="price">${{ item.price }}</p>
|
||
</div>
|
||
</a-card>
|
||
</div>
|
||
</section>
|
||
|
||
<!-- 热门商品区域 -->
|
||
<section class="section popular-section">
|
||
<div class="section-header">
|
||
<h3>热门商品</h3>
|
||
<a class="see-all" @click="handleSeeAll('popular')">查看全部 <RightOutlined /></a>
|
||
</div>
|
||
<div class="product-grid">
|
||
<a-card
|
||
v-for="item in popularItems"
|
||
:key="item.id"
|
||
class="product-card grid-item"
|
||
:body-style="{ padding: '12px' }"
|
||
hoverable
|
||
@click="handleProductClick(item)"
|
||
>
|
||
<div class="product-img" :style="{ backgroundColor: item.bgColor }">
|
||
<span>{{ item.name[0] }}</span>
|
||
</div>
|
||
<div class="product-info">
|
||
<p class="name">{{ item.name }}</p>
|
||
<p class="price">${{ item.price }}</p>
|
||
</div>
|
||
</a-card>
|
||
</div>
|
||
<!-- 加载状态 -->
|
||
<div v-if="loading" class="loading-more">
|
||
<a-spin size="small" />
|
||
<span style="margin-left: 8px;">加载中...</span>
|
||
</div>
|
||
<!-- 暂无更多商品 -->
|
||
<div v-else-if="!hasMore && popularItems.length > 0" class="no-more">
|
||
暂无更多商品推荐
|
||
</div>
|
||
<!-- 无商品时的提示 -->
|
||
<div v-else-if="popularItems.length === 0 && !loading" class="no-products">
|
||
暂无热门商品
|
||
</div>
|
||
</section>
|
||
</div>
|
||
</template>
|
||
|
||
<script setup lang="ts">
|
||
import { ref, onMounted, onUnmounted } from 'vue'
|
||
import { useRouter } from 'vue-router'
|
||
import {
|
||
BellOutlined,
|
||
SearchOutlined,
|
||
FilterOutlined,
|
||
RightOutlined
|
||
} from '@ant-design/icons-vue'
|
||
import { userApi, type UserVO } from '../api/user'
|
||
import { productApi } from '../api/product'
|
||
import { shopApi, type ShopVO } from '../api/shop'
|
||
import { fileApi } from '../api/file'
|
||
|
||
const router = useRouter()
|
||
|
||
// 用户信息
|
||
const userInfo = ref<UserVO>({
|
||
id: 0,
|
||
nickname: '',
|
||
account: '',
|
||
avatar: '',
|
||
type: 0,
|
||
status: 0,
|
||
createTime: '',
|
||
updateTime: ''
|
||
})
|
||
|
||
// 搜索关键词
|
||
const searchQuery = ref('')
|
||
|
||
// Banner图片数据
|
||
const bannerImages = ref<string[]>([])
|
||
|
||
// 精选商品数据
|
||
const featuredItems = ref<any[]>([])
|
||
|
||
// 热门商品数据
|
||
const popularItems = ref<any[]>([])
|
||
const loading = ref(false)
|
||
const hasMore = ref(true)
|
||
const page = ref(1)
|
||
const pageSize = ref(10)
|
||
|
||
// 加载用户信息
|
||
const loadUserInfo = async () => {
|
||
try {
|
||
const response = await userApi.getUserInfo() as any
|
||
userInfo.value = response || {}
|
||
} catch (error) {
|
||
console.error('获取用户信息失败:', error)
|
||
}
|
||
}
|
||
|
||
// 加载Banner图片
|
||
const loadBannerImages = async () => {
|
||
try {
|
||
const fileIdPromises: Promise<string>[] = []
|
||
|
||
// 从店铺获取图片
|
||
const shops = await shopApi.getAllShops() as ShopVO[]
|
||
console.log('店铺数量:', shops.length)
|
||
console.log('店铺数据:', shops)
|
||
|
||
// 处理店铺图片 - 只使用coverImageFileId(banner图片)
|
||
for (const shop of shops) {
|
||
// 处理coverImageFileId(专门用于banner的图片)
|
||
if (shop.coverImageFileId) {
|
||
const fileIds = shop.coverImageFileId.split(',').filter(id => id.trim())
|
||
for (const idStr of fileIds) {
|
||
const fileId = parseInt(idStr)
|
||
if (!isNaN(fileId)) {
|
||
const promise = fileApi.getSignedUrl(fileId).catch(err => {
|
||
console.error('获取店铺banner图签名失败:', err)
|
||
return null
|
||
})
|
||
fileIdPromises.push(promise)
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
// 并行请求所有签名URL
|
||
const results = await Promise.all(fileIdPromises)
|
||
console.log('原始请求结果:', results)
|
||
|
||
// 过滤掉null值并去除反引号
|
||
const filteredResults = results.filter((url): url is string => url !== null).map(url => url.replace(/[`]/g, '').trim())
|
||
console.log('过滤后结果:', filteredResults)
|
||
|
||
// 去重
|
||
const uniqueImages = [...new Set(filteredResults)]
|
||
console.log('去重后结果:', uniqueImages)
|
||
console.log('去重后数量:', uniqueImages.length)
|
||
|
||
// 限制数量并更新bannerImages
|
||
bannerImages.value = uniqueImages.slice(0, 10) // 最多显示10张
|
||
console.log('Banner图片数量:', bannerImages.value.length)
|
||
console.log('Banner图片列表:', bannerImages.value)
|
||
} catch (error) {
|
||
console.error('获取Banner图片失败:', error)
|
||
}
|
||
}
|
||
|
||
// 加载精选商品
|
||
const loadFeaturedProducts = async () => {
|
||
try {
|
||
const response = await productApi.getProductsByTag('精选') as any
|
||
const data = response || []
|
||
const productList = Array.isArray(data) ? data : []
|
||
const shuffled = productList.sort(() => 0.5 - Math.random())
|
||
featuredItems.value = shuffled.slice(0, 5).map((item: any) => ({
|
||
id: item.id,
|
||
name: item.name,
|
||
price: item.price,
|
||
bgColor: getRandomColor()
|
||
}))
|
||
} catch (error) {
|
||
console.error('获取精选商品失败:', error)
|
||
}
|
||
}
|
||
|
||
// 加载热门商品
|
||
const loadPopularProducts = async (loadMore = false) => {
|
||
if (loading.value || !hasMore.value) return
|
||
|
||
loading.value = true
|
||
try {
|
||
const response = await productApi.getProductsByTag('热门') as any
|
||
const data = response || []
|
||
const productList = Array.isArray(data) ? data : []
|
||
|
||
if (loadMore) {
|
||
const start = (page.value - 1) * pageSize.value
|
||
const end = start + pageSize.value
|
||
const newItems = productList.slice(start, end)
|
||
|
||
if (newItems.length < pageSize.value) {
|
||
hasMore.value = false
|
||
}
|
||
|
||
popularItems.value = [...popularItems.value, ...newItems.map((item: any) => ({
|
||
id: item.id,
|
||
name: item.name,
|
||
price: item.price,
|
||
bgColor: getRandomColor()
|
||
}))]
|
||
} else {
|
||
popularItems.value = productList.slice(0, pageSize.value).map((item: any) => ({
|
||
id: item.id,
|
||
name: item.name,
|
||
price: item.price,
|
||
bgColor: getRandomColor()
|
||
}))
|
||
|
||
if (productList.length <= pageSize.value) {
|
||
hasMore.value = false
|
||
}
|
||
}
|
||
} catch (error) {
|
||
console.error('获取热门商品失败:', error)
|
||
} finally {
|
||
loading.value = false
|
||
}
|
||
}
|
||
|
||
// 处理滚动事件,实现懒加载
|
||
const handleScroll = () => {
|
||
const scrollTop = document.documentElement.scrollTop || document.body.scrollTop
|
||
const scrollHeight = document.documentElement.scrollHeight || document.body.scrollHeight
|
||
const clientHeight = document.documentElement.clientHeight
|
||
|
||
if (scrollTop + clientHeight >= scrollHeight - 100 && !loading.value && hasMore.value) {
|
||
page.value++
|
||
loadPopularProducts(true)
|
||
}
|
||
}
|
||
|
||
// 添加滚动监听
|
||
onMounted(() => {
|
||
loadUserInfo()
|
||
loadBannerImages()
|
||
loadFeaturedProducts()
|
||
loadPopularProducts()
|
||
window.addEventListener('scroll', handleScroll)
|
||
})
|
||
|
||
// 移除滚动监听
|
||
onUnmounted(() => {
|
||
window.removeEventListener('scroll', handleScroll)
|
||
})
|
||
|
||
// 生成随机颜色
|
||
const getRandomColor = () => {
|
||
const colors = ['#ffe4d6', '#d6eaff', '#e2ffd6', '#fff2d6', '#d6ffe4', '#e4d6ff']
|
||
return colors[Math.floor(Math.random() * colors.length)]
|
||
}
|
||
|
||
// 处理用户头像点击
|
||
const handleUserClick = () => {
|
||
console.log('用户头像被点击')
|
||
router.push('/profile')
|
||
}
|
||
|
||
// 处理通知图标点击
|
||
const handleNotificationClick = () => {
|
||
console.log('通知图标被点击')
|
||
}
|
||
|
||
// 处理搜索输入
|
||
const handleSearch = (e: any) => {
|
||
console.log('搜索内容改变:', e.target.value)
|
||
}
|
||
|
||
// 处理筛选按钮点击
|
||
const handleFilterClick = () => {
|
||
console.log('筛选按钮被点击')
|
||
}
|
||
|
||
// 处理 Banner 点击
|
||
const handleBannerClick = () => {
|
||
console.log('Banner被点击')
|
||
}
|
||
|
||
// 处理查看全部点击
|
||
const handleSeeAll = (type: string) => {
|
||
console.log(`查看全部: ${type}`)
|
||
router.push('/products')
|
||
}
|
||
|
||
// 处理商品卡片点击
|
||
const handleProductClick = (item: any) => {
|
||
console.log('商品被点击:', item.name)
|
||
router.push(`/product/${item.id}`)
|
||
}
|
||
</script>
|
||
|
||
<style scoped>
|
||
.header {
|
||
display: flex;
|
||
justify-content: space-between;
|
||
align-items: center;
|
||
padding: 15px 20px;
|
||
}
|
||
|
||
.user-info {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 12px;
|
||
cursor: pointer;
|
||
transition: transform 0.2s;
|
||
}
|
||
|
||
.user-info:hover {
|
||
transform: scale(1.02);
|
||
}
|
||
|
||
.greeting {
|
||
font-size: 12px;
|
||
color: #999;
|
||
margin: 0;
|
||
}
|
||
|
||
.username {
|
||
font-weight: bold;
|
||
font-size: 16px;
|
||
margin: 0;
|
||
}
|
||
|
||
.actions {
|
||
display: flex;
|
||
align-items: center;
|
||
}
|
||
|
||
.notification-icon {
|
||
font-size: 24px;
|
||
cursor: pointer;
|
||
color: #333;
|
||
transition: color 0.3s;
|
||
}
|
||
|
||
.notification-icon:hover {
|
||
color: #1890ff;
|
||
}
|
||
|
||
.search-section {
|
||
display: flex;
|
||
padding: 10px 20px;
|
||
gap: 10px;
|
||
align-items: center;
|
||
}
|
||
|
||
.search-section :deep(.ant-input) {
|
||
border-radius: 20px;
|
||
height: 44px;
|
||
}
|
||
|
||
.filter-btn {
|
||
width: 44px;
|
||
height: 44px;
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: center;
|
||
flex-shrink: 0;
|
||
}
|
||
|
||
/* Banner轮播区域 */
|
||
.banner-carousel {
|
||
margin: 15px 20px;
|
||
border-radius: 16px;
|
||
overflow: hidden;
|
||
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
|
||
}
|
||
|
||
.banner-carousel :deep(.ant-carousel) {
|
||
border-radius: 16px;
|
||
}
|
||
|
||
.banner-slide {
|
||
width: 100%;
|
||
height: 180px;
|
||
cursor: pointer;
|
||
}
|
||
|
||
.banner-img {
|
||
width: 100%;
|
||
height: 100%;
|
||
object-fit: cover;
|
||
border-radius: 16px;
|
||
}
|
||
|
||
/* 轮播指示器样式 */
|
||
.banner-carousel :deep(.slick-dots) {
|
||
bottom: 12px;
|
||
}
|
||
|
||
.banner-carousel :deep(.slick-dots li) {
|
||
margin: 0 4px;
|
||
}
|
||
|
||
.banner-carousel :deep(.slick-dots li button) {
|
||
width: 8px;
|
||
height: 8px;
|
||
border-radius: 50%;
|
||
background: rgba(255, 255, 255, 0.6);
|
||
}
|
||
|
||
.banner-carousel :deep(.slick-dots li.slick-active button) {
|
||
width: 20px;
|
||
border-radius: 4px;
|
||
background: #fff;
|
||
}
|
||
|
||
.banner {
|
||
margin: 15px 20px;
|
||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||
border-radius: 20px;
|
||
height: 160px;
|
||
padding: 20px;
|
||
display: flex;
|
||
justify-content: space-between;
|
||
align-items: center;
|
||
color: white;
|
||
overflow: hidden;
|
||
position: relative;
|
||
cursor: pointer;
|
||
transition: transform 0.3s, box-shadow 0.3s;
|
||
}
|
||
|
||
.banner:hover {
|
||
transform: translateY(-4px);
|
||
box-shadow: 0 8px 20px rgba(102, 126, 234, 0.4);
|
||
}
|
||
|
||
.banner-tag {
|
||
margin-bottom: 8px;
|
||
border: none;
|
||
font-weight: 500;
|
||
}
|
||
|
||
.banner-content h3 {
|
||
font-size: 14px;
|
||
opacity: 0.9;
|
||
margin: 0 0 4px 0;
|
||
}
|
||
|
||
.banner-content h2 {
|
||
font-size: 28px;
|
||
margin: 4px 0;
|
||
font-weight: bold;
|
||
text-shadow: 2px 2px 4px rgba(0,0,0,0.2);
|
||
}
|
||
|
||
.banner-content p {
|
||
font-size: 14px;
|
||
margin: 4px 0 12px 0;
|
||
opacity: 0.9;
|
||
}
|
||
|
||
.banner-btn {
|
||
border-radius: 20px;
|
||
height: 32px;
|
||
font-weight: 500;
|
||
}
|
||
|
||
.banner-image {
|
||
flex: 1;
|
||
display: flex;
|
||
justify-content: center;
|
||
align-items: center;
|
||
}
|
||
|
||
.img-placeholder {
|
||
width: 100px;
|
||
height: 100px;
|
||
background: rgba(255,255,255,0.2);
|
||
border-radius: 50%;
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: center;
|
||
font-size: 14px;
|
||
backdrop-filter: blur(10px);
|
||
}
|
||
|
||
.section {
|
||
padding: 15px 0;
|
||
}
|
||
|
||
.section-header {
|
||
display: flex;
|
||
justify-content: space-between;
|
||
align-items: center;
|
||
padding: 0 20px 15px;
|
||
}
|
||
|
||
.section-header h3 {
|
||
font-size: 18px;
|
||
font-weight: bold;
|
||
margin: 0;
|
||
}
|
||
|
||
.see-all {
|
||
color: #1890ff;
|
||
font-size: 13px;
|
||
cursor: pointer;
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 4px;
|
||
transition: color 0.3s;
|
||
}
|
||
|
||
.see-all:hover {
|
||
color: #40a9ff;
|
||
}
|
||
|
||
.horizontal-scroll {
|
||
display: flex;
|
||
overflow-x: auto;
|
||
padding: 0 20px;
|
||
gap: 15px;
|
||
scrollbar-width: none;
|
||
}
|
||
|
||
.horizontal-scroll::-webkit-scrollbar {
|
||
display: none;
|
||
}
|
||
|
||
.product-card {
|
||
width: 140px;
|
||
flex-shrink: 0;
|
||
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;
|
||
display: flex;
|
||
justify-content: center;
|
||
align-items: center;
|
||
font-size: 40px;
|
||
font-weight: bold;
|
||
color: rgba(0,0,0,0.1);
|
||
margin-bottom: 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;
|
||
}
|
||
|
||
.price {
|
||
color: #ff4d4f;
|
||
font-weight: bold;
|
||
margin: 0;
|
||
font-size: 16px;
|
||
}
|
||
|
||
.product-grid {
|
||
display: grid;
|
||
grid-template-columns: repeat(2, 1fr);
|
||
padding: 0 20px;
|
||
gap: 15px;
|
||
}
|
||
|
||
.grid-item {
|
||
width: 100%;
|
||
}
|
||
|
||
.popular-section {
|
||
padding-bottom: 20px;
|
||
}
|
||
|
||
.loading-more {
|
||
display: flex;
|
||
justify-content: center;
|
||
align-items: center;
|
||
padding: 20px 0;
|
||
color: #999;
|
||
font-size: 14px;
|
||
}
|
||
|
||
.no-more {
|
||
text-align: center;
|
||
padding: 20px 0;
|
||
color: #999;
|
||
font-size: 14px;
|
||
}
|
||
|
||
.no-products {
|
||
text-align: center;
|
||
padding: 40px 0;
|
||
color: #999;
|
||
font-size: 14px;
|
||
}
|
||
</style>
|