wupengfei
2025-03-13 186234cb3833e620aa0ae46212fe337eb2d6e77d
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
import axios from 'axios';
 
export class SensitiveManage {
  static sensitiveFilePath = `https://parkmanagement.oss-cn-hangzhou.aliyuncs.com/12333/sensitive.txt?${new Date().getTime()}`;
 
  static sensitiveList = '';
 
  static async getSensitiveList() {
    try {
      if (!this.sensitiveList) {
        const res = await axios.get(this.sensitiveFilePath);
        this.sensitiveList = res.data;
      }
      return this.sensitiveList;
    } catch (error) {}
  }
 
  static filterSensitiveWord(data: any, needFilter = true) {
    if (needFilter && this.sensitiveList && data) {
      if (typeof data === 'string') {
        const regex = new RegExp(this.sensitiveList, 'gi');
        return data.replace(regex, (match) => match.replace(/./g, '*'));
      } else if (Array.isArray(data)) {
        return data.map((item) => this.filterSensitiveWord(item));
      } else if (typeof data === 'object') {
        const dataKeys = Object.keys(data);
        const newData: any = Object.create(Object.getPrototypeOf(data));
        dataKeys.forEach((value) => {
          newData[value] = this.filterSensitiveWord(data[value]);
        });
        return newData;
      }
    }
    return data;
  }
}
 
SensitiveManage.getSensitiveList();