Files
accounts-manager-web/lib/api.ts
cloud370 7aaeffa498 feat: 实现账户批量导出功能和界面优化
- 新增批量导出功能,支持text模式导出账户数据
- 添加导出弹窗,支持文本全选和文件下载
- 移动刷新按钮到全局位置,统一刷新账户和统计数据
- 在统计卡片中将已导出状态计入可用账户
- 创建自定义确认对话框替换系统confirm弹窗
- 统一按钮尺寸,修复刷新和上传按钮大小不一致
- 添加已导出状态的中文映射和样式

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-24 05:28:45 +08:00

98 lines
2.8 KiB
TypeScript

import {
ApiResponse,
ListAccountsBody,
ListAccountsResponse,
BatchDeleteBody,
BatchUpdateBody,
BatchExportBody,
BatchExportResponse,
StatsOverview,
ScriptUploadItem
} from './types';
const getApiBaseUrl = () => {
// 检查是否有配置的环境变量(构建时注入)
const envUrl = process.env.NEXT_PUBLIC_API_BASE_URL;
if (envUrl && envUrl !== 'undefined') {
return envUrl;
}
// 在浏览器环境中,没有配置则使用当前域名
// if (typeof window !== 'undefined') {
// return `${window.location.protocol}//${window.location.hostname}:3006`;
// }
// 服务端渲染时的默认值
return 'http://170.205.39.58:13007';
};
class ApiClient {
private async request<T>(
endpoint: string,
options: RequestInit = {}
): Promise<ApiResponse<T>> {
const API_BASE_URL = getApiBaseUrl(); // 每次请求时动态获取
const url = `${API_BASE_URL}${endpoint}`;
const response = await fetch(url, {
headers: {
'Content-Type': 'application/json',
...options.headers,
},
...options,
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json();
}
// 获取账户列表
async getAccountsList(body: ListAccountsBody): Promise<ApiResponse<ListAccountsResponse>> {
return this.request<ListAccountsResponse>('/web/v1/accounts/list', {
method: 'POST',
body: JSON.stringify(body),
});
}
// 批量删除账户
async batchDeleteAccounts(body: BatchDeleteBody): Promise<ApiResponse<{ deletedCount: number }>> {
return this.request<{ deletedCount: number }>('/web/v1/accounts/delete-batch', {
method: 'POST',
body: JSON.stringify(body),
});
}
// 批量更新账户
async batchUpdateAccounts(body: BatchUpdateBody): Promise<ApiResponse<{ updatedCount: number }>> {
return this.request<{ updatedCount: number }>('/web/v1/accounts/update-batch', {
method: 'POST',
body: JSON.stringify(body),
});
}
// 批量导出账户
async batchExportAccounts(body: BatchExportBody): Promise<ApiResponse<BatchExportResponse>> {
return this.request<BatchExportResponse>('/web/v1/accounts/export', {
method: 'POST',
body: JSON.stringify(body),
});
}
// 获取统计概览
async getStatsOverview(): Promise<ApiResponse<StatsOverview>> {
return this.request<StatsOverview>('/web/v1/stats/overview');
}
// 上传账户
async uploadAccounts(accounts: ScriptUploadItem[], ownerId: string): Promise<ApiResponse<{ processedCount: number; createdCount: number; updatedCount: number }>> {
return this.request<{ processedCount: number; createdCount: number; updatedCount: number }>(`/s/v1/${ownerId}/upload`, {
method: 'POST',
body: JSON.stringify(accounts),
});
}
}
export const apiClient = new ApiClient();