From 375513370cc01fcd976987d07797249600b0bb3e Mon Sep 17 00:00:00 2001
From: 杨磊 <505174330@qq.com>
Date: 星期四, 07 八月 2025 17:15:09 +0800
Subject: [PATCH] 'first'

---
 src/plugin/axios/index.js |   74 +++++++++++++++++++++++++++++++++++++
 1 files changed, 74 insertions(+), 0 deletions(-)

diff --git a/src/plugin/axios/index.js b/src/plugin/axios/index.js
new file mode 100644
index 0000000..8c72b15
--- /dev/null
+++ b/src/plugin/axios/index.js
@@ -0,0 +1,74 @@
+import axios from "axios";
+import myConfig from "@/assets/js/config";
+import toolClass from "@/assets/js/toolClass";
+import { Message } from "element-ui";
+
+import router from "@/router";
+
+// 鍒涘缓 axios 瀹炰緥
+const service = axios.create({
+  baseURL: myConfig.requestCtx,
+  timeout: myConfig.requestTimeOut, // 璇锋眰瓒呮椂鏃堕棿
+});
+
+// 璇锋眰鎷︽埅鍣�
+service.interceptors.request.use(
+  (config) => {
+    let token = toolClass.getCookie(myConfig.tokenKey);
+    if (token) config.headers["Authorization"] = `bearer ${token}`;
+    return config;
+  },
+  (error) => {
+    // 鍙戦�佸け璐�
+    Promise.reject(error);
+  }
+);
+
+// 鍝嶅簲鎷︽埅鍣�
+service.interceptors.response.use(
+  (response) => {
+    // dataAxios 鏄� axios 杩斿洖鏁版嵁涓殑 data
+    const dataAxios = response.data;
+    const { success } = dataAxios;
+    if (dataAxios.currentDate) {
+      sessionStorage.currentDate = new Date(dataAxios.currentDate).getTime();
+    }
+    // 鏍规嵁 code 杩涜鍒ゆ柇
+    if (success) {
+      // 澶勭悊涓婁紶鏂囦欢
+      if (dataAxios.msg == "鏂囦欢涓婁紶鎴愬姛") {
+        return dataAxios.msg;
+      }
+      // 濡傛灉杩斿洖鍊间负 甯冨皵(Boolean)绫诲瀷锛岀洿鎺ヨ繑鍥炲嵆鍙紙渚嬪锛氭洿鏀圭粦瀹氭墜鏈哄彿锛�
+      if (typeof dataAxios.data == "boolean") {
+        return dataAxios.data;
+      }
+      return dataAxios.data ? dataAxios.data : dataAxios.result;
+    } else {
+      // 鎻愮ず閿欒
+      console.error(dataAxios.msg);
+    }
+  },
+  (error) => {
+    if (error.response && error.response.status == 401) {
+      toolClass.delCookie(myConfig.tokenKey);
+      toolClass.delCookie(myConfig.userInfoKey);
+      router.replace({
+        path: "/login",
+      });
+    } else {
+      if (error.response && error.response.data && error.response.data) {
+        console.error(error.response.data.msg);
+        Message({
+          message: error.response.data.msg,
+          type: "error",
+        });
+      } else {
+        console.error("璇锋眰鍙戠敓閿欒");
+      }
+    }
+    return Promise.reject(error);
+  }
+);
+
+export default service;

--
Gitblit v1.9.1