From f691435f22203613e87bfb1b84ccf60d0fb4d139 Mon Sep 17 00:00:00 2001 From: hzm <934585316@qq.com> Date: Sat, 21 Feb 2026 11:22:24 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=B8=9A=E7=BB=A9=E8=B6=8B=E5=8A=BF?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=BF=913=E5=B9=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/fund.js | 70 +++++++++++++++++++++++-------- app/components/FundTrendChart.jsx | 1 + wrangler.jsonc | 1 - 3 files changed, 54 insertions(+), 18 deletions(-) diff --git a/app/api/fund.js b/app/api/fund.js index a0014d8..be41861 100644 --- a/app/api/fund.js +++ b/app/api/fund.js @@ -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; + } }); }; diff --git a/app/components/FundTrendChart.jsx b/app/components/FundTrendChart.jsx index 6e9e86a..bcaf8cf 100644 --- a/app/components/FundTrendChart.jsx +++ b/app/components/FundTrendChart.jsx @@ -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(() => { diff --git a/wrangler.jsonc b/wrangler.jsonc index b63ef2a..727f4f4 100644 --- a/wrangler.jsonc +++ b/wrangler.jsonc @@ -1,6 +1,5 @@ { "name": "real-time-fund", - "compatibility_date": "2026-02-20", "assets": { "directory": "./out" }