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 @@
-