export = () => {
const data = {
trade: {
filterVal: [],
list: [],
},
};
const nodes = [
{
entityName: '真实身份',
entityNum: '010100',
id: '010100_0010111_142702199711111111',
label: '142702199711111111\n李晓慧\n汉族',
pageJumpParam: {
encryptParam: 'c6f995bed195c881',
entityType: '0010111',
type: 'person',
notEncryptParam: '142702199711111111',
},
showEntityProperties: [
{ en: 'name', cn: '姓名', value: '李晓' },
{ en: 'nation', cn: '民族', value: '汉族' },
{ en: 'entity_value', cn: '证件号码', value: '142702199711111111' },
],
},
{
entityName: '虚拟身份',
entityNum: '010000',
id: '010000_0010100_142702199711111111',
label: '142702199711111111\n李晓\n汉族',
pageJumpParam: {
encryptParam: 'c6f995bed195c881',
entityType: '0010111',
type: 'person',
notEncryptParam: '142702199711111111',
},
showEntityProperties: [
{ en: 'name', cn: '姓名', value: '李晓' },
{ en: 'nation', cn: '民族', value: '汉族' },
{ en: 'entity_value', cn: '证件号码', value: '142702199711111111' },
],
},
];
data.trade.list = props.data.nodes.map((item) => {
return {
...item,
label: item.label?.split('\n').join('-'),
};
});
// 注:csv文件:","逗号换列,\n换行,\t防止excel将长数字变科学计算法等样式
const mainLists = data.trade;
const mainTitleForKey = mainLists.filterVal; //一级过滤
const mainList = mainLists.list; //一级数据
for (const th in mainList[0]) {
mainTitleForKey.push(th);
}
const mainStr = [];
mainStr.push(mainTitleForKey.join('\t,') + '\n'); //标题添加上换列转成字符串并存进数组
for (let i = 0; i < mainList.length; i++) {
const temp = [];
for (let j = 0; j < mainTitleForKey.length; j++) {
//根据过滤器拿出对应的值
temp.push(
typeof mainList[i][mainTitleForKey[j]] === 'undefined'
? mainList[i][mainTitleForKey[j]]
: JSON.stringify(mainList[i][mainTitleForKey[j]]).split(',').join(','),
);
}
mainStr.push(temp.join('\t,') + '\n'); //取出来的值加上逗号换列转字符串存数组
}
const merged = mainStr.join('');
//## 导出操作
// encodeURIComponent解决中文乱码
const uri = 'data:text/csv;charset=utf-8,\ufeff' + encodeURIComponent(merged);
// 通过创建a标签实现
const link = document.createElement('a');
link.href = uri;
// 对下载的文件命名
link.download = new Date().toISOString().substring(0, 10) + '-' + type + '.csv';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}