POST请求的接口
import Qs from 'qs';
const baseUrl == 'https://test.net.cn'
const token = '91c4f519d83d120f95013ce7586046cc2249415a'
// post 方式导出
export const exportDataFn = async (params = {}, url = '/customer/export') => {
if (!url) {
return;
}
const newSearchParams={ ...params, 'authen-token': token }
const form = document.createElement('form');
form.style.display = 'none';
form.action = `${baseUrl}${url}`;
form.method = 'POST';
form.target='_blank'
document.body.appendChild(form);
for (const [key, value] of Object.entries(newSearchParams)) {
if (Array.isArray(value)) {
// 如果参数值是数组,为每个数组元素创建对应的表单元素
value.forEach((item, index) => {
const input = document.createElement('input');
input.type = 'hidden';
input.name = `${key}[]`;
input.value = item;
form.appendChild(input);
});
} else {
// 如果参数值不是数组,创建单个表单元素
const input = document.createElement('input');
input.type = 'text';
input.name = key;
input.value = value;
form.appendChild(input);
}
}
form.submit();
form.remove();
}
GET请求的接口
import Qs from 'qs';
const baseUrl == 'https://test.net.cn'
const token = '91c4f519d83d120f95013ce7586046cc2249415a'
// get 方式导出
export const exportDataGetFn = async (params = {}, url = '/customer/export') => {
// if (searchParams['ctime[]']) {
// searchParams.ctime = searchParams['ctime[]']
// delete searchParams['ctime[]'];
// }
if (!url) {
return;
}
const export_url: any = `${baseUrl}${url}?` + Qs.stringify({ ...params, 'authen-token': token })
console.log('export:url:', export_url, params)
window.open(export_url, '_blank');
return;
}