system-copyright-react/src/util/AjaxUtils.ts

151 lines
4.3 KiB
TypeScript
Raw Normal View History

2024-03-20 18:30:39 +08:00
import axios, {AxiosRequestConfig, AxiosResponse} from "axios";
import type {MessageInstance} from "antd/es/message/interface";
2024-03-22 21:22:12 +08:00
export const Axios = axios;
2024-04-01 20:39:22 +08:00
2024-04-26 10:10:41 +08:00
axios.defaults.baseURL = 'http://192.168.0.163:7025/copyright';
// axios.defaults.baseURL = '/copyright';
export const WebSocketBaseUrl: string = 'ws://192.168.0.163:7025/copyright';
// export const WebSocketBaseUrl: string = '/copyright';
export const DevUserId: string = '80d3365e-0597-4988-979e-18ef1c3ec671'; // 18634604067
2024-04-12 18:18:14 +08:00
// export const DevUserId: string = 'c2438eb8-2685-49a9-bf02-5111a5192d96'; // 18647109157
2024-04-26 10:10:41 +08:00
// export const DevUserId: string = '';
2024-03-22 21:22:12 +08:00
2024-03-27 18:56:48 +08:00
type Req<T> = {
2024-03-20 18:30:39 +08:00
messageApi: MessageInstance;
url: string;
body?: any;
config?: AxiosRequestConfig;
onBefore?(): void;
2024-03-27 18:56:48 +08:00
onSuccess(data: AxiosResponse<T>): void;
2024-03-20 18:30:39 +08:00
onFinally?(): void;
}
2024-03-19 19:19:07 +08:00
2024-04-03 18:33:37 +08:00
export interface IDictionary {
dataId: string;
dataParentId: string;
dataName: string;
dataCode: string;
}
2024-03-20 18:30:39 +08:00
axios.interceptors.request.use(config => {
if (config.method === 'get') {
config.data = {unused: 0} // 这个是关键点解决get 请求添加不上content_type
}
2024-03-22 21:22:12 +08:00
config.headers['X-USER-ID'] = DevUserId;
2024-03-20 18:30:39 +08:00
return config
}
);
2024-04-25 15:37:50 +08:00
export function websocketUrl() {
if (WebSocketBaseUrl.startsWith('ws')) {
return WebSocketBaseUrl;
}
const location = window.location;
const protocol = location.protocol;
let wsProtocol = 'ws';
if (protocol == 'https') {
wsProtocol = 'wss';
}
return `${wsProtocol}://${location.host}${WebSocketBaseUrl}`;
}
2024-03-22 21:22:12 +08:00
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`;
}
2024-03-20 18:30:39 +08:00
2024-04-03 18:33:37 +08:00
export async function listDictionary(parentId: string, messageApi: MessageInstance) {
return new Promise<IDictionary[]>(resolve => {
get<IDictionary[]>({
messageApi,
url: `/api/data/listbyparentid/${parentId}`,
onSuccess({data}) {
resolve(data);
}
})
})
}
2024-03-27 18:56:48 +08:00
export function get<T>(req: Req<T>) {
2024-03-20 18:30:39 +08:00
req.onBefore?.();
2024-03-22 21:22:12 +08:00
Axios.get<T>(req.url, req.config).then(res => {
2024-03-20 18:30:39 +08:00
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?.();
})
}
2024-03-27 18:56:48 +08:00
export function post<T>(req: Req<T>) {
2024-03-20 18:30:39 +08:00
req.onBefore?.();
2024-03-22 21:22:12 +08:00
Axios.post<T>(req.url, req.body, req.config).then(res => {
2024-03-20 18:30:39 +08:00
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?.();
})
}
2024-03-27 18:56:48 +08:00
export function put<T>(req: Req<T>) {
2024-03-20 18:30:39 +08:00
req.onBefore?.();
2024-03-22 21:22:12 +08:00
Axios.put<T>(req.url, req.body, req.config).then(res => {
2024-03-20 18:30:39 +08:00
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?.();
})
}
2024-03-19 19:19:07 +08:00
2024-03-27 18:56:48 +08:00
export function del<T>(req: Req<T>) {
2024-03-20 18:30:39 +08:00
req.onBefore?.();
2024-03-22 21:22:12 +08:00
Axios.delete<T>(req.url, req.config).then(res => {
2024-03-20 18:30:39 +08:00
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?.();
})
}