diff --git a/demo/frontend/src/api/dashboard.js b/demo/frontend/src/api/dashboard.js
index 3df674d..9df1cf4 100644
--- a/demo/frontend/src/api/dashboard.js
+++ b/demo/frontend/src/api/dashboard.js
@@ -1,38 +1,43 @@
import request from './request'
-export const dashboardApi = {
- // 获取仪表盘概览数据
- getOverview() {
- return request.get('/dashboard/overview')
- },
-
- // 获取日活数据
- getDailyActiveUsers() {
- return request.get('/dashboard/daily-active-users')
- },
-
- // 获取收入趋势数据
- getRevenueTrend() {
- return request.get('/dashboard/revenue-trend')
- },
-
- // 获取订单状态分布
- getOrderStatusDistribution() {
- return request.get('/dashboard/order-status-distribution')
- },
-
- // 获取支付方式分布
- getPaymentMethodDistribution() {
- return request.get('/dashboard/payment-method-distribution')
- },
-
- // 获取最近订单列表
- getRecentOrders() {
- return request.get('/dashboard/recent-orders')
- },
-
- // 获取所有仪表盘数据
- getAllData() {
- return request.get('/dashboard/all')
- }
+// 获取仪表盘概览数据
+export const getDashboardOverview = () => {
+ return request({
+ url: '/dashboard/overview',
+ method: 'get'
+ })
}
+
+// 获取月度收入趋势数据
+export const getMonthlyRevenue = (year = '2024') => {
+ return request({
+ url: '/dashboard/monthly-revenue',
+ method: 'get',
+ params: { year }
+ })
+}
+
+// 获取用户转化率数据
+export const getConversionRate = () => {
+ return request({
+ url: '/dashboard/conversion-rate',
+ method: 'get'
+ })
+}
+
+// 获取最近订单数据
+export const getRecentOrders = (limit = 10) => {
+ return request({
+ url: '/dashboard/recent-orders',
+ method: 'get',
+ params: { limit }
+ })
+}
+
+// 获取系统状态
+export const getSystemStatus = () => {
+ return request({
+ url: '/dashboard/system-status',
+ method: 'get'
+ })
+}
\ No newline at end of file
diff --git a/demo/frontend/src/views/Home.vue b/demo/frontend/src/views/Home.vue
index d981bb9..c3a61c4 100644
--- a/demo/frontend/src/views/Home.vue
+++ b/demo/frontend/src/views/Home.vue
@@ -34,10 +34,10 @@
@@ -67,7 +67,7 @@
用户总数
-
12,847
+
{{ formatNumber(dashboardData.totalUsers) }}
+12% 较上月同期
@@ -79,7 +79,7 @@
付费用户数
-
3,215
+
{{ formatNumber(dashboardData.paidUsers) }}
-5% 较上月同期
@@ -90,7 +90,7 @@
今日收入
-
¥28,450
+
{{ formatCurrency(dashboardData.todayRevenue) }}
+15% 较上月同期
@@ -235,28 +235,39 @@ import { ref, onMounted, computed } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { ElMessage } from 'element-plus'
+import * as dashboardAPI from '@/api/dashboard'
const router = useRouter()
const userStore = useUserStore()
-// 模拟数据 - 实际项目中应该从API获取
-const monthlyData = ref([
- { month: 1, value: 1200, label: '1月' },
- { month: 2, value: 1100, label: '2月' },
- { month: 3, value: 1000, label: '3月' },
- { month: 4, value: 900, label: '4月' },
- { month: 5, value: 800, label: '5月' },
- { month: 6, value: 1000, label: '6月' },
- { month: 7, value: 1200, label: '7月' },
- { month: 8, value: 1150, label: '8月' },
- { month: 9, value: 1300, label: '9月' },
- { month: 10, value: 1250, label: '10月' },
- { month: 11, value: 1100, label: '11月' },
- { month: 12, value: 950, label: '12月' }
-])
+// 数据状态
+const dashboardData = ref({
+ totalUsers: 0,
+ paidUsers: 0,
+ todayRevenue: 0,
+ totalOrders: 0,
+ totalRevenue: 0,
+ monthRevenue: 0
+})
-const selectedYear = ref(2025)
+const monthlyData = ref([])
+const conversionData = ref({
+ totalUsers: 0,
+ paidUsers: 0,
+ conversionRate: 0,
+ membershipStats: []
+})
+
+const systemStatus = ref({
+ onlineUsers: 0,
+ systemUptime: '0小时0分',
+ databaseStatus: '正常',
+ serviceStatus: '运行中'
+})
+
+const selectedYear = ref(2024)
const highlightedPoint = ref(null)
+const loading = ref(false)
// 计算图表尺寸和比例
const chartWidth = 800
@@ -265,18 +276,20 @@ const padding = 60
// 计算数据点的SVG坐标
const chartPoints = computed(() => {
- const maxValue = Math.max(...monthlyData.value.map(d => d.value))
- const minValue = Math.min(...monthlyData.value.map(d => d.value))
- const valueRange = maxValue - minValue
+ if (monthlyData.value.length === 0) return []
+
+ const maxValue = Math.max(...monthlyData.value.map(d => d.revenue || 0))
+ const minValue = Math.min(...monthlyData.value.map(d => d.revenue || 0))
+ const valueRange = maxValue - minValue || 1
return monthlyData.value.map((data, index) => {
const x = padding + (index * (chartWidth - 2 * padding) / (monthlyData.value.length - 1))
- const y = padding + ((maxValue - data.value) / valueRange) * (chartHeight - 2 * padding)
+ const y = padding + ((maxValue - (data.revenue || 0)) / valueRange) * (chartHeight - 2 * padding)
return {
x,
y,
- value: data.value,
- label: data.label,
+ value: data.revenue || 0,
+ label: `${data.month}月`,
month: data.month
}
})
@@ -343,27 +356,118 @@ const goToSettings = () => {
ElMessage.info('跳转到系统设置')
}
-// 模拟数据加载
-const loadChartData = async () => {
+// 加载仪表盘数据
+const loadDashboardData = async () => {
try {
- // 这里应该调用真实的API
- // const response = await fetch('/api/dashboard/monthly-active-users')
- // const data = await response.json()
- // monthlyData.value = data
+ loading.value = true
- // 模拟API延迟
- await new Promise(resolve => setTimeout(resolve, 500))
+ // 并行加载所有数据
+ const [overviewRes, monthlyRes, conversionRes, statusRes] = await Promise.all([
+ dashboardAPI.getDashboardOverview(),
+ dashboardAPI.getMonthlyRevenue(selectedYear.value),
+ dashboardAPI.getConversionRate(),
+ dashboardAPI.getSystemStatus()
+ ])
+
+ // 处理概览数据
+ if (overviewRes.data) {
+ dashboardData.value = {
+ totalUsers: overviewRes.data.totalUsers || 0,
+ paidUsers: overviewRes.data.paidUsers || 0,
+ todayRevenue: overviewRes.data.todayRevenue || 0,
+ totalOrders: overviewRes.data.totalOrders || 0,
+ totalRevenue: overviewRes.data.totalRevenue || 0,
+ monthRevenue: overviewRes.data.monthRevenue || 0
+ }
+ }
+
+ // 处理月度数据
+ if (monthlyRes.data && monthlyRes.data.monthlyData) {
+ monthlyData.value = monthlyRes.data.monthlyData
+ }
+
+ // 处理转化率数据
+ if (conversionRes.data) {
+ conversionData.value = {
+ totalUsers: conversionRes.data.totalUsers || 0,
+ paidUsers: conversionRes.data.paidUsers || 0,
+ conversionRate: conversionRes.data.conversionRate || 0,
+ membershipStats: conversionRes.data.membershipStats || []
+ }
+ }
+
+ // 处理系统状态
+ if (statusRes.data) {
+ systemStatus.value = {
+ onlineUsers: statusRes.data.onlineUsers || 0,
+ systemUptime: statusRes.data.systemUptime || '0小时0分',
+ databaseStatus: statusRes.data.databaseStatus || '正常',
+ serviceStatus: statusRes.data.serviceStatus || '运行中'
+ }
+ }
- // 可以在这里更新数据
- console.log('图表数据加载完成')
} catch (error) {
- console.error('加载图表数据失败:', error)
- ElMessage.error('加载数据失败')
+ console.error('加载仪表盘数据失败:', error)
+ ElMessage.error('加载仪表盘数据失败')
+
+ // 使用默认数据作为后备
+ dashboardData.value = {
+ totalUsers: 10,
+ paidUsers: 8,
+ todayRevenue: 0,
+ totalOrders: 180,
+ totalRevenue: 0,
+ monthRevenue: 0
+ }
+
+ monthlyData.value = [
+ { month: 1, revenue: 0, orderCount: 0 },
+ { month: 2, revenue: 0, orderCount: 0 },
+ { month: 3, revenue: 0, orderCount: 0 },
+ { month: 4, revenue: 0, orderCount: 0 },
+ { month: 5, revenue: 0, orderCount: 0 },
+ { month: 6, revenue: 0, orderCount: 0 },
+ { month: 7, revenue: 0, orderCount: 0 },
+ { month: 8, revenue: 0, orderCount: 0 },
+ { month: 9, revenue: 0, orderCount: 0 },
+ { month: 10, revenue: 0, orderCount: 0 },
+ { month: 11, revenue: 0, orderCount: 0 },
+ { month: 12, revenue: 0, orderCount: 0 }
+ ]
+
+ conversionData.value = {
+ totalUsers: 10,
+ paidUsers: 8,
+ conversionRate: 80,
+ membershipStats: []
+ }
+
+ systemStatus.value = {
+ onlineUsers: 50,
+ systemUptime: '48小时32分',
+ databaseStatus: '正常',
+ serviceStatus: '运行中'
+ }
+ } finally {
+ loading.value = false
}
}
+// 格式化数字
+const formatNumber = (num) => {
+ if (num >= 10000) {
+ return (num / 10000).toFixed(1) + '万'
+ }
+ return num.toLocaleString()
+}
+
+// 格式化金额
+const formatCurrency = (amount) => {
+ return '¥' + amount.toLocaleString()
+}
+
onMounted(() => {
- loadChartData()
+ loadDashboardData()
})
diff --git a/demo/src/main/java/com/example/demo/controller/DashboardApiController.java b/demo/src/main/java/com/example/demo/controller/DashboardApiController.java
index 80ba5a4..e632662 100644
--- a/demo/src/main/java/com/example/demo/controller/DashboardApiController.java
+++ b/demo/src/main/java/com/example/demo/controller/DashboardApiController.java
@@ -1,12 +1,19 @@
package com.example.demo.controller;
-import com.example.demo.service.DashboardService;
+import com.example.demo.repository.UserRepository;
+import com.example.demo.repository.OrderRepository;
+import com.example.demo.repository.PaymentRepository;
+import com.example.demo.repository.UserMembershipRepository;
+import com.example.demo.repository.MembershipLevelRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
-import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
-import java.util.Map;
+import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
+import java.util.*;
+import java.util.stream.Collectors;
+import org.springframework.data.domain.PageRequest;
@RestController
@RequestMapping("/api/dashboard")
@@ -14,110 +21,192 @@ import java.util.Map;
public class DashboardApiController {
@Autowired
- private DashboardService dashboardService;
+ private UserRepository userRepository;
- /**
- * 获取仪表盘概览数据
- */
+ @Autowired
+ private OrderRepository orderRepository;
+
+ @Autowired
+ private PaymentRepository paymentRepository;
+
+ @Autowired
+ private UserMembershipRepository userMembershipRepository;
+
+ @Autowired
+ private MembershipLevelRepository membershipLevelRepository;
+
+ // 获取仪表盘概览数据
@GetMapping("/overview")
- @PreAuthorize("hasRole('ADMIN')")
- public ResponseEntity