Files
auv/src/lib/core-stock-api.ts
T

61 lines
1.9 KiB
TypeScript

// 核心股历史数据获取工具
import { getApiBaseUrl } from "@/lib/api-client";
export interface ActiveCoreStock {
stockCode: string;
stockName: string;
coverCount: number | null; // 覆盖题材数
dailyGains: Record<string, number | null>; // 日期 -> 当日涨幅(可空)
appearCount: number;
lastAppear: string | null;
daysSinceLastAppear: number; // 最近上榜距窗口最新交易日的自然日差
themes: { theme_code: string; theme_name: string }[]; // 所属题材列表
}
export interface ActiveCoreStocksResponse {
dates: string[];
stocks: ActiveCoreStock[];
}
export interface CoreStockHistoryItem {
id: number;
trade_date: string;
stock_code: string;
stock_name: string;
f3: number | null;
cover_count: number | null;
rank: number;
themes: { theme_code: string; theme_name: string }[];
}
export interface CoreStockHistoryResponse {
date: string;
items: CoreStockHistoryItem[];
}
/** 获取活跃核心股 + 最近N日涨幅矩阵 */
export async function fetchActiveCoreStocks(days: number = 10): Promise<ActiveCoreStocksResponse> {
const baseUrl = getApiBaseUrl();
try {
const resp = await fetch(`${baseUrl}/api/core-stocks/active?days=${days}`, { method: "GET", cache: "no-store" });
if (!resp.ok) return { dates: [], stocks: [] };
return resp.json();
} catch (err) {
console.error("[core-stock-api] 获取活跃核心股失败:", err);
return { dates: [], stocks: [] };
}
}
/** 获取指定交易日核心股(含所属题材) */
export async function fetchCoreStockHistory(date: string): Promise<CoreStockHistoryResponse | null> {
const baseUrl = getApiBaseUrl();
try {
const resp = await fetch(`${baseUrl}/api/core-stocks/history?date=${date}`, { method: "GET", cache: "no-store" });
if (!resp.ok) return null;
return resp.json();
} catch (err) {
console.error("[core-stock-api] 获取核心股历史失败:", err);
return null;
}
}