You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
164 lines
4.6 KiB
164 lines
4.6 KiB
import { message } from "antd";
|
|
|
|
/**
|
|
* GET 请求
|
|
* @param {string} url - 请求的 URL 地址
|
|
* @param {object} params - 请求参数对象
|
|
* @returns {Promise} - 返回一个 Promise 对象,包含 response 数据或 error 信息
|
|
*/
|
|
export function getRequest(url: string, params: object) {
|
|
// 将参数对象转换为查询字符串
|
|
const queryString = Object.keys(params)
|
|
.map(key => encodeURIComponent(key) + '=' + encodeURIComponent(params[key]))
|
|
.join('&');
|
|
|
|
// 拼接查询字符串到 URL
|
|
let urlWithParams = url;
|
|
if (queryString) {
|
|
urlWithParams += `?${queryString}`;
|
|
}
|
|
|
|
return fetch(urlWithParams, {
|
|
method: 'GET',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
})
|
|
.then(response => {
|
|
if (!response.ok) { throw new Error('请求失败'); }
|
|
return response.json();
|
|
})
|
|
.catch(error => console.error(error));
|
|
}
|
|
|
|
|
|
/**
|
|
* POST 请求
|
|
* @param {string} url - 请求的 URL 地址
|
|
* @param {object} data - 请求数据
|
|
* @returns {Promise} - 返回一个 Promise 对象,包含 response 数据或 error 信息
|
|
*/
|
|
export function postRequest(url: string, data: object) {
|
|
return fetch(url, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify(data),
|
|
})
|
|
.then(response => {
|
|
message.info('response',url,response)
|
|
if (!response.ok) { throw new Error('请求失败'); }
|
|
return response.json();
|
|
})
|
|
.catch(error => {console.error(error), message.info('responerrorse',url,error)});
|
|
}
|
|
|
|
|
|
/**
|
|
* POST表单请求
|
|
* @param {string} url - 请求的 URL 地址
|
|
* @param {object} data - 请求数据
|
|
* @returns {Promise} - 返回一个 Promise 对象,包含 response 数据或 error 信息
|
|
*/
|
|
export function postFormDataRequest(url: string, data: object) {
|
|
const formData = new FormData();
|
|
for (const key in data) {
|
|
formData.append(key, data[key]);
|
|
}
|
|
|
|
return fetch(url, {
|
|
method: 'POST',
|
|
body: formData
|
|
})
|
|
.then(response => {
|
|
if (!response.ok) { throw new Error('请求失败'); }
|
|
return response.json();
|
|
})
|
|
.catch(error => console.error(error));
|
|
}
|
|
|
|
/**
|
|
* Delete 请求
|
|
* @param {string} url - 请求的 URL 地址
|
|
* @param {object} params - 参数对象
|
|
* @returns {Promise} - 返回一个 Promise 对象,表示请求的结果(成功或失败)
|
|
*/
|
|
export function deleteRequest(url: string, params: object) {
|
|
const searchParams = new URLSearchParams(params);
|
|
return fetch(`${url}?${searchParams.toString()}`, {
|
|
method: 'DELETE',
|
|
})
|
|
.then(response => {
|
|
if (!response.ok) { throw new Error('请求失败'); }
|
|
return response.json();
|
|
})
|
|
.catch(error => console.error(error));
|
|
}
|
|
|
|
|
|
/**
|
|
* 发送 PUT 网络请求
|
|
* @param {string} url - 请求的 URL 地址
|
|
* @param {object} data - 请求的数据
|
|
* @returns {Promise} - 返回一个 Promise 对象,表示请求的结果(成功或失败)
|
|
*/
|
|
export function putRequest(url: string, data: object) {
|
|
return fetch(url, {
|
|
method: 'PUT',
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify(data)
|
|
})
|
|
.then(response => {
|
|
if (!response.ok) { throw new Error('请求失败'); }
|
|
return response.json();
|
|
})
|
|
.catch(error => console.error(error));
|
|
}
|
|
|
|
|
|
/**
|
|
* 下载文件请求
|
|
* @param {string} url - 请求的 URL 地址
|
|
* @param {string} fileName - 文件的名称
|
|
* @returns {Promise} - 返回一个 Promise 对象,包含文件 blob 数据或 error 信息
|
|
*/
|
|
export function downloadFile(url: string, fileName?: string) {
|
|
return fetch(url, {
|
|
method: 'GET',
|
|
})
|
|
.then(response => {
|
|
if (!response.ok) { throw new Error('下载失败'); }
|
|
response.blob()
|
|
})
|
|
.then(blob => {
|
|
const url = window.URL.createObjectURL(new Blob([blob]));
|
|
const link = document.createElement('a');
|
|
link.href = url;
|
|
link.setAttribute('download', (fileName || '文件名称')); // 设置下载文件的名称
|
|
document.body.appendChild(link);
|
|
link.click();
|
|
document.body.removeChild(link);
|
|
})
|
|
.catch(error => console.error(error));
|
|
}
|
|
|
|
/**
|
|
* 上传文件的网络请求
|
|
* @param {string} url - 上传的 URL 地址
|
|
* @param {File} formData - 包含上传文件的 FormData 对象
|
|
* @returns {Promise} - 返回一个 Promise 对象,表示请求的结果(成功或失败)
|
|
*/
|
|
export function uploadFile(url: string, formData: any) {
|
|
return fetch(url, {
|
|
method: 'POST',
|
|
body: formData,
|
|
})
|
|
.then(response => {
|
|
if (!response.ok) { throw new Error('上传失败'); }
|
|
return response.json();
|
|
})
|
|
.catch(error => console.error(error));
|
|
} |