Commit 8b0e6616 by 江名洋

fix: 动态推导API路径并修正相对路径

1 parent 5005b25a
......@@ -4,6 +4,15 @@
注意: 不要删除这个注释。
*/
// ========== 自动计算 API 基础路径 ==========
// 从浏览器地址栏 pathname 动态推导,兼容本地 /ods 和服务器 /daw/ods 两种场景
(function() {
var p = window.location.pathname;
if (p.endsWith('/')) p = p.slice(0, -1);
var idx = p.lastIndexOf('/');
window.API_BASE = p.substring(0, idx) + '/api';
})();
// ========== 工具函数 ==========
function fmtDate(d) {
return `${d.getFullYear()}-${String(d.getMonth()+1).padStart(2,'0')}-${String(d.getDate()).padStart(2,'0')}`;
......@@ -22,8 +31,7 @@ let historyCache = {};
// ========== 历史记录渲染 ==========
async function fetchHistory(filterFn) {
try {
const apiBase = (typeof APP_ROOT !== 'undefined' ? APP_ROOT : '.') + '/api';
const res = await fetch(apiBase + '/etl/history?limit=15');
const res = await fetch(window.API_BASE + '/etl/history?limit=15');
const history = await res.json();
const tbody = document.getElementById('history-tbody');
if (!tbody) return false;
......
......@@ -197,7 +197,7 @@
if (natStart) genForm.append('nature_start_date', natStart);
if (natEnd) genForm.append('nature_end_date', natEnd);
const genRes = await fetch('../api/analysis/generate', { method: 'POST', body: genForm });
const genRes = await fetch(window.API_BASE + '/analysis/generate', { method: 'POST', body: genForm });
const genData = await genRes.json();
if (genData.status === 'success') {
......
......@@ -98,8 +98,8 @@
</section>
</main>
<script>var APP_ROOT = '..';</script>
<script>
(function(){var p=window.location.pathname;if(p.endsWith('/'))p=p.slice(0,-1);var i=p.lastIndexOf('/');window.API_BASE=p.substring(0,i)+'/api';})();
document.addEventListener('DOMContentLoaded', () => {
const fmt = d => `${d.getFullYear()}-${String(d.getMonth()+1).padStart(2,'0')}-${String(d.getDate()).padStart(2,'0')}`;
const now = new Date();
......@@ -176,7 +176,7 @@
if (hasFactory) uploadForm.append('factory_file', factoryFileInput.files[0]);
if (hasValue) uploadForm.append('value_file', valueFileInput.files[0]);
const uploadRes = await fetch('../api/harmony/upload', { method: 'POST', body: uploadForm });
const uploadRes = await fetch(window.API_BASE + '/harmony/upload', { method: 'POST', body: uploadForm });
const uploadData = await uploadRes.json();
if (uploadData.status !== 'success') {
throw new Error(uploadData.detail || '文件上传失败');
......@@ -191,7 +191,7 @@
if (sd) genForm.append('start_date', sd);
if (ed) genForm.append('end_date', ed);
const genRes = await fetch('../api/harmony/generate', { method: 'POST', body: genForm });
const genRes = await fetch(window.API_BASE + '/harmony/generate', { method: 'POST', body: genForm });
const genData = await genRes.json();
if (genData.status === 'success') {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!