后台管理功能进行一些优化
This commit is contained in:
+166
-13
@@ -6,11 +6,11 @@
|
||||
<!-- 用户信息区域 -->
|
||||
<div class="user-info" @click="handleUserClick">
|
||||
<div class="avatar">
|
||||
<a-avatar :size="40" src="https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png" />
|
||||
<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">昵称</p>
|
||||
<p class="username">{{ userInfo.nickname || '用户' }}</p>
|
||||
</div>
|
||||
</div>
|
||||
<!-- 通知按钮 -->
|
||||
@@ -102,12 +102,25 @@
|
||||
</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 } from 'vue'
|
||||
import { ref, onMounted, onUnmounted } from 'vue'
|
||||
import { useRouter } from 'vue-router'
|
||||
import {
|
||||
BellOutlined,
|
||||
@@ -115,25 +128,142 @@ import {
|
||||
FilterOutlined,
|
||||
RightOutlined
|
||||
} from '@ant-design/icons-vue'
|
||||
import { userApi, type UserVO } from '../api/user'
|
||||
import { productApi } from '../api/product'
|
||||
|
||||
const router = useRouter()
|
||||
|
||||
// 用户信息
|
||||
const userInfo = ref<UserVO>({
|
||||
id: 0,
|
||||
nickname: '',
|
||||
account: '',
|
||||
avatar: '',
|
||||
createTime: '',
|
||||
updateTime: ''
|
||||
})
|
||||
|
||||
// 搜索关键词
|
||||
const searchQuery = ref('')
|
||||
|
||||
// 精选商品数据
|
||||
const featuredItems = ref([
|
||||
{ id: 1, name: 'Watch', price: 40, bgColor: '#ffe4d6' },
|
||||
{ id: 2, name: 'Nike Shoes', price: 430, bgColor: '#d6eaff' },
|
||||
{ id: 3, name: 'Airpods', price: 333, bgColor: '#e2ffd6' }
|
||||
])
|
||||
const featuredItems = ref<any[]>([])
|
||||
|
||||
// 热门商品数据
|
||||
const popularItems = ref([
|
||||
{ id: 4, name: 'LG TV', price: 330, bgColor: '#f0d6ff' },
|
||||
{ id: 5, name: 'Hoodie', price: 50, bgColor: '#ffd6e2' },
|
||||
{ id: 6, name: 'Jacket', price: 400, bgColor: '#d6fff0' }
|
||||
])
|
||||
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 data = await userApi.getUserInfo()
|
||||
userInfo.value = data as unknown as UserVO
|
||||
} catch (error) {
|
||||
console.error('获取用户信息失败:', error)
|
||||
}
|
||||
}
|
||||
|
||||
// 加载精选商品
|
||||
const loadFeaturedProducts = async () => {
|
||||
try {
|
||||
const data = await productApi.getProductsByTag('精选')
|
||||
// 随机选择5个商品
|
||||
const shuffled = data.sort(() => 0.5 - Math.random())
|
||||
featuredItems.value = shuffled.slice(0, 5).map(item => ({
|
||||
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 data = await productApi.getProductsByTag('热门')
|
||||
|
||||
if (loadMore) {
|
||||
// 模拟分页,实际应该在API中实现
|
||||
const start = (page.value - 1) * pageSize.value
|
||||
const end = start + pageSize.value
|
||||
const newItems = data.slice(start, end)
|
||||
|
||||
if (newItems.length < pageSize.value) {
|
||||
hasMore.value = false
|
||||
}
|
||||
|
||||
popularItems.value = [...popularItems.value, ...newItems.map(item => ({
|
||||
id: item.id,
|
||||
name: item.name,
|
||||
price: item.price,
|
||||
bgColor: getRandomColor()
|
||||
}))]
|
||||
} else {
|
||||
// 初始加载
|
||||
popularItems.value = data.slice(0, pageSize.value).map(item => ({
|
||||
id: item.id,
|
||||
name: item.name,
|
||||
price: item.price,
|
||||
bgColor: getRandomColor()
|
||||
}))
|
||||
|
||||
if (data.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()
|
||||
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)]
|
||||
}
|
||||
|
||||
// 页面加载时获取数据
|
||||
onMounted(() => {
|
||||
loadUserInfo()
|
||||
loadFeaturedProducts()
|
||||
loadPopularProducts()
|
||||
})
|
||||
|
||||
// 处理用户头像点击
|
||||
const handleUserClick = () => {
|
||||
@@ -419,4 +549,27 @@ const handleProductClick = (item: any) => {
|
||||
.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>
|
||||
|
||||
Reference in New Issue
Block a user