Files
gecko/srde/miniprogram/services/api.ts
Daniel a94bd44c3a Initial commit
Made-with: Cursor
2026-02-28 18:43:09 +08:00

65 lines
2.0 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.
// API 层 - 对接后端 SRDE支持模拟数据
const USE_MOCK = true;
const BASE_URL = 'https://your-api.com'; // 对接时改为真实地址
function getToken(): string {
return wx.getStorageSync('srde_token') || '';
}
export function request<T>(opt: { url: string; method?: string; data?: object }): Promise<T> {
if (USE_MOCK) return Promise.reject(new Error('MOCK模式'));
const token = getToken();
return new Promise((resolve, reject) => {
wx.request({
url: BASE_URL + (opt.url.startsWith('/') ? opt.url : '/' + opt.url),
method: (opt.method as any) || 'GET',
data: opt.data,
header: {
'Content-Type': 'application/json',
...(token ? { Authorization: `Bearer ${token}` } : {}),
},
success: (res) => {
if ((res.statusCode || 0) >= 200 && (res.statusCode || 0) < 300) {
resolve(res.data as T);
} else reject(new Error((res.data as any)?.detail || '请求失败'));
},
fail: reject,
});
});
}
export function setMockMode(use: boolean) {
(global as any).__SRDE_USE_MOCK__ = use;
}
export function isMockMode(): boolean {
return USE_MOCK || !!(global as any).__SRDE_USE_MOCK__;
}
export const mockAccount = {
total_capital: 100000,
current_capital: 98500,
current_drawdown: 1.5,
max_drawdown: 5.2,
consecutive_losses: 1,
trading_locked_until: null as string | null,
status: 'tradable' as 'tradable' | 'compressed' | 'locked',
daily_risk_limit: 2000,
single_risk_limit: 1000,
};
export const mockTrades = [
{ id: '1', symbol: 'BTCUSDT', direction: 'long', entry_price: 42000, status: 'open', position_size: 0.5 },
{ id: '2', symbol: 'ETHUSDT', direction: 'short', entry_price: 2200, status: 'closed', pnl: -120 },
{ id: '3', symbol: 'SOLUSDT', direction: 'long', entry_price: 98, status: 'closed', pnl: 85 },
];
export const mockStats = {
win_rate: 62,
avg_odds: 1.8,
profit_factor: 2.1,
max_drawdown: 5.2,
risk_score: 72,
equity_curve: [100000, 101200, 99800, 100500, 102100, 100800, 98500],
};