فهرست منبع

feat: 调整接口调用地址的配置方式

zhupei 1 سال پیش
والد
کامیت
cedfcda00a
6فایلهای تغییر یافته به همراه64 افزوده شده و 5 حذف شده
  1. 2 1
      public/config.js
  2. 2 2
      src/ajax/api/message.js
  3. 52 0
      src/ajax/replace-proxy-url.js
  4. 6 0
      src/ajax/request.js
  5. 1 1
      src/components/Slider.vue
  6. 1 1
      vite.config.js

+ 2 - 1
public/config.js

@@ -3,7 +3,8 @@
  */
 window.__IFRAME_URL_CHUJUAN = 'http://192.168.20.119:8501'
 
-window.WEBSOCKET_BASE_URL = location.host;
+window.WEBSOCKET_BASE_URL = `ws://smartai.com:8401`;
+window.API_BASE_URL = 'http://smartai.com:8401';
 
 /**
  * 静态资源地址

+ 2 - 2
src/ajax/api/message.js

@@ -5,8 +5,8 @@ const create = (token, agentId, dialogId, type) => {
   const wsParam = type === "report" ? "report" : "chat";
   const wsUrl =
     type === "excel"
-      ? `ws://${socket_url}/api/document/ws/excel`
-      : `ws://${socket_url}/api/${wsParam}/ws/${agentId}/${dialogId}?token=${token}`;
+      ? `${socket_url}/api/document/ws/excel`
+      : `${socket_url}/api/${wsParam}/ws/${agentId}/${dialogId}?token=${token}`;
 
   const ws = new WebSocket(wsUrl);
   // 当 WebSocket 打开时...

+ 52 - 0
src/ajax/replace-proxy-url.js

@@ -0,0 +1,52 @@
+const getConfigField = (key) => {
+    if (!key) {
+        return null;
+    }
+
+    // if (!config) {
+    //   return null;
+    // }
+
+    if (key in window) {
+        return window[key];
+    } else {
+        return null;
+    }
+};
+
+/**
+ * 处理请求路径
+ * 当在开发模式下,使用 vite 中配置 proxy,代理所有的 /api/ 相关的请求
+ * 开发模式下使用外网地址:http://smartai.com:8191
+ * 使用外网地址的原因是:有其他成员在非内网环境下进行共同开发,故无法使用内网地址
+ *
+ * 当在production模式下,通过此处的方法,将所有 /api/ 请求地址,调整为正常的地址
+ * @param {object} config
+ * @returns
+ */
+export default function replaceProxyUrl(config) {
+  /**
+   * 如果是发布模式
+   * 则不能使用代理的方式,需要直接请求服务器路径
+   * API_BASE_URL 需要在 /public/config.js 中进行配置
+   */
+  if (import.meta.env.MODE === 'production') {
+    const cur_url = config.url;
+
+    const is_api_request = cur_url.indexOf('/api/') === 0;
+
+    if (is_api_request) {
+      const base_url = getConfigField('API_BASE_URL');
+
+      if (!base_url) {
+        throw new Error('关键配置项 API_BASE_URL 不存在,请在 /config.js 进行配置');
+      }
+
+      config.url = base_url + cur_url;
+    }
+  } else {
+    // 开发环境,无需处理
+  }
+
+  return config;
+}

+ 6 - 0
src/ajax/request.js

@@ -2,6 +2,9 @@ import axios from "axios";
 import router from "@/router";
 import ArcoVue from "@arco-design/web-vue";
 import helper from "@/utils/helper";
+
+import replaceProxyUrl from './replace-proxy-url';
+
 const service = axios.create({
   withCredentials: true, // 跨域发送 cookie
   timeout: 300000, // 请求超时
@@ -15,6 +18,9 @@ service.interceptors.request.use(
     if (helper.read("token")) {
       config.headers["Authorization"] = `Bearer ${helper.read("token")}`;
     }
+
+    replaceProxyUrl(config);
+
     return config;
   },
   (error) => {

+ 1 - 1
src/components/Slider.vue

@@ -246,7 +246,7 @@ const openHelp = ref(false);
 const { ajax, helper, config } = inject("$global");
 
 const getDisplayMenu = () => {
-    const agent_list = props.agent_list;
+    const agent_list = props.agent_list || [];
     const agent_config = config.slider;
 
     const arr = [];

+ 1 - 1
vite.config.js

@@ -20,7 +20,7 @@ export default defineConfig({
         host: "0.0.0.0", // IP配置,支持从IP启动
         proxy: {
             "/api": {
-                target: "http://smartai.com:8191",
+                target: "http://smartai.com:8401",
                 // target: "http://192.168.20.119:9301",
                 changeOrigin: true,
                 secure: false,