|
@@ -0,0 +1,84 @@
|
|
|
+import axios from "axios";
|
|
|
+import { Capacitor } from '@capacitor/core';
|
|
|
+import { showNotify } from 'vant';
|
|
|
+import { useSystemStore } from "@/stores/modules/systemStore";
|
|
|
+import { fn_logout } from "@/utils";
|
|
|
+
|
|
|
+// 用于存储请求和对应的 AbortController
|
|
|
+const requestMap = new Map();
|
|
|
+const requestCountMap = new Map(); // 用于记录请求的次数
|
|
|
+
|
|
|
+const isNativeApp = Capacitor.isNativePlatform();
|
|
|
+
|
|
|
+const axiosInstance = axios.create({
|
|
|
+ baseURL: (isNativeApp ? import.meta.env.VITE_PRO_BACKEND_PATH : import.meta.env.VITE_PRO_BACKEND_PATH) + "/api/v2", // 设置 API 基础 URL
|
|
|
+ timeout: 300000, // 设置请求超时时间
|
|
|
+});
|
|
|
+
|
|
|
+const requestState = {
|
|
|
+ success: [200],
|
|
|
+ beOverdue: [886],
|
|
|
+ NotAccessRight: [500],
|
|
|
+ exception: [400],
|
|
|
+};
|
|
|
+
|
|
|
+const pathArr = ["/api/admin/system/login", "/api/logout"];
|
|
|
+
|
|
|
+axiosInstance.interceptors.request.use(
|
|
|
+ (config) => {
|
|
|
+
|
|
|
+ const systemStore = useSystemStore();
|
|
|
+ systemStore.localLoading(true);
|
|
|
+
|
|
|
+ if (!pathArr.includes(config.url)) {
|
|
|
+ const token = localStorage.getItem("token");
|
|
|
+ if (token && config.headers) {
|
|
|
+ config.headers["Authorization"] = token;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return config;
|
|
|
+ },
|
|
|
+ (err) => {
|
|
|
+ const systemStore = useSystemStore();
|
|
|
+ systemStore.localLoading();
|
|
|
+ return Promise.reject(err);
|
|
|
+ }
|
|
|
+);
|
|
|
+
|
|
|
+// 响应拦截器
|
|
|
+axiosInstance.interceptors.response.use(
|
|
|
+ (res) => {
|
|
|
+ const systemStore = useSystemStore();
|
|
|
+ systemStore.localLoading();
|
|
|
+ const { code, message: msg } = res.data;
|
|
|
+
|
|
|
+
|
|
|
+ if (requestState.NotAccessRight.includes(code)) {
|
|
|
+ showNotify({ type: 'warning', message: msg });
|
|
|
+
|
|
|
+ return Promise.reject(msg);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (requestState.exception.includes(code)) {
|
|
|
+ showNotify({ type: 'warning', message: msg });
|
|
|
+ return Promise.reject(msg);
|
|
|
+ }
|
|
|
+
|
|
|
+ return res.data;
|
|
|
+ },
|
|
|
+ (err) => {
|
|
|
+ const systemStore = useSystemStore();
|
|
|
+ systemStore.localLoading();
|
|
|
+ const msg = err.response?.data ? err.response.data.message : "";
|
|
|
+ showNotify({ type: 'warning', message: msg });
|
|
|
+ if (requestState.beOverdue.includes( err.status)) {
|
|
|
+ //
|
|
|
+ fn_logout();
|
|
|
+ return undefined;
|
|
|
+ }
|
|
|
+ return Promise.reject(err);
|
|
|
+ }
|
|
|
+);
|
|
|
+
|
|
|
+export default axiosInstance;
|