1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
| <template> <div @click="importTxtDataToJSON">测试txt导入数据</div> </template>
<script setup lang="ts"> import {onMounted} from "vue";
type TFileData=Promise<[string,{label:string,value:string}[]]>
const filePathList=[ { path:"1", name:"监管方式" }, { path:"2", name:"国家" }, { path:"3", name:"指运港" }, { path:"4", name:"境内货源地" }, { path:"5", name:"运输方式" }, { path:"6", name:"商品_征免" }, {name:'征免性质', path:"7"}, {name:'货币', path:"8"}, {name:'成交方式', path:"9"}, {name:'包装种类', path:"10"}, {name:'单位', path:"11"}, {name:'运费保费杂费种类', path:"12"}, {name:'随附单证', path:"13"}, {name:'随附单据类型', path:"14"}, {name:'集装箱', path:"15"}, {name:'产地', path:"16"}, {name:'报关单类型', path:"17"}, {name:'危险品_危包类别', path:"18"}, {name:'离境口岸', path:"19"}, {name:'出境关别', path:"20"}, {name:'老国家代码', path:"21"}, {name:'进口商检_海关机关代码', path:"22"}, {name:'进口商检_企业资质类别', path:"23"}, {name:'进口商检_关联理由', path:"24"}, {name:'进口商检_表体_用途', path:"25"}, {name:'进口商检_表体_许可证类别', path:"26"}, {name:'核注清单_清单类型', path:"27"}, {name:'核注清单_料件成品标志', path:"28"}, {name:'老币制代码', path:"29"}, {name:'核注清单_报关标志', path:"30"}, {name:'核注清单_是否系统生成报关单', path:"31"}, {name:'核注清单_报关类型', path:"32"}, {name:'核注清单_报关单类型', path:"33"}, {name:'优惠贸易协定', path:"34"}, {name:'指运港_不带国内', path:"103"}, {name:'通用_是否', path:"50"}, {name:'出口商检_表体_许可证类别', path:"51"}, ]
onMounted(() => { !localStorage.getItem('testData')&&importTxtDataToJSON() })
// 存储文件数据 const importTxtDataToJSON=()=>{ const initTime=new Date().getTime() const promise:TFileData[]=[] filePathList.forEach((item)=>{ promise.push(importTxtFile(`/txt/${item.path}.txt`,item.name)) }) Promise.all(promise).then((value)=>{ const str=JSON.stringify(Object.fromEntries(value)) const curTime=new Date().getTime() console.log("数据",Object.fromEntries(value)) console.log("加载时间",curTime-initTime) console.log("数据大小",getJsonLength(str)) localStorage.setItem("testData",str) }) }
// 导入文件 function importTxtFile(filePath:string,name:string,charsetType:string="GBK"):TFileData { return new Promise((resolve, reject) => { const xhr = new XMLHttpRequest(); xhr.open('GET', filePath, false); xhr.overrideMimeType(`text/plain; charset=${charsetType}`); xhr.send();
if (xhr.readyState === 4 && xhr.status === 200) { const content = xhr.responseText; const lines = content.split('\n'); const jsonData = [];
for (let line of lines) { if (line.trim() === '') { continue; }
const [key, value] = line.split(':').map(item => item.trim()); jsonData.push({label:key, value}); }
resolve([name,jsonData]); } else { reject(); } }) }
// 判断json串的大小 function getJsonLength(jsonString:string){ // 将 JSON 字符串转换为 JavaScript 对象 const jsonObject = JSON.parse(jsonString);
// 将 JavaScript 对象重新转换为 JSON 字符串 const jsonStringified = JSON.stringify(jsonObject);
// 将 JSON 字符串编码为 Uint8Array 类型的数据 const encoder = new TextEncoder(); const encodedData = encoder.encode(jsonStringified);
// 获取 JSON 字符串的内存大小 const memorySize = encodedData.byteLength;
return memorySize // 输出 JSON 字符串的内存大小(字节数) } </script>
|