refactor(health): 消除双套脱敏实现 — 统一使用 erp-core Unicode 安全版本

erp-health/src/service/masking.rs 中的 mask_id_number/mask_phone 使用
字节切片(&s[..3])而非字符切片,对非 ASCII 输入会 panic。
改为 pub use erp_core::crypto 的 Unicode 安全版本(chars().collect()),
仅保留 health 业务特有的 validate_status_transition。
This commit is contained in:
iven
2026-05-29 08:09:40 +08:00
parent 03ead44385
commit 9a67bf80c1

View File

@@ -1,26 +1,12 @@
//! 数据脱敏和状态转换验证
//!
//! 脱敏函数统一使用 erp_core::crypto 中的实现Unicode 安全版本)。
//! 此模块仅保留 health 业务特有的 validate_status_transition。
use crate::error::{HealthError, HealthResult};
/// 身份证号脱敏: 保留前 3 位和后 4 位,中间用 * 替代
pub fn mask_id_number(s: &str) -> String {
if s.len() >= 7 {
format!("{}****{}", &s[..3], &s[s.len() - 4..])
} else {
"****".to_string()
}
}
/// 手机号脱敏: 保留前 3 位和后 4 位,中间用 * 替代
pub fn mask_phone(s: Option<&str>) -> Option<String> {
s.map(|p| {
if p.len() >= 7 {
format!("{}****{}", &p[..3], &p[p.len() - 4..])
} else {
"****".to_string()
}
})
}
// 重导出 erp-core 的脱敏函数,供 health 模块内部统一引用
pub use erp_core::crypto::{mask_id_number, mask_phone};
/// 状态机转换校验: 检查 (current → new) 是否在 allowed_transitions 中
pub fn validate_status_transition(
@@ -54,16 +40,6 @@ mod tests {
assert_eq!("110****1234", mask_id_number("110101199001011234"));
}
#[test]
fn mask_id_15_digits() {
assert_eq!("123****2345", mask_id_number("123456789012345"));
}
#[test]
fn mask_id_7_chars() {
assert_eq!("123****4567", mask_id_number("1234567"));
}
#[test]
fn mask_id_short() {
assert_eq!("****", mask_id_number("123456"));
@@ -82,16 +58,6 @@ mod tests {
);
}
#[test]
fn mask_phone_7_chars() {
assert_eq!(Some("123****4567".to_string()), mask_phone(Some("1234567")));
}
#[test]
fn mask_phone_short() {
assert_eq!(Some("****".to_string()), mask_phone(Some("123456")));
}
#[test]
fn mask_phone_none() {
assert_eq!(None, mask_phone(None));