225 lines
5.5 KiB
Vue
225 lines
5.5 KiB
Vue
<template>
|
|
<div class="dashboard">
|
|
<a-page-header title="仪表盘" sub-title="欢迎使用后台管理系统" />
|
|
|
|
<a-row :gutter="16" class="stat-cards">
|
|
<a-col :span="6">
|
|
<a-card>
|
|
<a-statistic
|
|
title="总用户数"
|
|
:value="stats.totalUsers"
|
|
:value-style="{ color: '#3f8600' }"
|
|
:loading="loading"
|
|
>
|
|
<template #prefix>
|
|
<UserOutlined />
|
|
</template>
|
|
</a-statistic>
|
|
</a-card>
|
|
</a-col>
|
|
<a-col :span="6">
|
|
<a-card>
|
|
<a-statistic
|
|
title="商品总数"
|
|
:value="stats.totalProducts"
|
|
:value-style="{ color: '#1890ff' }"
|
|
:loading="loading"
|
|
>
|
|
<template #prefix>
|
|
<ShopOutlined />
|
|
</template>
|
|
</a-statistic>
|
|
</a-card>
|
|
</a-col>
|
|
<a-col :span="6" v-if="isAdmin">
|
|
<a-card>
|
|
<a-statistic
|
|
title="店铺数量"
|
|
:value="stats.totalShops"
|
|
:value-style="{ color: '#722ed1' }"
|
|
:loading="loading"
|
|
>
|
|
<template #prefix>
|
|
<HomeOutlined />
|
|
</template>
|
|
</a-statistic>
|
|
</a-card>
|
|
</a-col>
|
|
<a-col :span="6">
|
|
<a-card>
|
|
<a-statistic
|
|
title="今日订单"
|
|
:value="stats.todayOrders"
|
|
:value-style="{ color: '#cf1322' }"
|
|
:loading="loading"
|
|
>
|
|
<template #prefix>
|
|
<ShoppingOutlined />
|
|
</template>
|
|
</a-statistic>
|
|
</a-card>
|
|
</a-col>
|
|
</a-row>
|
|
|
|
<a-row :gutter="16" class="charts-section">
|
|
<a-col :span="16">
|
|
<a-card title="订单趋势" :bordered="false" class="chart-card">
|
|
<div class="chart-placeholder">
|
|
<BarChartOutlined :style="{ fontSize: '64px', color: '#d9d9d9' }" />
|
|
<p>订单趋势图表</p>
|
|
</div>
|
|
</a-card>
|
|
</a-col>
|
|
<a-col :span="8">
|
|
<a-card title="热门商品" :bordered="false" class="chart-card">
|
|
<a-spin :spinning="loading">
|
|
<a-list :data-source="stats.hotProducts" size="small">
|
|
<template #renderItem="{ item }">
|
|
<a-list-item>
|
|
<a-list-item-meta>
|
|
<template #title>
|
|
<a href="#">{{ item.title }}</a>
|
|
</template>
|
|
<template #description>
|
|
点赞: {{ item.likeCount }} | 收藏: {{ item.collectCount || 0 }}
|
|
</template>
|
|
</a-list-item-meta>
|
|
</a-list-item>
|
|
</template>
|
|
</a-list>
|
|
</a-spin>
|
|
</a-card>
|
|
</a-col>
|
|
</a-row>
|
|
|
|
<a-card title="最近活动" :bordered="false" class="activity-card">
|
|
<a-spin :spinning="loading">
|
|
<a-timeline>
|
|
<a-timeline-item
|
|
v-for="activity in stats.recentActivities"
|
|
:key="activity.id"
|
|
:color="activity.color"
|
|
>
|
|
<p>{{ activity.description }}</p>
|
|
<p style="font-size: 12px; color: #999;">{{ formatTime(activity.time) }}</p>
|
|
</a-timeline-item>
|
|
</a-timeline>
|
|
</a-spin>
|
|
</a-card>
|
|
</div>
|
|
</template>
|
|
|
|
<script setup lang="ts">
|
|
import { ref, reactive, onMounted, computed } from 'vue'
|
|
import { message } from 'ant-design-vue'
|
|
import {
|
|
UserOutlined,
|
|
ShopOutlined,
|
|
HomeOutlined,
|
|
ShoppingOutlined,
|
|
BarChartOutlined
|
|
} from '@ant-design/icons-vue'
|
|
import { adminDashboardApi, type DashboardStats } from '@/api/admin'
|
|
|
|
const loading = ref(false)
|
|
const stats = reactive<DashboardStats>({
|
|
totalUsers: 0,
|
|
totalProducts: 0,
|
|
totalShops: 0,
|
|
todayOrders: 0,
|
|
hotProducts: [],
|
|
recentActivities: []
|
|
})
|
|
|
|
// 获取当前用户信息
|
|
const getCurrentUserInfo = () => {
|
|
try {
|
|
const userInfoStr = localStorage.getItem('adminUserInfo')
|
|
if (userInfoStr) {
|
|
const userInfo = JSON.parse(userInfoStr)
|
|
return userInfo
|
|
}
|
|
} catch (error) {
|
|
console.error('获取当前用户信息失败', error)
|
|
}
|
|
return null
|
|
}
|
|
|
|
// 判断是否为管理员
|
|
const isAdmin = computed(() => {
|
|
const userInfo = getCurrentUserInfo()
|
|
return userInfo?.type === 4
|
|
})
|
|
|
|
const formatTime = (timeStr: string) => {
|
|
if (!timeStr) return ''
|
|
const time = new Date(timeStr)
|
|
const now = new Date()
|
|
const diff = now.getTime() - time.getTime()
|
|
|
|
const minutes = Math.floor(diff / 60000)
|
|
const hours = Math.floor(diff / 3600000)
|
|
const days = Math.floor(diff / 86400000)
|
|
|
|
if (minutes < 1) return '刚刚'
|
|
if (minutes < 60) return `${minutes}分钟前`
|
|
if (hours < 24) return `${hours}小时前`
|
|
if (days < 7) return `${days}天前`
|
|
|
|
return time.toLocaleDateString('zh-CN')
|
|
}
|
|
|
|
const fetchStats = async () => {
|
|
try {
|
|
loading.value = true
|
|
const data = await adminDashboardApi.getStats()
|
|
Object.assign(stats, data)
|
|
} catch (error) {
|
|
console.error('获取统计数据失败:', error)
|
|
message.error('获取统计数据失败')
|
|
} finally {
|
|
loading.value = false
|
|
}
|
|
}
|
|
|
|
onMounted(() => {
|
|
fetchStats()
|
|
})
|
|
</script>
|
|
|
|
<style scoped>
|
|
.dashboard {
|
|
padding: 0;
|
|
}
|
|
|
|
.stat-cards {
|
|
margin-bottom: 24px;
|
|
}
|
|
|
|
.charts-section {
|
|
margin-bottom: 24px;
|
|
}
|
|
|
|
.chart-card {
|
|
height: 400px;
|
|
}
|
|
|
|
.chart-placeholder {
|
|
display: flex;
|
|
flex-direction: column;
|
|
align-items: center;
|
|
justify-content: center;
|
|
height: 300px;
|
|
color: #999;
|
|
}
|
|
|
|
.chart-placeholder p {
|
|
margin-top: 16px;
|
|
font-size: 14px;
|
|
}
|
|
|
|
.activity-card {
|
|
margin-top: 24px;
|
|
}
|
|
</style>
|