chore: add frontend/src/services/accounts.ts
This commit is contained in:
@@ -0,0 +1,47 @@
|
|||||||
|
/**
|
||||||
|
* 账户服务 - Accounts API Service
|
||||||
|
* 功能:封装账户相关的 API 调用,提供账户 CRUD 操作
|
||||||
|
* API 依赖:
|
||||||
|
* - GET /api/accounts - 获取账户列表
|
||||||
|
* - GET /api/accounts/:id - 获取单个账户
|
||||||
|
* - POST /api/accounts - 创建账户
|
||||||
|
* - PUT /api/accounts/:id - 更新账户
|
||||||
|
* - DELETE /api/accounts/:id - 删除账户
|
||||||
|
*/
|
||||||
|
import { apiClient } from './apiClient';
|
||||||
|
import type { Account } from '../types';
|
||||||
|
import { useUserStore } from '../stores/userStore';
|
||||||
|
|
||||||
|
export const accountsApi = {
|
||||||
|
// API: GET /api/accounts - 获取当前用户的所有账户
|
||||||
|
async getAccounts(): Promise<Account[]> {
|
||||||
|
const userId = useUserStore.getState().userId;
|
||||||
|
const response = await apiClient.get<Account[]>('/accounts', { userId });
|
||||||
|
return response.data;
|
||||||
|
},
|
||||||
|
|
||||||
|
// API: GET /api/accounts/:id - 获取指定账户详情
|
||||||
|
async getAccount(id: number): Promise<Account> {
|
||||||
|
const response = await apiClient.get<Account>(`/accounts/${id}`);
|
||||||
|
return response.data;
|
||||||
|
},
|
||||||
|
|
||||||
|
// API: POST /api/accounts - 创建新账户,自动关联当前用户
|
||||||
|
async createAccount(data: Omit<Account, 'id' | 'createdAt' | 'updatedAt'>): Promise<Account> {
|
||||||
|
const userId = useUserStore.getState().userId;
|
||||||
|
const response = await apiClient.post<Account>('/accounts', { ...data, userId });
|
||||||
|
return response.data;
|
||||||
|
},
|
||||||
|
|
||||||
|
// API: PUT /api/accounts/:id - 更新账户信息,禁止修改 userId 和时间字段
|
||||||
|
async updateAccount(id: number, data: Partial<Omit<Account, 'id' | 'userId' | 'createdAt' | 'updatedAt'>>): Promise<Account> {
|
||||||
|
const response = await apiClient.put<Account>(`/accounts/${id}`, data);
|
||||||
|
return response.data;
|
||||||
|
},
|
||||||
|
|
||||||
|
// API: DELETE /api/accounts/:id - 删除指定账户
|
||||||
|
async deleteAccount(id: number): Promise<void> {
|
||||||
|
const response = await apiClient.delete<void>(`/accounts/${id}`);
|
||||||
|
return response.data;
|
||||||
|
},
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user