1
0
mirror of https://github.com/1Panel-dev/1Panel.git synced 2025-01-21 01:09:17 +08:00
1Panel/frontend/src/api/index.ts

101 lines
3.7 KiB
Go
Raw Normal View History

2022-08-16 23:30:23 +08:00
import axios, { AxiosInstance, AxiosError, AxiosRequestConfig, AxiosResponse } from 'axios';
import { ResultData } from '@/api/interface';
import { ResultEnum } from '@/enums/http-enum';
import { checkStatus } from './helper/check-status';
import { ElMessage } from 'element-plus';
import router from '@/routers';
import { GlobalStore } from '@/store';
const globalStore = GlobalStore();
const config = {
baseURL: import.meta.env.VITE_API_URL as string,
timeout: ResultEnum.TIMEOUT as number,
withCredentials: true,
};
class RequestHttp {
service: AxiosInstance;
public constructor(config: AxiosRequestConfig) {
this.service = axios.create(config);
this.service.interceptors.request.use(
(config: AxiosRequestConfig) => {
if (config.method != 'get') {
config.headers = {
'X-CSRF-TOKEN': globalStore.csrfToken,
...config.headers,
};
}
2022-08-25 17:54:52 +08:00
2022-08-16 23:30:23 +08:00
return {
...config,
};
},
(error: AxiosError) => {
return Promise.reject(error);
},
);
this.service.interceptors.response.use(
(response: AxiosResponse) => {
2022-08-25 17:54:52 +08:00
const { data } = response;
2022-08-16 23:30:23 +08:00
if (response.headers['x-csrf-token']) {
globalStore.setCsrfToken(response.headers['x-csrf-token']);
}
if (data.code == ResultEnum.OVERDUE || data.code == ResultEnum.FORBIDDEN) {
2022-08-16 23:30:23 +08:00
router.replace({
path: '/login',
});
return Promise.reject(data);
}
2022-09-15 17:15:03 +08:00
if (data.code == ResultEnum.UNSAFETY) {
router.replace({
path: '/login',
});
return data;
}
if (data.code == ResultEnum.EXPIRED) {
router.push({ name: 'Expired' });
return data;
}
if (data.code == ResultEnum.ERRAUTH) {
return data;
}
2022-08-16 23:30:23 +08:00
if (data.code && data.code !== ResultEnum.SUCCESS) {
2022-12-05 12:20:28 +08:00
ElMessage.error(data.message);
2022-08-16 23:30:23 +08:00
return Promise.reject(data);
}
return data;
},
async (error: AxiosError) => {
const { response } = error;
if (error.message.indexOf('timeout') !== -1) ElMessage.error('请求超时请您稍后重试');
if (response) checkStatus(response.status);
if (!window.navigator.onLine) router.replace({ path: '/500' });
return Promise.reject(error);
},
);
}
get<T>(url: string, params?: object, _object = {}): Promise<ResultData<T>> {
return this.service.get(url, { params, ..._object });
}
post<T>(url: string, params?: object, _object = {}): Promise<ResultData<T>> {
return this.service.post(url, params, _object);
}
put<T>(url: string, params?: object, _object = {}): Promise<ResultData<T>> {
return this.service.put(url, params, _object);
}
delete<T>(url: string, params?: any, _object = {}): Promise<ResultData<T>> {
return this.service.delete(url, { params, ..._object });
}
2022-09-06 17:48:49 +08:00
download<BlobPart>(url: string, params?: object, _object = {}): Promise<BlobPart> {
return this.service.post(url, params, _object);
}
2022-11-08 19:03:38 +08:00
upload<T>(url: string, params: object = {}, config?: AxiosRequestConfig): Promise<T> {
2022-09-13 11:10:02 +08:00
return this.service.post(url, params, config);
}
2022-08-16 23:30:23 +08:00
}
export default new RequestHttp(config);