Quellcode durchsuchen

fix: 修复生产环境配置和API地址逻辑

修复CORS配置中生产环境前端地址协议错误
优化API地址逻辑,非localhost环境下使用同源地址
移除autoAccessTasks接口中不必要的status字段
max_liu vor 13 Stunden
Ursprung
Commit
173810f7ce
3 geänderte Dateien mit 8 neuen und 9 gelöschten Zeilen
  1. 2 4
      server/routes/autoAccessTasks.js
  2. 1 1
      server/server.js
  3. 5 4
      src/services/api.js

+ 2 - 4
server/routes/autoAccessTasks.js

@@ -144,7 +144,6 @@ router.post("/", async (req, res) => {
       api_provider,
       api_doc,
       operate_path_data,
-      status,
     } = req.body;
 
     // 验证必填字段
@@ -168,9 +167,9 @@ router.post("/", async (req, res) => {
     const sql = `
       INSERT INTO tools_auto_access_task (
         access_task_id, tools_name, tools_function_name, tools_function_desc,
-        access_type, api_provider, api_doc, operate_path_data, status,
+        access_type, api_provider, api_doc, operate_path_data,
         create_time, update_time
-      ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), NOW())
+      ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, NOW(), NOW())
     `;
 
     // 保证 operate_path_data 为字符串(可能传入对象)
@@ -186,7 +185,6 @@ router.post("/", async (req, res) => {
       api_provider || null,
       api_doc || null,
       operatePathValue || null,
-      status || null,
     ]);
 
     res.status(201).json({

+ 1 - 1
server/server.js

@@ -14,7 +14,7 @@ const PORT = process.env.PORT || 3001;
 app.use(
   cors({
     origin: [
-      "https://tools.aiddit.com", // 生产环境前端地址
+      "http://tools.aiddit.com", // 生产环境前端地址
       // 'http://47.93.61.163:3030',  // 生产环境前端地址
       "http://localhost:3000", // 本地开发环境
       "http://localhost:3030", // 本地测试环境

+ 5 - 4
src/services/api.js

@@ -1,10 +1,11 @@
 import axios from "axios";
 
 // 根据环境自动切换API地址
-const API_BASE_URL =
-  process.env.NODE_ENV === "production"
-    ? "https://tools.aiddit.com/api" // 生产环境直接访问后端服务
-    : "http://localhost:3001/api"; // 开发环境使用本地地址
+// 非 localhost 环境下,优先使用同源地址,避免跨域
+const isLocalhost = typeof window !== "undefined" && /localhost/.test(window.location.host);
+const API_BASE_URL = isLocalhost
+  ? "http://localhost:3001/api"
+  : (typeof window !== "undefined" ? `${window.location.origin}/api` : "http://tools.aiddit.com/api");
 
 const api = axios.create({
   baseURL: API_BASE_URL,