|
@@ -1,59 +1,90 @@
|
|
|
+import Vue from "vue";
|
|
|
import axios from "axios";
|
|
|
import {
|
|
|
baseURL,
|
|
|
contentType,
|
|
|
+ debounce,
|
|
|
invalidCode,
|
|
|
- messageDuration,
|
|
|
noPermissionCode,
|
|
|
requestTimeout,
|
|
|
successCode,
|
|
|
tokenName,
|
|
|
- debounce,
|
|
|
} from "@/config/settings";
|
|
|
-import { Loading, Message } from "element-ui";
|
|
|
import store from "@/store";
|
|
|
import qs from "qs";
|
|
|
import router from "@/router";
|
|
|
-import _ from "lodash";
|
|
|
import { isArray } from "@/utils/validate";
|
|
|
|
|
|
-const service = axios.create({
|
|
|
+let loadingInstance;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @copyright chuzhixin 1204505056@qq.com
|
|
|
+ * @description 判断当前url是否需要加loading
|
|
|
+ * @param {*} config
|
|
|
+ * @returns
|
|
|
+ */
|
|
|
+const needLoading = (config) => {
|
|
|
+ let status = false;
|
|
|
+ debounce.forEach((item) => {
|
|
|
+ if (Vue.prototype.$baseLodash.includes(config.url, item)) {
|
|
|
+ status = true;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ return status;
|
|
|
+};
|
|
|
+
|
|
|
+/**
|
|
|
+ * @copyright chuzhixin 1204505056@qq.com
|
|
|
+ * @description 处理code异常
|
|
|
+ * @param {*} code
|
|
|
+ * @param {*} msg
|
|
|
+ */
|
|
|
+const handleCode = (code, msg) => {
|
|
|
+ switch (code) {
|
|
|
+ case invalidCode:
|
|
|
+ Vue.prototype.$baseMessage(msg || `后端接口${code}异常`, "error");
|
|
|
+ store.dispatch("user/resetAccessToken").catch(() => {});
|
|
|
+ break;
|
|
|
+ case noPermissionCode:
|
|
|
+ router.push({ path: "/401" }).catch(() => {});
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ Vue.prototype.$baseMessage(msg || `后端接口${code}异常`, "error");
|
|
|
+ break;
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+const instance = axios.create({
|
|
|
baseURL,
|
|
|
timeout: requestTimeout,
|
|
|
headers: {
|
|
|
"Content-Type": contentType,
|
|
|
},
|
|
|
});
|
|
|
-let loadingInstance;
|
|
|
-service.interceptors.request.use(
|
|
|
+
|
|
|
+instance.interceptors.request.use(
|
|
|
(config) => {
|
|
|
if (store.getters["user/accessToken"]) {
|
|
|
config.headers[tokenName] = store.getters["user/accessToken"];
|
|
|
}
|
|
|
- if (config.data) {
|
|
|
- //这里会过滤所有为空、0、false的key,如果不需要请自行注释
|
|
|
- config.data = _.pickBy(config.data, _.identity);
|
|
|
- }
|
|
|
- if (process.env.NODE_ENV !== "preview") {
|
|
|
- if (contentType === "application/x-www-form-urlencoded;charset=UTF-8") {
|
|
|
- if (config.data) {
|
|
|
- config.data = qs.stringify(config.data);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- const needLoading = () => {
|
|
|
- let status = false;
|
|
|
- debounce.forEach((item) => {
|
|
|
- if (_.includes(config.url, item)) {
|
|
|
- status = true;
|
|
|
- }
|
|
|
- });
|
|
|
- return status;
|
|
|
- };
|
|
|
- if (needLoading()) {
|
|
|
- loadingInstance = Loading.service();
|
|
|
+ //这里会过滤所有为空、0、false的key,如果不需要请自行注释
|
|
|
+ if (config.data)
|
|
|
+ config.data = Vue.prototype.$baseLodash.pickBy(
|
|
|
+ config.data,
|
|
|
+ Vue.prototype.$baseLodash.identity
|
|
|
+ );
|
|
|
+
|
|
|
+ if (
|
|
|
+ process.env.NODE_ENV !== "preview" &&
|
|
|
+ contentType === "application/x-www-form-urlencoded;charset=UTF-8" &&
|
|
|
+ config.data
|
|
|
+ ) {
|
|
|
+ config.data = qs.stringify(config.data);
|
|
|
}
|
|
|
|
|
|
+ if (needLoading(config)) {
|
|
|
+ loadingInstance = Vue.prototype.$baseLoading();
|
|
|
+ }
|
|
|
return config;
|
|
|
},
|
|
|
(error) => {
|
|
@@ -61,41 +92,21 @@ service.interceptors.request.use(
|
|
|
}
|
|
|
);
|
|
|
|
|
|
-const errorMsg = (message) => {
|
|
|
- return Message({
|
|
|
- message: message,
|
|
|
- type: "error",
|
|
|
- duration: messageDuration,
|
|
|
- });
|
|
|
-};
|
|
|
-
|
|
|
-service.interceptors.response.use(
|
|
|
+instance.interceptors.response.use(
|
|
|
(response) => {
|
|
|
- if (loadingInstance) {
|
|
|
- loadingInstance.close();
|
|
|
- }
|
|
|
+ if (loadingInstance) loadingInstance.close();
|
|
|
+
|
|
|
const { status, data, config } = response;
|
|
|
const { code, msg } = data;
|
|
|
// 操作正常Code数组
|
|
|
- let codeVerificationArray = isArray(successCode)
|
|
|
+ const codeVerificationArray = isArray(successCode)
|
|
|
? [...successCode]
|
|
|
: [...[successCode]];
|
|
|
// 是否操作正常
|
|
|
if (codeVerificationArray.includes(code)) {
|
|
|
return data;
|
|
|
} else {
|
|
|
- switch (code) {
|
|
|
- case invalidCode:
|
|
|
- errorMsg(msg || `后端接口${code}异常`);
|
|
|
- store.dispatch("user/resetAccessToken").catch(() => {});
|
|
|
- break;
|
|
|
- case noPermissionCode:
|
|
|
- router.push({ path: "/401" }).catch(() => {});
|
|
|
- break;
|
|
|
- default:
|
|
|
- errorMsg(msg || `后端接口${code}异常`);
|
|
|
- break;
|
|
|
- }
|
|
|
+ handleCode(code, msg);
|
|
|
return Promise.reject(
|
|
|
"vue-admin-beautiful请求异常拦截:" +
|
|
|
JSON.stringify({ url: config.url, code, msg }) || "Error"
|
|
@@ -103,26 +114,14 @@ service.interceptors.response.use(
|
|
|
}
|
|
|
},
|
|
|
(error) => {
|
|
|
- if (loadingInstance) {
|
|
|
- loadingInstance.close();
|
|
|
- }
|
|
|
- /*网络连接过程异常处理*/
|
|
|
- let { message } = error;
|
|
|
- if (message === "Network Error") {
|
|
|
- message = "后端接口连接异常";
|
|
|
- }
|
|
|
- if (message.includes("timeout")) {
|
|
|
- message = "后端接口请求超时";
|
|
|
- }
|
|
|
- if (message.includes("Request failed with status code")) {
|
|
|
- message = "后端接口" + message.substr(message.length - 3) + "异常";
|
|
|
+ if (loadingInstance) loadingInstance.close();
|
|
|
+ const { response, message } = error;
|
|
|
+ if (error.response && error.response.data) {
|
|
|
+ const { status, data } = response;
|
|
|
+ handleCode(status, data.msg || message);
|
|
|
+ return Promise.reject(error);
|
|
|
}
|
|
|
- if (error.response) {
|
|
|
- const { data } = error.response;
|
|
|
- message = data.msg;
|
|
|
- }
|
|
|
- errorMsg(message || "后端接口未知异常");
|
|
|
- return Promise.reject(error);
|
|
|
}
|
|
|
);
|
|
|
-export default service;
|
|
|
+
|
|
|
+export default instance;
|