axios封装
- 请求配置
- 请求拦截
- 响应拦截
- 单个请求取消
- 取消所有请求
- 请求封装
import axios from "axios";
import type { AxiosRequestConfig } from "axios";
axios.defaults.baseURL = "/";
axios.defaults.timeout = 120000;
axios.defaults.headers.post["Content-Type"] =
"application/x-www-form-urlencoded;charset=UTF-8";
axios.interceptors.request.use(
(config) => {
config.cancelToken = new cancelToken((c) => {
addPending(config, c);
});
return config;
},
(error) => {
return Promise.reject(error);
}
);
axios.interceptors.response.use(
(response) => {
if (response.status === 200) {
return Promise.resolve(response);
} else {
return Promise.reject(response);
}
},
(error) => {
console.log(error);
if (error === "Error: Network Error") {
alert("网络错误,请检查网络连接");
} else if (error.response && error.response.status) {
switch (error.response.status) {
case 500:
alert("请求不存在-500");
break;
case 404:
alert("请求不存在-404");
break;
default:
alert("其他错误,请检查网络");
break;
}
}
return Promise.reject(error.response);
}
);
let pending: any[] = [];
let cancels: any[] = [];
const cancelToken = axios.CancelToken;
const addPending = (config: any, fn?: any) => {
if (pending.includes(config.url)) {
let index = pending.indexOf(config.url);
cancels.splice(index, 1, fn);
} else {
if (fn) {
pending.push(config.url);
cancels.push(fn);
}
}
};
export function cancelApi(url: string, cancelMsg?: string) {
if (pending.includes(url)) {
let index = pending.indexOf(url);
pending.splice(index, 1);
let cancel = cancels.splice(index, 1)[0];
cancelMsg ? cancel(cancelMsg) : cancel();
}
}
export function cancelAllApi() {
pending = [];
cancels.forEach((cancel) => cancel());
cancels = [];
}
export enum Method {
get = "get",
GET = "GET",
delete = "delete",
DELETE = "DELETE",
head = "head",
HEAD = "HEAD",
options = "options",
OPTIONS = "OPTIONS",
post = "post",
POST = "POST",
put = "put",
PUT = "PUT",
patch = "patch",
PATCH = "PATCH",
purge = "purge",
PURGE = "PURGE",
link = "link",
LINK = "LINK",
unlink = "unlink",
UNLINK = "UNLINK",
}
export function baseHttp(
url: string,
method: Method,
config: AxiosRequestConfig & any
) {
config.url = url;
config.method = method;
if (config.url?.indexOf("/") != 0) {
config.url = "/" + config.url;
}
config.url = config.url.replace(/\/\//gi, "/");
let sendConfig: object = {
data: config.data ? config.data : config.params,
params: config.params ? config.params : config.data,
};
sendConfig = Object.assign(config, sendConfig);
return new Promise((resolve, reject) => {
axios(sendConfig)
.then((res) => resolve(res.data))
.catch((e) => reject(e))
.finally(() => {
cancelApi(config.url);
});
});
}
export let get = (url: string, config: AxiosRequestConfig & any = {}) => {
return baseHttp(url, Method.GET, config);
};
export function post(url: string, config: AxiosRequestConfig & any = {}) {
return baseHttp(url, Method.POST, config);
}
export function put(url: string, config: AxiosRequestConfig & any = {}) {
return baseHttp(url, Method.PUT, config);
}
export function del(url: string, config: AxiosRequestConfig & any = {}) {
return baseHttp(url, Method.DELETE, config);
}
export function patch(url: string, config: AxiosRequestConfig & any = {}) {
return baseHttp(url, Method.PATCH, config);
}
export default { get, post, put, del, patch, cancelApi, cancelAllApi };