Files
wenzi/frontend/admin/src/services/dashboard.ts

112 lines
2.1 KiB
TypeScript
Raw Normal View History

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
}