|
@@ -0,0 +1,126 @@
|
|
|
+import Vue from "vue";
|
|
|
+// import Store from "@/store";
|
|
|
+import axios from "axios";
|
|
|
+// @ts-ignore
|
|
|
+import { loadProgressBar } from "axios-progress-bar";
|
|
|
+import cachingGet from "./axiosCache";
|
|
|
+import { notifyInvalidTokenThrottled } from "./axiosNotice";
|
|
|
+import axiosRetry from "axios-retry";
|
|
|
+// import { Notification } from "element-ui";
|
|
|
+const Notification = (...args: any) => {
|
|
|
+ console.log(args);
|
|
|
+};
|
|
|
+
|
|
|
+const config = {
|
|
|
+ // baseURL: process.env.baseURL || process.env.apiUrl || ""
|
|
|
+ timeout: 1 * 60 * 1000, // Timeout
|
|
|
+ withCredentials: true, // Check cross-site Access-Control
|
|
|
+};
|
|
|
+const cacheGetUrls: [RegExp] | [] = [];
|
|
|
+
|
|
|
+const _axiosApp = axios.create(config);
|
|
|
+axiosRetry(_axiosApp);
|
|
|
+
|
|
|
+_axiosApp.interceptors.request.use(
|
|
|
+ function (config) {
|
|
|
+ return config;
|
|
|
+ },
|
|
|
+ function (error) {
|
|
|
+ // Do something with request error
|
|
|
+ Notification({
|
|
|
+ showClose: true,
|
|
|
+ message: error,
|
|
|
+ type: "error",
|
|
|
+ });
|
|
|
+ console.log(error);
|
|
|
+ return Promise.reject(error);
|
|
|
+ }
|
|
|
+);
|
|
|
+
|
|
|
+// Add a response interceptor
|
|
|
+_axiosApp.interceptors.response.use(
|
|
|
+ (response) => {
|
|
|
+ return response;
|
|
|
+ },
|
|
|
+ (error) => {
|
|
|
+ const showErrorMessage = !error.config?.noErrorMessage;
|
|
|
+ if (!error.response) {
|
|
|
+ if (showErrorMessage) {
|
|
|
+ // "Network Error" 网络不通,直接返回
|
|
|
+ Notification({
|
|
|
+ showClose: true,
|
|
|
+ message: "网络连接异常,请检查网络设置。",
|
|
|
+ type: "error",
|
|
|
+ });
|
|
|
+ }
|
|
|
+ return Promise.reject(error);
|
|
|
+ }
|
|
|
+ // 这里是返回状态码不为200时候的错误处理
|
|
|
+ let status = error.response.status;
|
|
|
+
|
|
|
+ // 登录失效 跳转登录页面
|
|
|
+ if (status == 403 || status == 401) {
|
|
|
+ notifyInvalidTokenThrottled();
|
|
|
+ return Promise.reject(error);
|
|
|
+ } else if (status == 405) {
|
|
|
+ if (showErrorMessage) {
|
|
|
+ Notification({
|
|
|
+ showClose: true,
|
|
|
+ message: "没有权限!",
|
|
|
+ type: "error",
|
|
|
+ });
|
|
|
+ }
|
|
|
+ return Promise.reject(error);
|
|
|
+ } else if (status == 502) {
|
|
|
+ if (showErrorMessage) {
|
|
|
+ Notification({
|
|
|
+ showClose: true,
|
|
|
+ message: "服务器异常(502)!",
|
|
|
+ type: "error",
|
|
|
+ });
|
|
|
+ }
|
|
|
+ return Promise.reject(error);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (status != 200) {
|
|
|
+ const data = error.response.data;
|
|
|
+ if (data && data.message) {
|
|
|
+ if (showErrorMessage) {
|
|
|
+ Notification({
|
|
|
+ showClose: true,
|
|
|
+ message: data.message,
|
|
|
+ type: "error",
|
|
|
+ });
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if (showErrorMessage) {
|
|
|
+ Notification({
|
|
|
+ showClose: true,
|
|
|
+ message: "未定义异常: " + JSON.stringify(data, null, 2),
|
|
|
+ type: "error",
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return Promise.reject(error);
|
|
|
+ }
|
|
|
+ }
|
|
|
+);
|
|
|
+
|
|
|
+_axiosApp.get = cachingGet(_axiosApp, cacheGetUrls);
|
|
|
+loadProgressBar(null, _axiosApp);
|
|
|
+
|
|
|
+// Plugin.install = function (Vue) {
|
|
|
+// Object.defineProperties(Vue.prototype, {
|
|
|
+// $http: {
|
|
|
+// get() {
|
|
|
+// return _axiosApp;
|
|
|
+// },
|
|
|
+// },
|
|
|
+// });
|
|
|
+// };
|
|
|
+
|
|
|
+// Vue.use(Plugin);
|
|
|
+
|
|
|
+// export default Plugin;
|
|
|
+
|
|
|
+export const httpApp = _axiosApp;
|