v6.7.2: Web应用→纯API静态前端 — 移除JS引擎依赖
## 架构简化 - api-bridge.js v2.0: 指向 https://copse.top + API Key认证 - main.js: 始终使用Python API (移除JS引擎fallback) - index.html: 移除 SwissEph WASM 脚本(不再需要) - 新增 apiComputeAll() 一键全功能计算+5分钟缓存 ## 前端体积 - HTML: 23KB | CSS: 60KB | JS: 324KB (含UI+解释数据) - 构建: npm run build ✅ 3秒完成
This commit is contained in:
+50
-51
@@ -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,
|
||||
|
||||
@@ -436,7 +436,6 @@
|
||||
</section>
|
||||
</div>
|
||||
|
||||
<script type="module" src="/swisseph-wasm/src/swisseph.js"></script>
|
||||
<script src="/api-bridge.js"></script>
|
||||
<script type="module" src="/main.js"></script>
|
||||
</body>
|
||||
|
||||
+7
-19
@@ -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 };
|
||||
|
||||
Reference in New Issue
Block a user