|
|
|
@ -1,36 +1,36 @@
|
|
|
|
|
import axios from "axios";
|
|
|
|
|
// import { Loading } from "element-ui"; //项目已经全局引入element的话可以不单独引入
|
|
|
|
|
// let loading; //定义loading变量
|
|
|
|
|
// function startLoading() {
|
|
|
|
|
// //使用Element loading-start 方法
|
|
|
|
|
// loading = Loading.service({
|
|
|
|
|
// lock: true,
|
|
|
|
|
// text: "加载中……",
|
|
|
|
|
// background: "rgba(255, 255, 255, 0.7)",
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
// function endLoading() {
|
|
|
|
|
// //使用Element loading-close 方法
|
|
|
|
|
// loading.close();
|
|
|
|
|
// }
|
|
|
|
|
import { Loading } from "element-ui"; //项目已经全局引入element的话可以不单独引入
|
|
|
|
|
let loading; //定义loading变量
|
|
|
|
|
function startLoading() {
|
|
|
|
|
//使用Element loading-start 方法
|
|
|
|
|
loading = Loading.service({
|
|
|
|
|
lock: true,
|
|
|
|
|
text: "加载中……",
|
|
|
|
|
background: "rgba(255, 255, 255, 0.7)",
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
function endLoading() {
|
|
|
|
|
//使用Element loading-close 方法
|
|
|
|
|
loading.close();
|
|
|
|
|
}
|
|
|
|
|
// 那么 showFullScreenLoading() tryHideFullScreenLoading() 要干的事儿就是将同一时刻的请求合并。
|
|
|
|
|
// 声明一个变量 needLoadingRequestCount,每次调用showFullScreenLoading方法 needLoadingRequestCount + 1。
|
|
|
|
|
// 调用tryHideFullScreenLoading()方法,needLoadingRequestCount - 1。needLoadingRequestCount为 0 时,结束 loading。
|
|
|
|
|
// let needLoadingRequestCount = 0;
|
|
|
|
|
// export function showFullScreenLoading() {
|
|
|
|
|
// if (needLoadingRequestCount === 0) {
|
|
|
|
|
// startLoading();
|
|
|
|
|
// }
|
|
|
|
|
// needLoadingRequestCount++;
|
|
|
|
|
// }
|
|
|
|
|
let needLoadingRequestCount = 0;
|
|
|
|
|
export function showFullScreenLoading() {
|
|
|
|
|
if (needLoadingRequestCount === 0) {
|
|
|
|
|
startLoading();
|
|
|
|
|
}
|
|
|
|
|
needLoadingRequestCount++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// export function tryHideFullScreenLoading() {
|
|
|
|
|
// if (needLoadingRequestCount <= 0) return;
|
|
|
|
|
// needLoadingRequestCount--;
|
|
|
|
|
// if (needLoadingRequestCount === 0) {
|
|
|
|
|
// endLoading();
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
export function tryHideFullScreenLoading() {
|
|
|
|
|
if (needLoadingRequestCount <= 0) return;
|
|
|
|
|
needLoadingRequestCount--;
|
|
|
|
|
if (needLoadingRequestCount === 0) {
|
|
|
|
|
endLoading();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const service = axios.create({
|
|
|
|
|
// process.env.NODE_ENV === 'development' 来判断是否开发环境
|
|
|
|
@ -43,7 +43,7 @@ const service = axios.create({
|
|
|
|
|
|
|
|
|
|
service.interceptors.request.use(
|
|
|
|
|
(config) => {
|
|
|
|
|
//showFullScreenLoading();
|
|
|
|
|
showFullScreenLoading();
|
|
|
|
|
return config;
|
|
|
|
|
},
|
|
|
|
|
(error) => {
|
|
|
|
@ -55,7 +55,7 @@ service.interceptors.request.use(
|
|
|
|
|
service.interceptors.response.use(
|
|
|
|
|
(response) => {
|
|
|
|
|
if (response.status === 200) {
|
|
|
|
|
// tryHideFullScreenLoading();
|
|
|
|
|
tryHideFullScreenLoading();
|
|
|
|
|
return response.data;
|
|
|
|
|
} else {
|
|
|
|
|
Promise.reject();
|
|
|
|
|