iot-ui-app/common/js/util/desensitization.js

39 lines
1.5 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.

// 手机号脱敏
export function phoneHide(phone) {
let reg = /^(1[3-9][0-9])\d{4}(\d{4}$)/; // 定义手机号正则表达式
phone = phone.replace(reg, '$1****$2');
return phone; // 185****6696
}
// 邮箱脱敏
export function emailHide(email) {
var avg;
var splitted;
var email1;
var email2;
splitted = email.split('@');
email1 = splitted[0];
avg = email1.length / 2;
email1 = email1.substring(0, email1.length - avg);
email2 = splitted[1];
return email1 + '***@' + email2; // 输出为81226***@qq.com
}
// 身份证脱敏
export function cardHide(card) {
const reg = /^(.{6})(?:\d+)(.{4})$/; // 匹配身份证号前6位和后4位的正则表达式
const maskedIdCard = card.replace(reg, '$1******$2'); // 身份证号脱敏将中间8位替换为“*”
return maskedIdCard; // 输出371782******5896
}
// 姓名脱敏
export function nameHide(name) {
if (name.length == 2) {
name = name.substring(0, 1) + '*'; // 截取name 字符串截取第一个字符,
return name; // 张三显示为张*
} else if (name.length == 3) {
name = name.substring(0, 1) + '*' + name.substring(2, 3); // 截取第一个和第三个字符
return name; // 李思思显示为李*思
} else if (name.length > 3) {
name = name.substring(0, 1) + '*' + '*' + name.substring(3, name.length); // 截取第一个和大于第4个字符
return name; // 王五哈哈显示为王**哈
}
}