web init
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
import axios, { AxiosRequestConfig } from "axios";
|
||||
import { ElLoading } from "element-plus";
|
||||
|
||||
const request = axios.create({
|
||||
baseURL: "/api",
|
||||
timeout: 30000,
|
||||
headers: {
|
||||
'Content-Type': 'application/json;charset=UTF-8',
|
||||
}
|
||||
})
|
||||
|
||||
let loadingInstance: ReturnType<typeof ElLoading.service> | null = null;
|
||||
|
||||
request.interceptors.request.use(config => {
|
||||
if (config.showLoading) {
|
||||
loadingInstance = ElLoading.service({
|
||||
lock: true,
|
||||
text: "加载中...",
|
||||
background: "rgba(0, 0, 0, 0.7)",
|
||||
});
|
||||
}
|
||||
return config;
|
||||
});
|
||||
|
||||
request.interceptors.response.use(response => {
|
||||
if (loadingInstance) {
|
||||
loadingInstance.close();
|
||||
loadingInstance = null;
|
||||
}
|
||||
return response;
|
||||
});
|
||||
|
||||
export const api = {
|
||||
get: function(url: string, config = {} as AxiosRequestConfig) { return request.get(url, config) },
|
||||
post: function(url: string, data = {}, config = {} as AxiosRequestConfig) { return request.post(url, data, config) },
|
||||
put: function(url: string, data = {}, config = {} as AxiosRequestConfig) { return request.put(url, data, config) },
|
||||
delete: function(url: string, config = {} as AxiosRequestConfig) { return request.delete(url, config) },
|
||||
upload: function(url: string, formData: FormData, config = {} as AxiosRequestConfig) {
|
||||
return request.post(url, formData, {
|
||||
...config,
|
||||
headers: {
|
||||
'Content-Type': 'multipart/form-data',
|
||||
...(config.headers as Record<string, string>)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export default request;
|
||||
Reference in New Issue
Block a user