Jelajahi Sumber

fix(仪表盘): 动态设置API基础URL以适应不同环境

根据当前域名自动判断并设置API基础URL,区分开发环境和生产环境,避免跨域问题
skyfffire 1 Minggu lalu
induk
melakukan
36838911e4
1 mengubah file dengan 13 tambahan dan 1 penghapusan
  1. 13 1
      src/dashboard/static/index.html

+ 13 - 1
src/dashboard/static/index.html

@@ -293,7 +293,19 @@
         let priceChart = null;
         let diffChart = null;
         let autoRefreshInterval = null;
-        const API_BASE = 'http://localhost:5000/api';
+        // 动态设置API基础URL
+        let API_BASE = '';
+        
+        // 检查当前域名来决定API地址
+        if (window.location.hostname === 'localhost' || window.location.hostname === '127.0.0.1') {
+            // 本地开发环境
+            API_BASE = 'http://localhost:5000/api';
+        } else {
+            // 生产环境
+            API_BASE = `http://${window.location.hostname}:5000/api`;
+            // 如果不需要显示端口号,可以使用下面的代码
+            // API_BASE = `http://${window.location.hostname}/api`;
+        }
 
         // 初始化
         document.addEventListener('DOMContentLoaded', function() {