diff --git a/frontend/src/services/budgets.ts b/frontend/src/services/budgets.ts new file mode 100644 index 0000000..529c514 --- /dev/null +++ b/frontend/src/services/budgets.ts @@ -0,0 +1,49 @@ +/** + * 预算服务 - Budgets API Service + * 功能:封装预算相关的 API 调用,提供预算 CRUD 操作 + * API 依赖: + * - GET /api/budgets - 获取预算列表(可按月份筛选) + * - GET /api/budgets/:id - 获取单条预算 + * - POST /api/budgets - 创建预算 + * - PUT /api/budgets/:id - 更新预算 + * - DELETE /api/budgets/:id - 删除预算 + */ +import { apiClient } from './apiClient'; +import type { Budget, BudgetFormData } from '../types'; +import { useUserStore } from '../stores/userStore'; + +export const budgetsApi = { + // API: GET /api/budgets - 获取预算列表,支持按月份筛选 + async getBudgets(month?: string): Promise { + const userId = useUserStore.getState().userId; + const params: Record = { userId }; + if (month) params.month = month; + const response = await apiClient.get('/budgets', params); + return response.data; + }, + + // API: GET /api/budgets/:id - 获取指定预算详情 + async getBudget(id: number): Promise { + const response = await apiClient.get(`/budgets/${id}`); + return response.data; + }, + + // API: POST /api/budgets - 创建新预算,自动关联当前用户 + async createBudget(data: Omit): Promise { + const userId = useUserStore.getState().userId; + const response = await apiClient.post('/budgets', { ...data, userId }); + return response.data; + }, + + // API: PUT /api/budgets/:id - 更新预算,支持部分更新 + async updateBudget(id: number, data: Partial): Promise { + const response = await apiClient.put(`/budgets/${id}`, data); + return response.data; + }, + + // API: DELETE /api/budgets/:id - 删除指定预算 + async deleteBudget(id: number): Promise { + const response = await apiClient.delete(`/budgets/${id}`); + return response.data; + }, +};