update pay ui
This commit is contained in:
116
src/services/conceptStaticService.js
Normal file
116
src/services/conceptStaticService.js
Normal file
@@ -0,0 +1,116 @@
|
||||
/**
|
||||
* 概念涨跌幅静态数据服务
|
||||
* 从 /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,
|
||||
};
|
||||
@@ -30,6 +30,7 @@ import ReactECharts from 'echarts-for-react';
|
||||
import { logger } from '@utils/logger';
|
||||
import { getApiBase } from '@utils/apiConfig';
|
||||
import { useIndexQuote } from '@hooks/useIndexQuote';
|
||||
import conceptStaticService from '@services/conceptStaticService';
|
||||
|
||||
// 定义动画
|
||||
const animations = `
|
||||
@@ -74,18 +75,13 @@ const fetchIndexKline = async (indexCode) => {
|
||||
};
|
||||
|
||||
/**
|
||||
* 获取热门概念数据
|
||||
* 获取热门概念数据(使用静态文件)
|
||||
*/
|
||||
const fetchPopularConcepts = async () => {
|
||||
try {
|
||||
const response = await fetch(`${getApiBase()}/concept-api/search`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ query: '', size: 60, page: 1, sort_by: 'change_pct' })
|
||||
});
|
||||
const data = await response.json();
|
||||
if (data.results?.length > 0) {
|
||||
return data.results.map(item => ({
|
||||
const result = await conceptStaticService.fetchPopularConcepts();
|
||||
if (result.success && result.data?.results?.length > 0) {
|
||||
return result.data.results.map(item => ({
|
||||
name: item.concept,
|
||||
change_pct: item.price_info?.avg_change_pct || 0,
|
||||
}));
|
||||
|
||||
Reference in New Issue
Block a user