mirror of
https://github.com/talktao/Vue3-Vite-Vant-TS-H5.git
synced 2025-04-06 03:57:55 +08:00
fix:完善请求loading配置
This commit is contained in:
parent
d9a5e06c89
commit
17ade1be2c
@ -2,7 +2,7 @@
|
|||||||
* @author TalkTao
|
* @author TalkTao
|
||||||
* @description 接口封装
|
* @description 接口封装
|
||||||
*/
|
*/
|
||||||
import store from "@/store/index";
|
import { vuexStore } from "@/store/index";
|
||||||
import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse, Method } from "axios";
|
import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse, Method } from "axios";
|
||||||
export interface AxiosResponseProps {
|
export interface AxiosResponseProps {
|
||||||
code?: number;
|
code?: number;
|
||||||
@ -32,12 +32,35 @@ class HttpRequest {
|
|||||||
interceptors(instance: AxiosInstance) {
|
interceptors(instance: AxiosInstance) {
|
||||||
let requestList = [];
|
let requestList = [];
|
||||||
const setLoadingToFalse = response => {
|
const setLoadingToFalse = response => {
|
||||||
|
|
||||||
requestList
|
requestList
|
||||||
.filter(item => item.url == response.config.url && item.method == response.config.method)
|
.filter(item => item.url == response.config.url && item.method == response.config.method)
|
||||||
.forEach(item => (item.isLoading = false));
|
.forEach(item => (item.isLoading = false));
|
||||||
|
|
||||||
//所有请求都加载完才让加载提示消失
|
//所有请求都加载完才让加载提示消失
|
||||||
if (requestList.every(item => !item.isLoading)) store.commit("changeIsLoading", false);
|
if (requestList.every(item => !item.isLoading)) vuexStore.commit("changeIsLoading", false);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
instance.interceptors.request.use(
|
||||||
|
(config: any) => {
|
||||||
|
|
||||||
|
let ignoreLoadingUrls = ["/login"];
|
||||||
|
|
||||||
|
if (!ignoreLoadingUrls.includes(config.url)) {
|
||||||
|
|
||||||
|
if (requestList.length < 10) {
|
||||||
|
requestList.unshift({ ...config, isLoading: true });
|
||||||
|
} else {
|
||||||
|
requestList = [...requestList.slice(0, 9), { ...config, isLoading: true }];
|
||||||
|
}
|
||||||
|
vuexStore.commit("changeIsLoading", true);
|
||||||
|
}
|
||||||
|
return config;
|
||||||
|
},
|
||||||
|
error => Promise.reject(error + '请求错误')
|
||||||
|
);
|
||||||
|
|
||||||
instance.interceptors.response.use(
|
instance.interceptors.response.use(
|
||||||
response => {
|
response => {
|
||||||
setLoadingToFalse(response);
|
setLoadingToFalse(response);
|
||||||
@ -45,7 +68,7 @@ class HttpRequest {
|
|||||||
},
|
},
|
||||||
error => {
|
error => {
|
||||||
if (error.response.status == 301) {
|
if (error.response.status == 301) {
|
||||||
store.commit("changeLoginModalVisible", true);
|
vuexStore.commit("changeLoginModalVisible", true);
|
||||||
}
|
}
|
||||||
setLoadingToFalse(error);
|
setLoadingToFalse(error);
|
||||||
return Promise.reject(error.response?.data);
|
return Promise.reject(error.response?.data);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user