Browse Source

no message

xiaohaizhao 3 tháng trước cách đây
mục cha
commit
b290cd0c8a
1 tập tin đã thay đổi với 67 bổ sung79 xóa
  1. 67 79
      utils/Http.js

+ 67 - 79
utils/Http.js

@@ -2,44 +2,17 @@ import axios from "axios";
 
 class HTTP {
   constructor() {
-    this.urls = [
-      { name: "测试", url: "http://61.164.207.46:8300" },
-      { name: "正式", url: "https://crm.meida.com:16691" },
-      { name: "楚楚", url: "https://cucu.cnyunl.com:8079" }
-    ];
-
-    if (process.env.NODE_ENV === "development") {
-      this.baseUrl = this.urls[0].url;
-    } else {
-      this.baseUrl = this.urls[1].url;
-    }
-
     this.jsessionid = uni.getStorageSync("JSESSIONID") || "";
 
-    // 工具方法 - 更新分页列表
-    this.updateList = (content, getList) => {
-      content.copyContent = JSON.parse(JSON.stringify(content));
-      content.pageSize = (content.pageNumber - 1) * (content.pageSize || 20);
-      content.pageNumber = 1;
-      getList();
-    };
-
-    // 工具方法 - 获取缩略图 / 压缩图
-    this.getSpecifiedImage = (obj, getType = false) => {
-      let type = getType ? "compressed" : "thumbnail";
-      let imgObj = obj.subfiles.find(v => v.type == type);
-      return imgObj?.url;
-    };
-
     // 创建 axios 实例
     this.instance = axios.create({
       baseURL: "/yos/rest",
       withCredentials: true
     });
 
-    // 请求拦截器 - 自动加 JSESSIONID
+    // 请求拦截器
     this.instance.interceptors.request.use((config) => {
-      if (!config.headers) config.headers = {};
+      config.headers = config.headers || {};
       config.headers["content-type"] = "application/json";
       config.headers["accesstoken"] = uni.getStorageSync("userMsg")?.token || "";
       if (this.jsessionid) {
@@ -48,53 +21,49 @@ class HTTP {
       return config;
     });
 
-    // 响应拦截器 - 自动更新 JSESSIONID + 会话过期处理
-    this.instance.interceptors.response.use((response) => {
-      this._handleSessionCookies(response);
-      this._checkSessionExpired(response);
-      return response;
-    }, (error) => {
-      return Promise.reject(error);
-    });
-
-    console.log("接口地址", this.baseUrl);
+    // 响应拦截器
+    this.instance.interceptors.response.use(
+      (response) => {
+        this._handleSessionCookies(response);
+        this._checkSessionExpired(response);
+        return response;
+      },
+      (error) => Promise.reject(error)
+    );
   }
 
+  /**
+   * 发送请求
+   */
   request({ url, data = {}, method = "POST", showLoading = "" }) {
     // 分页边界处理
-    try {
-      if (data.content?.pageNumber && data.content.pageTotal) {
-        if (data.content.pageNumber > data.content.pageTotal) {
-          return Promise.resolve({ code: 0 });
-        }
+    if (data.content?.pageNumber && data.content.pageTotal) {
+      if (data.content.pageNumber > data.content.pageTotal) {
+        return Promise.resolve({ code: 0 });
       }
-    } catch (e) { }
+    }
 
     if (showLoading) {
       uni.showLoading({ title: showLoading, mask: true });
     }
 
-    return this.instance({
-      url,
-      method,
-      data
-    })
+    return this.instance({ url, method, data })
       .then((res) => {
         let result = res.data;
+
         // 分页处理
-        try {
-          if (result.pageNumber) {
-            result.firstPage = result.pageNumber === 1;
-            if (data.content?.copyContent) {
-              result.pageNumber = data.content.copyContent.pageNumber;
-              result.pageTotal = data.content.copyContent.pageTotal;
-              data.content.pageSize = data.content.copyContent.pageSize;
-              delete data.content.copyContent;
-            } else {
-              result.pageNumber++;
-            }
+        if (result.pageNumber) {
+          result.firstPage = result.pageNumber === 1;
+          if (data.content?.copyContent) {
+            result.pageNumber = data.content.copyContent.pageNumber;
+            result.pageTotal = data.content.copyContent.pageTotal;
+            data.content.pageSize = data.content.copyContent.pageSize;
+            delete data.content.copyContent;
+          } else {
+            result.pageNumber++;
           }
-        } catch (e) { }
+        }
+
         return result;
       })
       .catch((err) => {
@@ -106,30 +75,49 @@ class HTTP {
       });
   }
 
-  // 处理 JSESSIONID
+  /**
+   * 静态方法 - 更新分页列表
+   */
+  static updateList(content, getList) {
+    content.copyContent = JSON.parse(JSON.stringify(content));
+    content.pageSize = (content.pageNumber - 1) * (content.pageSize || 20);
+    content.pageNumber = 1;
+    getList();
+  }
+
+  /**
+   * 静态方法 - 获取缩略图 / 压缩图
+   */
+  static getSpecifiedImage(obj, getType = false) {
+    let type = getType ? "compressed" : "thumbnail";
+    let imgObj = obj.subfiles.find((v) => v.type == type);
+    return imgObj?.url;
+  }
+
+  /**
+   * 处理 JSESSIONID
+   */
   _handleSessionCookies(res) {
-    try {
-      const setCookie = res.headers["set-cookie"] || res.headers["Set-Cookie"];
-      if (setCookie) {
-        const cookieArray = Array.isArray(setCookie) ? setCookie : [setCookie];
-        for (const cookie of cookieArray) {
-          const match = cookie.match(/JSESSIONID=([^;]+)/i);
-          if (match && match[1]) {
-            const newSessionId = match[1];
-            if (newSessionId !== this.jsessionid) {
-              this.jsessionid = newSessionId;
-              uni.setStorageSync("JSESSIONID", newSessionId);
-            }
-            break;
+    const setCookie = res.headers["set-cookie"] || res.headers["Set-Cookie"];
+    if (setCookie) {
+      const cookieArray = Array.isArray(setCookie) ? setCookie : [setCookie];
+      for (const cookie of cookieArray) {
+        const match = cookie.match(/JSESSIONID=([^;]+)/i);
+        if (match && match[1]) {
+          const newSessionId = match[1];
+          if (newSessionId !== this.jsessionid) {
+            this.jsessionid = newSessionId;
+            uni.setStorageSync("JSESSIONID", newSessionId);
           }
+          break;
         }
       }
-    } catch (error) {
-      console.error("处理 JSESSIONID 失败:", error);
     }
   }
 
-  // 会话过期处理
+  /**
+   * 会话过期处理
+   */
   _checkSessionExpired(res) {
     if (res.data?.msg === "登陆状态已过期,请重新登陆!") {
       this.jsessionid = "";