Files
nofx/web/src/lib/api.ts
T
sue 6ea1ead263 fix: 修复AI学习数据加载失败问题
问题描述:
- AILearning组件直接使用硬编码的localhost:8080地址
- 绕过了Vite代理配置,导致加载失败
- 在生产环境无法正常工作
修复内容:
1. api.ts: 添加统一的getPerformance()方法
2. AILearning.tsx: 移除硬编码URL,使用统一API
3. 删除多余的fetcher函数
技术改进:
- 使用Vite代理配置,避免CORS问题
- 统一API管理,提高可维护性
- 支持开发和生产环境
影响范围:
- web/src/lib/api.ts: +11行 (新增getPerformance方法)
- web/src/components/AILearning.tsx: -4行, +2行 (重构API调用)
Co-Authored-By: tinkle-community <tinklefund@gmail.com>
2025-10-30 01:36:51 +08:00

114 lines
3.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import type {
SystemStatus,
AccountInfo,
Position,
DecisionRecord,
Statistics,
TraderInfo,
CompetitionData,
} from '../types';
const API_BASE = '/api';
export const api = {
// 竞赛相关接口
async getCompetition(): Promise<CompetitionData> {
const res = await fetch(`${API_BASE}/competition`);
if (!res.ok) throw new Error('获取竞赛数据失败');
return res.json();
},
async getTraders(): Promise<TraderInfo[]> {
const res = await fetch(`${API_BASE}/traders`);
if (!res.ok) throw new Error('获取trader列表失败');
return res.json();
},
// 获取系统状态(支持trader_id
async getStatus(traderId?: string): Promise<SystemStatus> {
const url = traderId
? `${API_BASE}/status?trader_id=${traderId}`
: `${API_BASE}/status`;
const res = await fetch(url);
if (!res.ok) throw new Error('获取系统状态失败');
return res.json();
},
// 获取账户信息(支持trader_id
async getAccount(traderId?: string): Promise<AccountInfo> {
const url = traderId
? `${API_BASE}/account?trader_id=${traderId}`
: `${API_BASE}/account`;
const res = await fetch(url, {
cache: 'no-store',
headers: {
'Cache-Control': 'no-cache',
},
});
if (!res.ok) throw new Error('获取账户信息失败');
const data = await res.json();
console.log('Account data fetched:', data);
return data;
},
// 获取持仓列表(支持trader_id
async getPositions(traderId?: string): Promise<Position[]> {
const url = traderId
? `${API_BASE}/positions?trader_id=${traderId}`
: `${API_BASE}/positions`;
const res = await fetch(url);
if (!res.ok) throw new Error('获取持仓列表失败');
return res.json();
},
// 获取决策日志(支持trader_id
async getDecisions(traderId?: string): Promise<DecisionRecord[]> {
const url = traderId
? `${API_BASE}/decisions?trader_id=${traderId}`
: `${API_BASE}/decisions`;
const res = await fetch(url);
if (!res.ok) throw new Error('获取决策日志失败');
return res.json();
},
// 获取最新决策(支持trader_id
async getLatestDecisions(traderId?: string): Promise<DecisionRecord[]> {
const url = traderId
? `${API_BASE}/decisions/latest?trader_id=${traderId}`
: `${API_BASE}/decisions/latest`;
const res = await fetch(url);
if (!res.ok) throw new Error('获取最新决策失败');
return res.json();
},
// 获取统计信息(支持trader_id
async getStatistics(traderId?: string): Promise<Statistics> {
const url = traderId
? `${API_BASE}/statistics?trader_id=${traderId}`
: `${API_BASE}/statistics`;
const res = await fetch(url);
if (!res.ok) throw new Error('获取统计信息失败');
return res.json();
},
// 获取收益率历史数据(支持trader_id)
async getEquityHistory(traderId?: string): Promise<any[]> {
const url = traderId
? `${API_BASE}/equity-history?trader_id=${traderId}`
: `${API_BASE}/equity-history`;
const res = await fetch(url);
if (!res.ok) throw new Error('获取历史数据失败');
return res.json();
},
// 获取AI学习表现分析(支持trader_id
async getPerformance(traderId?: string): Promise<any> {
const url = traderId
? `${API_BASE}/performance?trader_id=${traderId}`
: `${API_BASE}/performance`;
const res = await fetch(url);
if (!res.ok) throw new Error('获取AI学习数据失败');
return res.json();
},
};