Files
report_viewer/public/desensitize.js
2026-06-10 12:55:01 +08:00

117 lines
3.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// 读取JSON文件
// 读取JSON文件
const inputFile = path.join(__dirname, 'public/DWBG9FB3.json');
const data = JSON.parse(fs.readFileSync(inputFile, 'utf8'));
// 姓名映射表(保持同一姓名脱敏后一致)
const nameMap = {
'何志勇': '何某某',
'覃圣有': '覃某',
'刘飞': '刘某某',
'陈波': '陈某某',
'覃小群': '覃某某',
'陈观海': '陈某某',
'刘国富': '刘某某'
};
// 脱敏函数
function desensitizeName(name) {
if (nameMap[name]) {
return nameMap[name];
}
// 对于未知的姓名,保留姓氏,名字用星号代替
if (name && name.length > 0) {
const surname = name[0];
return surname + '某某';
}
return name;
}
function desensitizeIdCard(idCard) {
if (!idCard || idCard.length !== 18) return idCard;
return idCard.substring(0, 6) + '********' + idCard.substring(14);
}
function desensitizeMobile(mobile) {
if (!mobile || mobile.length !== 11) return mobile;
return mobile.substring(0, 3) + '****' + mobile.substring(7);
}
function desensitizeText(text) {
if (!text || typeof text !== 'string') return text;
let result = text;
// 替换所有出现的人名
for (const [realName, maskedName] of Object.entries(nameMap)) {
// 替换姓名
const regex1 = new RegExp(realName, 'g');
result = result.replace(regex1, maskedName);
// 替换姓名+某的形式(如:何志某 -> 何某某某)
const regex2 = new RegExp(realName.substring(0, realName.length - 1) + '某', 'g');
result = result.replace(regex2, maskedName);
}
return result;
}
// 递归遍历对象进行脱敏
function desensitizeObject(obj) {
if (obj === null || typeof obj !== 'object') {
return obj;
}
if (Array.isArray(obj)) {
return obj.map(item => desensitizeObject(item));
}
const result = {};
for (const [key, value] of Object.entries(obj)) {
switch (key) {
case 'name':
result[key] = desensitizeName(value);
break;
case 'id_card':
result[key] = desensitizeIdCard(value);
break;
case 'mobile':
result[key] = desensitizeMobile(value);
break;
case 'c_mc':
// 当事人姓名
result[key] = desensitizeName(value);
break;
case 'c_gkws_dsr':
case 'c_gkws_pjjg':
// 判决书内容中的文本
result[key] = desensitizeText(value);
break;
default:
result[key] = desensitizeObject(value);
break;
}
}
return result;
}
// 执行脱敏
const desensitizedData = desensitizeObject(data);
// 保存脱敏后的文件
const outputFile = path.join(__dirname, 'public/DWBG9FB3_desensitized.json');
fs.writeFileSync(outputFile, JSON.stringify(desensitizedData, null, 2), 'utf8');
console.log('脱敏完成!');
console.log('原始文件:', inputFile);
console.log('脱敏后文件:', outputFile);
// 显示脱敏摘要
console.log('\n脱敏摘要');
console.log('- 姓名:已脱敏(保留姓氏)');
console.log('- 身份证号已脱敏保留前6位和后4位');
console.log('- 手机号已脱敏保留前3位和后4位');
console.log('- 判决书文本中的姓名:已批量替换');