1.去除对象空值
/**
* 去除对象空值
* @param obj
* @returns
*/
export const removeProperty = (obj: { [x: string]: any }) => {
Object.keys(obj).forEach((item) => {
if (obj[item] === '' || obj[item] === undefined || obj[item] === null || obj[item] === 'null')
delete obj[item];
});
return obj;
};
2.对象数组去重
/**
* @对象数组去重
*/
export const objectArrayDeduplication = (arr: any, uniId: string) => {
const res = new Map();
return arr.filter(
(item: { [x: string]: any }) => !res.has(item[uniId]) && res.set(item[uniId], 1),
);
};
3.相同name组成新数组
/**
* 相同name组成新数组
* @param arr
* @param title
* @returns
*/
export const treeBoth = (arr: string | any[], title: string | number) => {
const tempArr = [];
const afterData = [];
for (let k = 0; k < arr.length; k++) {
if (tempArr.indexOf(arr[k][title]) === -1) {
afterData.push({
[title]: arr[k][title],
content: [arr[k]],
});
tempArr.push(arr[k][title]);
} else {
for (let j = 0; j < afterData.length; j++) {
if (afterData[j][title] == arr[k][title]) {
afterData[j].content.push(arr[k]);
break;
}
}
}
}
return afterData;
};
4._js在树形对象数组结构中查找对象
/**
* @_js在树形对象数组结构中查找对象
*/
export const breadthQuery = (tree: any, name: string, id: string | number) => {
let stark: any[] = [];
stark = stark.concat(tree);
while (stark.length) {
const temp = stark.shift();
if (temp.children) {
stark = stark.concat(temp.children);
}
if (temp[name] === id) {
return temp;
}
}
};
5._缺省数组处理
export const handleCardData = (res: any) => {
const arr: any = [];
for (let i = 0; i < res.length; i += 10) {
const resArr = res.slice(i, i + 10);
if (resArr.length < 10) {
for (let index = 0; resArr.length < 10; index += 1) {
resArr.push('');
}
}
arr.push(resArr);
}
return arr;
};
6.文件流下载
/**
* 文件流下载
* const blob = new Blob([res.data], { type: 'application/vnd.ms-excel;' });
* const blobUrl = window.URL.createObjectURL(blob);
* downLoad(blobUrl, `${obj.hospital_name}.xlsx`)
*/
export const downLoad = (blobUrl: any, xml: any) => {
const Link = document.createElement('a');
Link.download = xml;
Link.href = blobUrl;
document.body.appendChild(Link);
Link.click();
document.body.removeChild(Link);
};
7.获取url参数
export const getQueryVariable = (variable: string) => {
const query = window.location.search.substring(1);
const vars = query.split('&');
for (let i = 0; i < vars.length; i++) {
const pair = vars[i].split('=');
if (pair[0] == variable) {
return decodeURIComponent(pair[1]);
}
}
return false;
};
8.对象转url参数
export const splitQueryParams = (params: any) => {
const tempParams: string[] = [];
if (Object.keys(params).length <= 0) return tempParams;
for (let key in params) {
tempParams.push(`${key}=${params[key]}`);
}
return tempParams.join('&');
};
9.获取url参数
/** 获取url参数 */
export const urlToToObj = (url) => {
const obj = {};
if (url) {
const params = url.split('?')[1].split('&');
params.map((item) => (obj[item.split('=')[0]] = decodeURIComponent(item.split('=')[1])));
}
return obj;
};
10.文件下载
export const downloadInstitutionSingleFile = async (File: any,URL: string) => {
const DOWNLOAD_ALL_FILE: any = await request.post(URL, {
data: File,
responseType: 'blob',
getResponse: true,
});
const hide = message.loading('下载中...', 0);
try {
if (DOWNLOAD_ALL_FILE) {
setTimeout(hide, 500);
if (DOWNLOAD_ALL_FILE.response.headers.get('content-disposition')) {
const CD = DOWNLOAD_ALL_FILE.response.headers.get('content-disposition').split(';');
let CD_TEMP: string = '';
for (let i = 0; i < CD.length; i += 1) {
if (!CD[i].indexOf('filename=')) CD_TEMP = CD[i];
}
const FILE_EXTENSION = CD_TEMP.split('.')[CD_TEMP.split('.').length - 1];
const FILE_NAME = CD_TEMP.split('filename=')[CD_TEMP.split('filename=').length - 1];
let fileName = '未识别的文件名';
if (FILE_NAME) {
fileName = `${FILE_NAME}`;
} else {
fileName = `未识别的文件.${FILE_EXTENSION}`;
}
convertRes2Blob(DOWNLOAD_ALL_FILE.data, fileName);
message.success('下载成功');
} else {
request.post(URL, { params: File }).then((res: any) => {
message.error(res.msg,1);
});
}
} else {
message.error('下载失败');
}
} catch (error) {
console.log(error);
}
};
export const convertRes2Blob = (data: any, filename = '下载内容') => {
const blob = new Blob([data], { type: 'application/octet-stream' });
if (typeof window.navigator.msSaveBlob !== 'undefined') {
window.navigator.msSaveBlob(blob, decodeURI(filename));
} else {
const blobURL = window.URL.createObjectURL(blob);
const tempLink = document.createElement('a');
tempLink.style.display = 'none';
tempLink.href = blobURL;
tempLink.setAttribute('download', decodeURI(filename));
if (typeof tempLink.download === 'undefined') {
tempLink.setAttribute('target', '_blank');
}
document.body.appendChild(tempLink);
tempLink.click();
document.body.removeChild(tempLink);
window.URL.revokeObjectURL(blobURL);
}
};