diff --git a/src/lib/core-stock-api.ts b/src/lib/core-stock-api.ts index 11b661e..94d5384 100644 --- a/src/lib/core-stock-api.ts +++ b/src/lib/core-stock-api.ts @@ -4,7 +4,7 @@ import { getApiBaseUrl } from "@/lib/api-client"; export interface ActiveCoreStock { stockCode: string; stockName: string; - dailyGains: Record; // 日期 -> 当日涨幅 + dailyGains: Record; // 日期 -> 当日涨幅(可空) appearCount: number; lastAppear: string | null; } @@ -33,15 +33,25 @@ export interface CoreStockHistoryResponse { /** 获取活跃核心股 + 最近10日涨幅矩阵 */ export async function fetchActiveCoreStocks(): Promise { const baseUrl = getApiBaseUrl(); - const resp = await fetch(`${baseUrl}/api/core-stocks/active`, { method: "GET", cache: "no-store" }); - if (!resp.ok) return { dates: [], stocks: [] }; - return resp.json(); + try { + const resp = await fetch(`${baseUrl}/api/core-stocks/active`, { 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 { const baseUrl = getApiBaseUrl(); - const resp = await fetch(`${baseUrl}/api/core-stocks/history?date=${date}`, { method: "GET", cache: "no-store" }); - if (!resp.ok) return null; - return resp.json(); + 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; + } }