Files
JayuCloud/Frontend/src/views/HomeView.vue
T

684 lines
15 KiB
Vue
Raw Normal View History

<template>
<div class="mobile-container">
<!-- 头部区域 -->
<header class="header">
<!-- 用户信息区域 -->
<div class="user-info" @click="handleUserClick">
<div class="avatar">
2026-03-02 10:13:20 +08:00
<a-avatar :size="40" :src="userInfo?.avatar || 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png'" />
</div>
<div class="welcome">
<p class="greeting">Hello!</p>
2026-03-02 10:13:20 +08:00
<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"
2026-03-05 16:08:27 +08:00
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 轮播区域 -->
2026-03-05 15:15:59 +08:00
<div class="banner-container">
<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>
</div>
2026-03-05 16:08:27 +08:00
<!-- 精选作品区域 -->
<section class="section">
<div class="section-header">
2026-03-05 16:08:27 +08:00
<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>
2026-03-05 16:08:27 +08:00
<!-- 热门作品区域 -->
<section class="section popular-section">
<div class="section-header">
2026-03-05 16:08:27 +08:00
<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>
2026-02-28 14:32:28 +08:00
<!-- 加载状态 -->
<div v-if="loading" class="loading-more">
<a-spin size="small" />
<span style="margin-left: 8px;">加载中...</span>
</div>
2026-03-05 16:08:27 +08:00
<!-- 暂无更多作品 -->
2026-02-28 14:32:28 +08:00
<div v-else-if="!hasMore && popularItems.length > 0" class="no-more">
2026-03-05 16:08:27 +08:00
暂无更多作品推荐
2026-02-28 14:32:28 +08:00
</div>
2026-03-05 16:08:27 +08:00
<!-- 无作品时的提示 -->
2026-02-28 14:32:28 +08:00
<div v-else-if="popularItems.length === 0 && !loading" class="no-products">
2026-03-05 16:08:27 +08:00
暂无热门作品
2026-02-28 14:32:28 +08:00
</div>
</section>
</div>
</template>
<script setup lang="ts">
2026-02-28 14:32:28 +08:00
import { ref, onMounted, onUnmounted } from 'vue'
import { useRouter } from 'vue-router'
import {
BellOutlined,
SearchOutlined,
2026-02-24 10:57:00 +08:00
FilterOutlined,
RightOutlined
} from '@ant-design/icons-vue'
2026-02-28 14:32:28 +08:00
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()
2026-02-28 14:32:28 +08:00
// 用户信息
const userInfo = ref<UserVO>({
id: 0,
nickname: '',
account: '',
avatar: '',
2026-03-02 10:13:20 +08:00
type: 0,
status: 0,
2026-02-28 14:32:28 +08:00
createTime: '',
updateTime: ''
})
// 搜索关键词
const searchQuery = ref('')
// Banner图片数据
const bannerImages = ref<string[]>([])
2026-03-05 16:08:27 +08:00
// 精选作品数据
2026-02-28 14:32:28 +08:00
const featuredItems = ref<any[]>([])
2026-03-05 16:08:27 +08:00
// 热门作品数据
2026-02-28 14:32:28 +08:00
const popularItems = ref<any[]>([])
const loading = ref(false)
const hasMore = ref(true)
const page = ref(1)
const pageSize = ref(10)
// 加载用户信息
const loadUserInfo = async () => {
try {
2026-03-02 10:13:20 +08:00
const response = await userApi.getUserInfo() as any
userInfo.value = response || {}
2026-02-28 14:32:28 +08:00
} catch (error) {
console.error('获取用户信息失败:', error)
}
}
// 加载Banner图片
const loadBannerImages = async () => {
try {
const fileIdPromises: Promise<string>[] = []
// 从店铺获取图片
const shops = await shopApi.getAllShops() as ShopVO[]
// 处理店铺图片 - 只使用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)
// 过滤掉null值并去除反引号
const filteredResults = results.filter((url): url is string => url !== null).map(url => url.replace(/[`]/g, '').trim())
// 去重
const uniqueImages = [...new Set(filteredResults)]
// 限制数量并更新bannerImages
bannerImages.value = uniqueImages.slice(0, 10) // 最多显示10张
} catch (error) {
console.error('获取Banner图片失败:', error)
}
}
2026-03-05 16:08:27 +08:00
// 加载精选作品
2026-02-28 14:32:28 +08:00
const loadFeaturedProducts = async () => {
try {
2026-03-02 10:13:20 +08:00
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) => ({
2026-02-28 14:32:28 +08:00
id: item.id,
name: item.name,
price: item.price,
bgColor: getRandomColor()
}))
} catch (error) {
2026-03-05 16:08:27 +08:00
console.error('获取精选作品失败:', error)
2026-02-28 14:32:28 +08:00
}
}
2026-03-05 16:08:27 +08:00
// 加载热门作品
2026-02-28 14:32:28 +08:00
const loadPopularProducts = async (loadMore = false) => {
if (loading.value || !hasMore.value) return
loading.value = true
try {
2026-03-02 10:13:20 +08:00
const response = await productApi.getProductsByTag('热门') as any
const data = response || []
const productList = Array.isArray(data) ? data : []
2026-02-28 14:32:28 +08:00
if (loadMore) {
const start = (page.value - 1) * pageSize.value
const end = start + pageSize.value
2026-03-02 10:13:20 +08:00
const newItems = productList.slice(start, end)
2026-02-28 14:32:28 +08:00
if (newItems.length < pageSize.value) {
hasMore.value = false
}
2026-03-02 10:13:20 +08:00
popularItems.value = [...popularItems.value, ...newItems.map((item: any) => ({
2026-02-28 14:32:28 +08:00
id: item.id,
name: item.name,
price: item.price,
bgColor: getRandomColor()
}))]
} else {
2026-03-02 10:13:20 +08:00
popularItems.value = productList.slice(0, pageSize.value).map((item: any) => ({
2026-02-28 14:32:28 +08:00
id: item.id,
name: item.name,
price: item.price,
bgColor: getRandomColor()
}))
2026-03-02 10:13:20 +08:00
if (productList.length <= pageSize.value) {
2026-02-28 14:32:28 +08:00
hasMore.value = false
}
}
} catch (error) {
2026-03-05 16:08:27 +08:00
console.error('获取热门作品失败:', error)
2026-02-28 14:32:28 +08:00
} 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()
2026-02-28 14:32:28 +08:00
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 = () => {
router.push('/profile')
}
// 处理通知图标点击
const handleNotificationClick = () => {
}
// 处理搜索输入
2026-03-02 10:13:20 +08:00
const handleSearch = (e: any) => {
}
// 处理筛选按钮点击
const handleFilterClick = () => {
}
// 处理 Banner 点击
const handleBannerClick = () => {
}
// 处理查看全部点击
const handleSeeAll = (type: string) => {
router.push('/products')
}
2026-03-05 16:08:27 +08:00
// 处理作品卡片点击
const handleProductClick = (item: any) => {
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轮播区域 */
2026-03-05 15:15:59 +08:00
.banner-container {
display: flex;
justify-content: center;
margin: 15px 0;
max-width: 1200px;
width: 100%;
margin-left: auto;
margin-right: auto;
padding: 0 20px;
}
.banner-carousel {
2026-03-05 15:15:59 +08:00
width: 100%;
border-radius: 16px;
overflow: hidden;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
2026-03-05 15:15:59 +08:00
aspect-ratio: 4/3;
max-height: 450px;
}
.banner-carousel :deep(.ant-carousel) {
border-radius: 16px;
2026-03-05 15:15:59 +08:00
height: 100%;
}
.banner-slide {
width: 100%;
2026-03-05 15:15:59 +08:00
height: 100%;
cursor: pointer;
}
.banner-img {
width: 100%;
height: 100%;
object-fit: cover;
border-radius: 16px;
2026-03-05 15:15:59 +08:00
object-fit: stretch;
}
/* 轮播指示器样式 */
.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 {
2026-03-05 15:15:59 +08:00
width: 100%;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
border-radius: 20px;
2026-03-05 15:15:59 +08:00
height: 100%;
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;
}
2026-02-28 14:32:28 +08:00
.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;
}
2026-02-24 10:57:00 +08:00
</style>