From 8337f237bdbbe7d6989916bd0e55b357bcdfd28e Mon Sep 17 00:00:00 2001 From: 732642856 <732642856@qq.com> Date: Thu, 11 Jun 2026 22:47:57 +0800 Subject: [PATCH] =?UTF-8?q?v6.9.2:=20API=E6=99=BA=E8=83=BD=E5=9B=9E?= =?UTF-8?q?=E9=80=80=20=E2=80=94=20localhost=E8=87=AA=E5=8A=A8=E6=A3=80?= =?UTF-8?q?=E6=B5=8B+JS=E5=BC=95=E6=93=8Efallback?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## 修复 - api-bridge.js: 自动检测运行环境 (localhost→5200, 云端→copse.top) - main.js: API失败自动回退JS引擎, 不再直接报错 - 移除isResolved未闭合的try错误 --- jyotish-app/api-bridge.js | 17 +++++++++++------ jyotish-app/main.js | 25 +++++++++++++++++++------ 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/jyotish-app/api-bridge.js b/jyotish-app/api-bridge.js index 75a9cc8b..2dd470f9 100644 --- a/jyotish-app/api-bridge.js +++ b/jyotish-app/api-bridge.js @@ -1,11 +1,16 @@ /** - * API Bridge v2.0 - * 连接前端到 Python v6.7.0 精算引擎 - * - * 部署: API_KEY 在生产环境通过服务端环境变量注入 - * 此处为前端调用凭证,实际部署时建议配置环境变量 + * API Bridge v3.0 + * 自动检测环境:本地用 localhost:5200,云端用 copse.top + * 均不可达时回退到浏览器端 JS 引擎 */ -const API_BASE = 'https://copse.top'; +const API_BASE = (() => { + const host = window.location.hostname; + if (host === 'localhost' || host === '127.0.0.1' || host.startsWith('192.168.')) { + return 'http://localhost:5200'; + } + return 'https://copse.top'; +})(); + const API_KEY = 'sk-828a787bd2bb69d2d4707e8c05ae5cfe81b13de7be1db7f85932d49ed72e4c6a'; const API_CACHE = {}; diff --git a/jyotish-app/main.js b/jyotish-app/main.js index fd80064a..3acbabb7 100644 --- a/jyotish-app/main.js +++ b/jyotish-app/main.js @@ -216,16 +216,29 @@ function setupForm() { const [hour, minute] = timeVal.split(':').map(Number); btnText.classList.add('hidden'); btnLoading.classList.remove('hidden'); btn.disabled = true; try { - // 🔥 v6.7.0: 始终使用 Python API 精算引擎 + // v6.9.2: API优先, 不可用时回退JS引擎 if (window.JyotishAPI) { chartData = await window.JyotishAPI.computeAll({ year, month, day, hour, minute, lat, lon, tz }); - if (!chartData || !chartData.success) { - throw new Error(chartData?.error || 'API计算失败'); - } - console.log('[Jyotish] ✅ Python API v6.7.0 —', chartData.dasha_count, '种Dasha,', chartData.yogas?.length || 0, '个Yoga'); + if (!chartData || !chartData.success) throw new Error(chartData?.error || 'API计算失败'); + console.log('[Jyotish] ✅ API —', chartData.dasha_count, 'Dasha,', chartData.yogas?.length || 0, 'Yoga'); } else { - throw new Error('API桥接未加载,请刷新页面'); + throw new Error('API桥接未加载'); } + } catch (apiErr) { + console.warn('[Jyotish] API不可用:', apiErr.message, '→ 回退JS引擎'); + try { + await initEngine(); + chartData = await computeChart({ year, month, day, hour, minute, lat, lon, tz }); + chartData._fallback = true; + console.log('[Jyotish] ⚠️ JS引擎回退成功'); + } catch (jsErr) { + console.error('[Jyotish] JS引擎也失败:', jsErr); + alert('计算失败: ' + (apiErr.message || jsErr.message) + '\n请检查网络或启动API服务器'); + btnText.classList.remove('hidden'); btnLoading.classList.add('hidden'); btn.disabled = false; + return; + } + } + try { // 保存出生数据供生时校正使用 window.__jyotishBirth = { year, month, day, hour, minute, lat, lon, tz }; renderAll();