feat(dashboard): 实现仪表盘数据服务

- 新增 DashboardController 提供后端API
- 新增 dashboard.ts 前端服务
- 更新 ApiDataService 集成仪表盘数据
- 完成任务 TASK-401-404 (96%完成)

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Your Name
2026-03-05 13:13:49 +08:00
parent eee05426ad
commit 06c4eceebe
5 changed files with 378 additions and 19 deletions

View File

@@ -1,3 +1,5 @@
import { getDashboard } from '../dashboard'
const baseUrl = import.meta.env.VITE_MOSQUITO_API_BASE_URL ?? ''
const apiKey = import.meta.env.VITE_MOSQUITO_API_KEY ?? ''
const userToken = import.meta.env.VITE_MOSQUITO_USER_TOKEN ?? ''
@@ -19,11 +21,22 @@ const requestJson = async (url: string) => {
export const apiDataService = {
async getDashboard() {
return {
updatedAt: '刚刚',
kpis: [],
activities: [],
alerts: []
try {
const data = await getDashboard()
return {
updatedAt: data.updatedAt,
kpis: data.kpis,
activities: data.activities,
alerts: data.alerts
}
} catch (error) {
console.error('Failed to fetch dashboard:', error)
return {
updatedAt: '刚刚',
kpis: [],
activities: [],
alerts: []
}
}
},
async getActivities() {

View File

@@ -0,0 +1,111 @@
import axios from 'axios'
const baseURL = import.meta.env.VITE_API_BASE_URL ?? '/api'
const dashboardApi = axios.create({
baseURL,
headers: {
'Content-Type': 'application/json'
}
})
// 请求拦截器 - 添加认证头
dashboardApi.interceptors.request.use(
(config) => {
const apiKey = localStorage.getItem('apiKey')
if (apiKey) {
config.headers['X-API-Key'] = apiKey
}
const token = localStorage.getItem('token')
if (token) {
config.headers['Authorization'] = `Bearer ${token}`
}
return config
},
(error) => Promise.reject(error)
)
export interface KpiData {
label: string
value: number
status: string
hint: string
}
export interface ActivitySummary {
id: number
name: string
startTime?: string
endTime?: string
participants: number
shares: number
conversions: number
}
export interface Alert {
title: string
detail: string
type: string
level: string
}
export interface Todo {
id: string
title: string
description: string
type: string
link: string
priority: string
}
export interface DashboardData {
updatedAt: string
kpis: KpiData[]
activities: ActivitySummary[]
alerts: Alert[]
todos: Todo[]
}
interface ApiResponse<T> {
code: number
data: T
}
/**
* 获取仪表盘数据
*/
export async function getDashboard(): Promise<DashboardData> {
const response = await dashboardApi.get<ApiResponse<DashboardData>>('/dashboard')
return response.data.data
}
/**
* 获取KPI数据
*/
export async function getKpis(): Promise<KpiData[]> {
const response = await dashboardApi.get<ApiResponse<KpiData[]>>('/dashboard/kpis')
return response.data.data
}
/**
* 获取活动统计
*/
export async function getActivitySummary() {
const response = await dashboardApi.get('/dashboard/activities')
return response.data
}
/**
* 获取待办事项
*/
export async function getTodos(): Promise<Todo[]> {
const response = await dashboardApi.get<ApiResponse<Todo[]>>('/dashboard/todos')
return response.data.data
}
export default {
getDashboard,
getKpis,
getActivitySummary,
getTodos
}