|
@@ -0,0 +1,93 @@
|
|
|
+import axios from "axios";
|
|
|
+import { loadProgressBar } from "axios-progress-bar";
|
|
|
+
|
|
|
+import axiosRetry from "axios-retry";
|
|
|
+import { useMessage } from "naive-ui";
|
|
|
+const message = useMessage();
|
|
|
+import { store } from "@/store/store";
|
|
|
+
|
|
|
+const config = {
|
|
|
+
|
|
|
+ timeout: 1 * 60 * 1000,
|
|
|
+ withCredentials: true,
|
|
|
+};
|
|
|
+
|
|
|
+const _axiosApp = axios.create(config);
|
|
|
+axiosRetry(_axiosApp);
|
|
|
+
|
|
|
+_axiosApp.interceptors.request.use(
|
|
|
+ function (config) {
|
|
|
+ if (config.setGlobalMask) {
|
|
|
+ store.increaseGlobalMaskCount("axios");
|
|
|
+ }
|
|
|
+ return config;
|
|
|
+ },
|
|
|
+ function (error) {
|
|
|
+ if (error.config.setGlobalMask) {
|
|
|
+ store.decreaseGlobalMaskCount("axios");
|
|
|
+ }
|
|
|
+ message.error(JSON.stringify(error));
|
|
|
+ console.log(error);
|
|
|
+ return Promise.reject(error);
|
|
|
+ }
|
|
|
+);
|
|
|
+
|
|
|
+
|
|
|
+_axiosApp.interceptors.response.use(
|
|
|
+ (response) => {
|
|
|
+ if (response.config.setGlobalMask) {
|
|
|
+ store.decreaseGlobalMaskCount("axios");
|
|
|
+ }
|
|
|
+ return response;
|
|
|
+ },
|
|
|
+ (error) => {
|
|
|
+ if (error.config?.setGlobalMask) {
|
|
|
+ store.decreaseGlobalMaskCount("axios");
|
|
|
+ }
|
|
|
+ const showErrorMessage = !error.config?.noErrorMessage;
|
|
|
+ if (!error.response) {
|
|
|
+ if (showErrorMessage) {
|
|
|
+
|
|
|
+ message.error("网络连接异常,请检查网络设置。");
|
|
|
+ }
|
|
|
+ return Promise.reject(error);
|
|
|
+ }
|
|
|
+
|
|
|
+ const status = error.response.status;
|
|
|
+
|
|
|
+
|
|
|
+ if (status == 403 || status == 401) {
|
|
|
+
|
|
|
+ return Promise.reject(error);
|
|
|
+ } else if (status == 405) {
|
|
|
+ if (showErrorMessage) {
|
|
|
+ message.error("没有权限!");
|
|
|
+ }
|
|
|
+ return Promise.reject(error);
|
|
|
+ } else if (status == 502) {
|
|
|
+ if (showErrorMessage) {
|
|
|
+ message.error("服务器异常(502)!");
|
|
|
+ }
|
|
|
+ return Promise.reject(error);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (status != 200) {
|
|
|
+ const data = error.response.data;
|
|
|
+ if (data && data.message) {
|
|
|
+ if (showErrorMessage) {
|
|
|
+ message.error(data.message + "");
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if (showErrorMessage) {
|
|
|
+ message.error("未定义异常: " + JSON.stringify(data, null, 2));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return Promise.reject(error);
|
|
|
+ }
|
|
|
+ }
|
|
|
+);
|
|
|
+
|
|
|
+
|
|
|
+loadProgressBar(null, _axiosApp);
|
|
|
+
|
|
|
+export const httpApp = _axiosApp;
|