diff --git a/jyotish-app/api-bridge.js b/jyotish-app/api-bridge.js index 06bf196d..75a9cc8b 100644 --- a/jyotish-app/api-bridge.js +++ b/jyotish-app/api-bridge.js @@ -1,61 +1,64 @@ /** - * API Bridge v1.0 - * 连接前端到 Python v6.6.0 精算引擎 + * API Bridge v2.0 + * 连接前端到 Python v6.7.0 精算引擎 * - * 用法: 在 index.html 中加载此脚本后,自动检测并优先使用 Python API + * 部署: API_KEY 在生产环境通过服务端环境变量注入 + * 此处为前端调用凭证,实际部署时建议配置环境变量 */ -const API_BASE = 'http://localhost:5200'; +const API_BASE = 'https://copse.top'; +const API_KEY = 'sk-828a787bd2bb69d2d4707e8c05ae5cfe81b13de7be1db7f85932d49ed72e4c6a'; + +const API_CACHE = {}; +const CACHE_TTL = 300000; // 5分钟缓存 async function apiFetch(endpoint, body = {}) { + const cacheKey = endpoint + JSON.stringify(body); + if (API_CACHE[cacheKey] && Date.now() - API_CACHE[cacheKey].ts < CACHE_TTL) { + return API_CACHE[cacheKey].data; + } try { const resp = await fetch(`${API_BASE}${endpoint}`, { method: body && Object.keys(body).length > 0 ? 'POST' : 'GET', - headers: { 'Content-Type': 'application/json' }, + headers: { + 'Content-Type': 'application/json', + 'Authorization': `Bearer ${API_KEY}`, + }, body: body && Object.keys(body).length > 0 ? JSON.stringify(body) : undefined, }); - return await resp.json(); + const data = await resp.json(); + API_CACHE[cacheKey] = { data, ts: Date.now() }; + return data; } catch (e) { + console.warn('[API] fetch failed:', e.message); return null; } } async function checkApiAvailable() { const r = await apiFetch('/api/health'); - return r && r.status === 'ok'; + if (r && r.status === 'ok') { + console.log(`[API] ✅ Connected to Jyotish API v${r.version} (${r.modules})`); + return true; + } + return false; } -/** - * 使用 Python API 计算完整星盘 - * 替代原有的 jyotish-engine.js 计算 - */ async function apiComputeFullChart(birthData) { const result = await apiFetch('/api/chart', birthData); if (!result || !result.success) { - throw new Error(result?.error || 'API computation failed'); + throw new Error(result?.error || '计算失败,请检查输入信息'); } return result; } -/** - * 获取补救建议 - */ async function apiGetRemedies(chartData) { - const shadbala = {}; - if (chartData.planets) { - for (const [p, d] of Object.entries(chartData.planets)) { - shadbala[p] = { total_rupas: 3.0 }; // 默认值 - } - } return await apiFetch('/api/remedies', { - shadbala, + shadbala: chartData.planets || {}, doshas: [], dasha_lord: chartData.dasha?.current_md || '', }); } -/** - * 获取KP分析 - */ async function apiGetKP(chartData) { return await apiFetch('/api/kp', { planets: chartData.planets || {}, @@ -63,32 +66,18 @@ async function apiGetKP(chartData) { }); } -/** - * 获取合盘分析 - */ async function apiGetSynastry(maleMoonDeg, femaleMoonDeg) { - return await apiFetch('/api/synastry', { - male_moon: maleMoonDeg, - female_moon: femaleMoonDeg, - }); + return await apiFetch('/api/synastry', { male_moon: maleMoonDeg, female_moon: femaleMoonDeg }); } -/** - * 获取Sade Sati分析 - */ async function apiGetSadeSati(chartData) { - const moon = chartData.planets?.Moon; - const sun = chartData.planets?.Sun; return await apiFetch('/api/sade_sati', { - moon_degree: moon?.lon || 0, + moon_degree: chartData.planets?.Moon?.lon || 0, asc_degree: chartData.ascendant?.degree || 0, saturn_degree: chartData.planets?.Saturn?.lon || 0, }); } -/** - * 获取Pancha Mahapurusha分析 - */ async function apiGetPMC(chartData) { return await apiFetch('/api/pancha_mahapurusha', { planets: chartData.planets || {}, @@ -96,9 +85,6 @@ async function apiGetPMC(chartData) { }); } -/** - * 获取事业分析 - */ async function apiGetCareer(chartData) { return await apiFetch('/api/career', { planets: chartData.planets || {}, @@ -106,9 +92,6 @@ async function apiGetCareer(chartData) { }); } -/** - * 获取感情分析 - */ async function apiGetRelationship(chartData) { return await apiFetch('/api/relationship', { planets: chartData.planets || {}, @@ -116,9 +99,6 @@ async function apiGetRelationship(chartData) { }); } -/** - * 获取Prashna卜卦分析 - */ async function apiGetPrashna(chartData, question) { return await apiFetch('/api/prashna', { planets: chartData.planets || {}, @@ -126,10 +106,29 @@ async function apiGetPrashna(chartData, question) { }); } -// 导出 +// 🔥 一键全功能计算 +async function apiComputeAll(birthData) { + const chart = await apiComputeFullChart(birthData); + if (!chart || !chart.success) return chart; + + // 并行请求所有分析 + const [remedies, kp, sade_sati, pmc, career, relationship] = await Promise.all([ + apiGetRemedies(chart), + apiGetKP(chart), + apiGetSadeSati(chart), + apiGetPMC(chart), + apiGetCareer(chart), + apiGetRelationship(chart), + ]); + + chart._extended = { remedies, kp, sade_sati, pmc, career, relationship }; + return chart; +} + window.JyotishAPI = { checkAvailable: checkApiAvailable, computeChart: apiComputeFullChart, + computeAll: apiComputeAll, getRemedies: apiGetRemedies, getKP: apiGetKP, getSynastry: apiGetSynastry, diff --git a/jyotish-app/index.html b/jyotish-app/index.html index 19f46e1f..b85ed96d 100644 --- a/jyotish-app/index.html +++ b/jyotish-app/index.html @@ -436,7 +436,6 @@ - diff --git a/jyotish-app/main.js b/jyotish-app/main.js index e9762585..dac38ebe 100644 --- a/jyotish-app/main.js +++ b/jyotish-app/main.js @@ -216,27 +216,15 @@ function setupForm() { const [hour, minute] = timeVal.split(':').map(Number); btnText.classList.add('hidden'); btnLoading.classList.remove('hidden'); btn.disabled = true; try { - // ✨ v6.6.0: 优先尝试 Python API 精算引擎 - let apiResult = null; + // 🔥 v6.7.0: 始终使用 Python API 精算引擎 if (window.JyotishAPI) { - const apiAvailable = await window.JyotishAPI.checkAvailable(); - if (apiAvailable) { - try { - apiResult = await window.JyotishAPI.computeChart({ year, month, day, hour, minute, lat, lon, tz }); - if (apiResult && apiResult.success) { - chartData = apiResult; - console.log('[Jyotish] ✅ Using Python API v6.6.0'); - } - } catch (apiErr) { - console.warn('[Jyotish] API unavailable, falling back to JS engine:', apiErr.message); - } + chartData = await window.JyotishAPI.computeAll({ year, month, day, hour, minute, lat, lon, tz }); + if (!chartData || !chartData.success) { + throw new Error(chartData?.error || 'API计算失败'); } - } - // 回退到 JS 引擎 - if (!chartData) { - await initEngine(); - chartData = await computeChart({ year, month, day, hour, minute, lat, lon, tz }); - console.log('[Jyotish] ⚠️ Fallback to JS engine'); + console.log('[Jyotish] ✅ Python API v6.7.0 —', chartData.dasha_count, '种Dasha,', chartData.yogas?.length || 0, '个Yoga'); + } else { + throw new Error('API桥接未加载,请刷新页面'); } // 保存出生数据供生时校正使用 window.__jyotishBirth = { year, month, day, hour, minute, lat, lon, tz };