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();