|
@@ -1,6 +1,8 @@
|
|
|
import Vue from "vue";
|
|
|
import axios from "axios";
|
|
|
import router from "../router";
|
|
|
+import { loadProgressBar } from "axios-progress-bar";
|
|
|
+import networkInformationHint from "./networkInformationHint.js";
|
|
|
|
|
|
const ERROR_MSG_CONFIG = require("./errorMsgConfig").default;
|
|
|
|
|
@@ -15,8 +17,8 @@ let config = {
|
|
|
withCredentials: true // Check cross-site Access-Control
|
|
|
};
|
|
|
|
|
|
-const _axios = axios.create(config);
|
|
|
-const _axiosWithoutResponseInterceptors = axios.create(config);
|
|
|
+const _$httpWith500Msg = axios.create(config);
|
|
|
+const _$http = axios.create(config); // no auto 500 error UI
|
|
|
|
|
|
/**
|
|
|
* A. token lifecycle
|
|
@@ -37,8 +39,9 @@ function getRootOrgId() {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-_axios.interceptors.request.use(
|
|
|
+_$httpWith500Msg.interceptors.request.use(
|
|
|
function(config) {
|
|
|
+ networkInformationHint();
|
|
|
// Do something before request is sent
|
|
|
if (
|
|
|
config.url.includes("/login") === false &&
|
|
@@ -84,8 +87,10 @@ _axios.interceptors.request.use(
|
|
|
}
|
|
|
);
|
|
|
|
|
|
-_axiosWithoutResponseInterceptors.interceptors.request.use(
|
|
|
+_$http.interceptors.request.use(
|
|
|
+ // no auto 500 error UI
|
|
|
function(config) {
|
|
|
+ networkInformationHint();
|
|
|
// Do something before request is sent
|
|
|
if (config.url.includes("/login") === false) {
|
|
|
if (!wk_token) {
|
|
@@ -123,9 +128,32 @@ _axiosWithoutResponseInterceptors.interceptors.request.use(
|
|
|
}
|
|
|
);
|
|
|
|
|
|
+const recordRequest = response => {
|
|
|
+ let matchedRoutePath;
|
|
|
+ try {
|
|
|
+ const matched = router.resolve(location).route.matched;
|
|
|
+ const exactMatched = matched[matched.length - 1];
|
|
|
+ matchedRoutePath = exactMatched.path;
|
|
|
+ } catch (error) {
|
|
|
+ console.log(error);
|
|
|
+ window._hmt.push([
|
|
|
+ "_trackEvent",
|
|
|
+ `页面-${location.pathname}`,
|
|
|
+ "网络请求-响应",
|
|
|
+ "解析出错"
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+ window._hmt.push([
|
|
|
+ "_trackEvent",
|
|
|
+ `页面-${matchedRoutePath || location.pathname}`,
|
|
|
+ "网络请求-响应",
|
|
|
+ new URL(response.config.url, "http://www.qmth.com.cn").pathname
|
|
|
+ ]);
|
|
|
+};
|
|
|
// Add a response interceptor
|
|
|
-_axios.interceptors.response.use(
|
|
|
+_$httpWith500Msg.interceptors.response.use(
|
|
|
response => {
|
|
|
+ recordRequest(response);
|
|
|
return response;
|
|
|
},
|
|
|
error => {
|
|
@@ -199,8 +227,10 @@ _axios.interceptors.response.use(
|
|
|
);
|
|
|
|
|
|
// Add a response interceptor
|
|
|
-_axiosWithoutResponseInterceptors.interceptors.response.use(
|
|
|
+_$http.interceptors.response.use(
|
|
|
+ // no auto 500 error UI
|
|
|
response => {
|
|
|
+ recordRequest(response);
|
|
|
return response;
|
|
|
},
|
|
|
error => {
|
|
@@ -246,19 +276,33 @@ _axiosWithoutResponseInterceptors.interceptors.response.use(
|
|
|
);
|
|
|
|
|
|
Plugin.install = function(Vue) {
|
|
|
- Vue.$http = _axiosWithoutResponseInterceptors;
|
|
|
+ Vue.$http = _$http; // no auto 500 error UI
|
|
|
Object.defineProperties(Vue.prototype, {
|
|
|
$http: {
|
|
|
get() {
|
|
|
- return _axiosWithoutResponseInterceptors;
|
|
|
+ return _$http; // no auto 500 error UI
|
|
|
}
|
|
|
}
|
|
|
});
|
|
|
- Vue.$httpWithMsg = _axios;
|
|
|
+
|
|
|
+ Vue.$httpWithMsg = _$httpWith500Msg;
|
|
|
Object.defineProperties(Vue.prototype, {
|
|
|
$httpWithMsg: {
|
|
|
get() {
|
|
|
- return _axios;
|
|
|
+ return _$httpWith500Msg;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ // for below request
|
|
|
+ // config.url.includes("/api/ecs_ques/paper/") === false &&
|
|
|
+ // config.url.includes("/api/ecs_ques/questionAudio") === false
|
|
|
+ const _a = axios.create(config);
|
|
|
+ Vue.$httpWithoutAuth = _a;
|
|
|
+ Object.defineProperties(Vue.prototype, {
|
|
|
+ $httpWithoutAuth: {
|
|
|
+ get() {
|
|
|
+ return _a;
|
|
|
}
|
|
|
}
|
|
|
});
|
|
@@ -266,4 +310,31 @@ Plugin.install = function(Vue) {
|
|
|
|
|
|
Vue.use(Plugin);
|
|
|
|
|
|
+loadProgressBar({}, Vue.$http);
|
|
|
+loadProgressBar({}, Vue.$httpWithMsg);
|
|
|
+loadProgressBar({}, Vue.$httpWithoutAuth);
|
|
|
+
|
|
|
+// const update = (type, e) => {
|
|
|
+// // debugger;
|
|
|
+// console.log(type);
|
|
|
+// console.log(
|
|
|
+// "e.target.url: ",
|
|
|
+// e.target.responseURL,
|
|
|
+// " timeStamp: ",
|
|
|
+// e.timeStamp.toFixed(2),
|
|
|
+// " loaded:",
|
|
|
+// e.loaded,
|
|
|
+// " total: ",
|
|
|
+// e.total
|
|
|
+// );
|
|
|
+// console.log(e);
|
|
|
+// };
|
|
|
+// Vue.$httpWithMsg.defaults.onDownloadProgress = e => {
|
|
|
+// update("下载", e);
|
|
|
+// };
|
|
|
+// Vue.$httpWithMsg.defaults.onUploadProgress = e => {
|
|
|
+// update("上传", e);
|
|
|
+// };
|
|
|
+
|
|
|
+import "axios-progress-bar/dist/nprogress.css";
|
|
|
export default Plugin;
|