v6.9.2: API智能回退 — localhost自动检测+JS引擎fallback

## 修复
- api-bridge.js: 自动检测运行环境 (localhost→5200, 云端→copse.top)
- main.js: API失败自动回退JS引擎, 不再直接报错
- 移除isResolved未闭合的try错误
This commit is contained in:
732642856
2026-06-11 22:47:57 +08:00
parent 8e0f64f423
commit 8337f237bd
2 changed files with 30 additions and 12 deletions
+11 -6
View File
@@ -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 = {};
+19 -6
View File
@@ -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();