151 lines
4.3 KiB
TypeScript
151 lines
4.3 KiB
TypeScript
import axios, {AxiosRequestConfig, AxiosResponse} from "axios";
|
||
import type {MessageInstance} from "antd/es/message/interface";
|
||
|
||
export const Axios = axios;
|
||
|
||
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
|
||
// export const DevUserId: string = 'c2438eb8-2685-49a9-bf02-5111a5192d96'; // 18647109157
|
||
// export const DevUserId: string = '';
|
||
|
||
type Req<T> = {
|
||
messageApi: MessageInstance;
|
||
url: string;
|
||
body?: any;
|
||
config?: AxiosRequestConfig;
|
||
onBefore?(): void;
|
||
onSuccess(data: AxiosResponse<T>): void;
|
||
onFinally?(): void;
|
||
}
|
||
|
||
export interface IDictionary {
|
||
dataId: string;
|
||
dataParentId: string;
|
||
dataName: string;
|
||
dataCode: string;
|
||
}
|
||
|
||
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 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}`;
|
||
}
|
||
|
||
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 async function listDictionary(parentId: string, messageApi: MessageInstance) {
|
||
return new Promise<IDictionary[]>(resolve => {
|
||
get<IDictionary[]>({
|
||
messageApi,
|
||
url: `/api/data/listbyparentid/${parentId}`,
|
||
onSuccess({data}) {
|
||
resolve(data);
|
||
}
|
||
})
|
||
})
|
||
}
|
||
|
||
export function get<T>(req: Req<T>) {
|
||
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<T>) {
|
||
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<T>) {
|
||
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<T>) {
|
||
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?.();
|
||
})
|
||
} |