114 lines
3.2 KiB
TypeScript
114 lines
3.2 KiB
TypeScript
import axios, {AxiosRequestConfig, AxiosResponse} from "axios";
|
||
import type {MessageInstance} from "antd/es/message/interface";
|
||
|
||
export const Axios = axios;
|
||
export const DevUserId: string = '80d3365e-0597-4988-979e-18ef1c3ec671';
|
||
|
||
type Req = {
|
||
messageApi: MessageInstance;
|
||
url: string;
|
||
body?: any;
|
||
config?: AxiosRequestConfig;
|
||
onBefore?(): void;
|
||
onSuccess(data: AxiosResponse): void;
|
||
onFinally?(): void;
|
||
}
|
||
|
||
axios.defaults.baseURL = 'http://127.0.0.1:7025/copyright';
|
||
|
||
axios.interceptors.request.use(config => {
|
||
if (config.method === 'get') {
|
||
config.data = {unused: 0} // 这个是关键点,解决get 请求添加不上content_type
|
||
}
|
||
config.headers['X-USER-ID'] = DevUserId;
|
||
return config
|
||
}
|
||
);
|
||
|
||
export function downloadUrl(fileId: string, isDownload?: boolean) {
|
||
return `${Axios.defaults?.baseURL}/route/file/v2/download/${isDownload == false}/${fileId}`
|
||
}
|
||
|
||
export function uploadImageUrl() {
|
||
return `${Axios.defaults?.baseURL}/api/file/v2/upload-image`;
|
||
}
|
||
|
||
export function uploadFileUrl() {
|
||
return `${Axios.defaults?.baseURL}/api/file/v2/upload-file`;
|
||
}
|
||
|
||
export function get<T>(req: Req) {
|
||
req.onBefore?.();
|
||
Axios.get<T>(req.url, req.config).then(res => {
|
||
req.onSuccess(res);
|
||
}).catch(error => {
|
||
if (error.response) {
|
||
const data = error.response.data;
|
||
req.messageApi.open({
|
||
type: 'error',
|
||
content: data.msg ? data.msg : `${data.path}(${data.status})`,
|
||
});
|
||
} else {
|
||
console.error(error)
|
||
}
|
||
}).finally(() => {
|
||
req.onFinally?.();
|
||
})
|
||
}
|
||
|
||
export function post<T>(req: Req) {
|
||
req.onBefore?.();
|
||
Axios.post<T>(req.url, req.body, req.config).then(res => {
|
||
req.onSuccess(res);
|
||
}).catch(error => {
|
||
if (error.response) {
|
||
const data = error.response.data;
|
||
req.messageApi.open({
|
||
type: 'error',
|
||
content: data.msg ? data.msg : `${data.path}(${data.status})`,
|
||
});
|
||
} else {
|
||
console.error(error)
|
||
}
|
||
}).finally(() => {
|
||
req.onFinally?.();
|
||
})
|
||
}
|
||
|
||
export function put<T>(req: Req) {
|
||
req.onBefore?.();
|
||
Axios.put<T>(req.url, req.body, req.config).then(res => {
|
||
req.onSuccess(res);
|
||
}).catch(error => {
|
||
if (error.response) {
|
||
const data = error.response.data;
|
||
req.messageApi.open({
|
||
type: 'error',
|
||
content: data.msg ? data.msg : `${data.path}(${data.status})`,
|
||
});
|
||
} else {
|
||
console.error(error)
|
||
}
|
||
}).finally(() => {
|
||
req.onFinally?.();
|
||
})
|
||
}
|
||
|
||
export function del<T>(req: Req) {
|
||
req.onBefore?.();
|
||
Axios.delete<T>(req.url, req.config).then(res => {
|
||
req.onSuccess(res);
|
||
}).catch(error => {
|
||
if (error.response) {
|
||
const data = error.response.data;
|
||
req.messageApi.open({
|
||
type: 'error',
|
||
content: data.msg ? data.msg : `${data.path}(${data.status})`,
|
||
});
|
||
} else {
|
||
console.error(error)
|
||
}
|
||
}).finally(() => {
|
||
req.onFinally?.();
|
||
})
|
||
} |