Files
vf_react/src/services/conceptStaticService.js
2025-12-14 17:15:29 +08:00

117 lines
2.7 KiB
JavaScript
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.

/**
* 概念涨跌幅静态数据服务
* 从 /data/concept/ 目录读取预生成的 JSON 文件
* 不依赖后端 API适合静态部署
*/
// 数据基础路径
const DATA_BASE_URL = '/data/concept';
// 内存缓存
const cache = {
latest: null,
dates: null,
daily: new Map(),
};
/**
* 获取最新的热门概念数据
* 这是 HeroPanel 滚动窗口的主要数据源
*/
export const fetchPopularConcepts = async () => {
try {
// 使用缓存
if (cache.latest) {
return { success: true, data: cache.latest, from_cache: true };
}
const response = await fetch(`${DATA_BASE_URL}/latest.json`);
if (!response.ok) {
throw new Error(`HTTP ${response.status}`);
}
const data = await response.json();
// 缓存结果
cache.latest = data;
return { success: true, data, from_cache: false };
} catch (error) {
console.error('[conceptStaticService] fetchPopularConcepts error:', error);
return { success: false, error: error.message, data: null };
}
};
/**
* 获取可用日期列表
*/
export const fetchAvailableDates = async () => {
try {
// 使用缓存
if (cache.dates) {
return { success: true, dates: cache.dates };
}
const response = await fetch(`${DATA_BASE_URL}/dates.json`);
if (!response.ok) {
throw new Error(`HTTP ${response.status}`);
}
const data = await response.json();
// 缓存结果
cache.dates = data.dates || [];
return { success: true, dates: cache.dates, total: data.total };
} catch (error) {
console.error('[conceptStaticService] fetchAvailableDates error:', error);
return { success: false, error: error.message, dates: [] };
}
};
/**
* 获取指定日期的概念数据
*/
export const fetchDailyConcepts = async (date) => {
try {
// 使用缓存
if (cache.daily.has(date)) {
return { success: true, data: cache.daily.get(date), from_cache: true };
}
const response = await fetch(`${DATA_BASE_URL}/daily/${date}.json`);
if (!response.ok) {
if (response.status === 404) {
return { success: false, error: `日期 ${date} 的数据不存在` };
}
throw new Error(`HTTP ${response.status}`);
}
const data = await response.json();
// 缓存结果
cache.daily.set(date, data);
return { success: true, data, from_cache: false };
} catch (error) {
console.error('[conceptStaticService] fetchDailyConcepts error:', error);
return { success: false, error: error.message };
}
};
/**
* 清除缓存
*/
export const clearCache = () => {
cache.latest = null;
cache.dates = null;
cache.daily.clear();
};
export default {
fetchPopularConcepts,
fetchAvailableDates,
fetchDailyConcepts,
clearCache,
};