feat: 业绩趋势增加近3年

This commit is contained in:
hzm
2026-02-21 11:22:24 +08:00
parent 4e6d681986
commit f691435f22
3 changed files with 54 additions and 18 deletions

View File

@@ -1,6 +1,7 @@
import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';
import timezone from 'dayjs/plugin/timezone';
import { cachedRequest, clearCachedRequest } from '../lib/cacheRequest';
dayjs.extend(utc);
dayjs.extend(timezone);
@@ -19,23 +20,58 @@ const nowInTz = () => dayjs().tz(TZ);
const toTz = (input) => (input ? dayjs.tz(input, TZ) : nowInTz());
export const loadScript = (url) => {
return new Promise((resolve, reject) => {
if (typeof document === 'undefined' || !document.body) return resolve();
const script = document.createElement('script');
script.src = url;
script.async = true;
const cleanup = () => {
if (document.body.contains(script)) document.body.removeChild(script);
};
script.onload = () => {
cleanup();
resolve();
};
script.onerror = () => {
cleanup();
reject(new Error('数据加载失败'));
};
document.body.appendChild(script);
if (typeof document === 'undefined' || !document.body) return Promise.resolve();
let cacheKey = url;
try {
const parsed = new URL(url);
parsed.searchParams.delete('_');
parsed.searchParams.delete('_t');
cacheKey = parsed.toString();
} catch (e) {
}
const cacheTime = 10 * 60 * 1000;
return cachedRequest(
() =>
new Promise((resolve) => {
const script = document.createElement('script');
script.src = url;
script.async = true;
const cleanup = () => {
if (document.body.contains(script)) document.body.removeChild(script);
};
script.onload = () => {
cleanup();
let apidata;
try {
apidata = window?.apidata ? JSON.parse(JSON.stringify(window.apidata)) : undefined;
} catch (e) {
apidata = window?.apidata;
}
resolve({ ok: true, apidata });
};
script.onerror = () => {
cleanup();
resolve({ ok: false, error: '数据加载失败' });
};
document.body.appendChild(script);
}),
cacheKey,
{ cacheTime }
).then((result) => {
if (!result?.ok) {
clearCachedRequest(cacheKey);
throw new Error(result?.error || '数据加载失败');
}
if (typeof window !== 'undefined' && result.apidata !== undefined) {
window.apidata = result.apidata;
}
});
};

View File

@@ -73,6 +73,7 @@ export default function FundTrendChart({ code, isExpanded, onToggleExpand }) {
{ label: '近3月', value: '3m' },
{ label: '近6月', value: '6m' },
{ label: '近1年', value: '1y' },
{ label: '近3年', value: '3y'}
];
const change = useMemo(() => {