Просмотр исходного кода

fix: 修复接口请求异常的问题

zhupei@smartai.com 1 год назад
Родитель
Сommit
5fb8128c47
3 измененных файлов с 72 добавлено и 71 удалено
  1. 7 8
      public/config.js
  2. 3 63
      src/base/ajax.js
  3. 62 0
      src/base/ajax/replace-proxy-url.js

+ 7 - 8
public/config.js

@@ -7,21 +7,20 @@ window.DMX_VERSION = '1.0.1';
  * 配置项最后一个字符 无需是 /
  * 即,无需以 / 结束
  */
-// window.API_BASE_URL = 'http://192.168.20.119:9201/api';
 window.API_BASE_URL = 'http://192.168.20.119:9301';
-/**
- * 小数系统访问地址
- */
-window.XIAOSHU_URL = 'http://192.168.20.119:9301/';
-
 /**
  * rgflow 接口地址
  *
  * */
-window.RGFLOW_BASE_URL = 'http://192.168.20.119:28002/';
+window.RGFLOW_BASE_URL = 'http://192.168.20.119:28002';
 
 /**
  * dify 接口地址
  *
  * */
-window.DIFY_BASE_URL = 'http://192.168.20.119:28003/';
+window.DIFY_BASE_URL = 'http://192.168.20.119:28003';
+
+/**
+ * 小数系统访问地址
+ */
+window.XIAOSHU_URL = 'http://192.168.20.119:9301';

+ 3 - 63
src/base/ajax.js

@@ -2,69 +2,10 @@ import axios from 'axios';
 
 import { kuky_Authorization } from './storage';
 
-import { getConfigField } from '../config/index';
-
-/**
- * 处理请求路径
- * 当在开发模式下,使用 vite 中配置 proxy,代理所有的 /api/ 相关的请求
- * 开发模式下使用外网地址:http://smartai.com:8191
- * 使用外网地址的原因是:有其他成员在非内网环境下进行共同开发,故无法使用内网地址
- *
- * 当在production模式下,通过此处的方法,将所有 /api/ 请求地址,调整为正常的地址
- * @param {object} config
- * @returns
- */
-function applyRequestProxyUrl(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 进行配置');
-      }
-
-      // const replace_str = base_url + '/';
-
-      // const real_url = cur_url.replace('/api/', replace_str);
-
-      config.url = base_url + cur_url;
-    }
-    if (cur_url.startsWith('/rgflow/')) {
-      const base_url = getConfigField('RGFLOW_BASE_URL');
-      if (!base_url) {
-        throw new Error('关键配置项 RGFLOW_BASE_URL 不存在,请在 /config.js 进行配置');
-      }
-      const replace_str = base_url + '/';
-      const real_url = cur_url.replace('/rgflow/', replace_str);
-      config.url = real_url;
-    }
-    if (cur_url.startsWith('/dify/')) {
-      const base_url = getConfigField('DIFY_BASE_URL');
-      if (!base_url) {
-        throw new Error('关键配置项 MITY_BASE_URL 不存在,请在 /config.js 进行配置');
-      }
-      const replace_str = base_url + '/';
-      const real_url = cur_url.replace('/dify/', replace_str);
-      config.url = real_url;
-    }
-  } else {
-    // 开发环境,无需处理
-  }
-
-  return config;
-}
+import replaceProxyUrl from './ajax/replace-proxy-url';
 
 const ajax = axios.create({
-  // withCredentials: true, // 跨域发送 cookie
+  withCredentials: true, // 跨域发送 cookie
   timeout: 300000, // 请求超时
   headers: {
     'Content-Type': 'application/json'
@@ -73,8 +14,7 @@ const ajax = axios.create({
 
 ajax.interceptors.request.use(
   (config) => {
-    console.log('请求地址:', config.url);
-    applyRequestProxyUrl(config);
+    replaceProxyUrl(config);
 
     const token = kuky_Authorization.get();
     if (token) {

+ 62 - 0
src/base/ajax/replace-proxy-url.js

@@ -0,0 +1,62 @@
+import { getConfigField } from '../../config/index';
+
+/**
+ * 处理请求路径
+ * 当在开发模式下,使用 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;
+    }
+
+    if (cur_url.startsWith('/rgflow/')) {
+      const base_url = getConfigField('RGFLOW_BASE_URL');
+
+      if (!base_url) {
+        throw new Error('关键配置项 RGFLOW_BASE_URL 不存在,请在 /config.js 进行配置');
+      }
+
+      const replace_str = base_url + '/';
+      const real_url = cur_url.replace('/rgflow/', replace_str);
+      config.url = real_url;
+    }
+
+    if (cur_url.startsWith('/dify/')) {
+      const base_url = getConfigField('DIFY_BASE_URL');
+
+      if (!base_url) {
+        throw new Error('关键配置项 DIFY_BASE_URL 不存在,请在 /config.js 进行配置');
+      }
+
+      const replace_str = base_url + '/';
+      const real_url = cur_url.replace('/dify/', replace_str);
+      config.url = real_url;
+    }
+  } else {
+    // 开发环境,无需处理
+  }
+
+  return config;
+}