SSdk.ts
6.67 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import word from "./word"
// const word: Array<string> = [];
class SSdk{
version:string = "1.0.0";
/**@description SDK 名称 */
name:string = "cocos-creator 敏感词屏蔽SDK";
/**@description 是否开启敏感词检测 */
_open_check:boolean = true;
/**@description 应用的ID */
_app_id:string = "test";
/**@description 应用的密钥 */
_app_secret:string = "test";
/**@description 敏感词词库 */
_s_content:Array<string> = [];
/**@description 是否开启log */
_console_log:boolean = true;
/**@description 数据校验的md5 */
_s_md5:string = "";
/**@description 下载文件失败的重试次数 */
max_retry_count: number = 3;
/**@description 缓存已经验证过的文本 */
_had_ver:any = {};
_had_find: boolean = false;
constructor(){
}
/**@description 设置md5 */
public set_s_md5 (_s_md5) {
this._s_md5 = _s_md5;
}
/**@description 设置应用的Secret */
public set_app_secret(_app_secret) {
this._app_secret = _app_secret;
}
/**@description 设置应用ID */
public set_app_id(_app_id) {
this._app_id = _app_id;
}
/**@description 开启log */
public close_log() {
this._console_log = false;
}
/**@description 打印日志 */
public _log(content) {
if (this._console_log) {
console.log(content);
}
}
/**@description 开启敏感词检测 true 开启检车 false 关闭检测 */
public set_open_check(_open_check) {
this._open_check = _open_check;
}
public get(url, body, success, fail) {
var xhr = new XMLHttpRequest();
var self = this;
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400)) {
var response = xhr.responseText;
success && success(response);
}else{
fail && fail(xhr);
}
};
xhr.open("GET", url, true);
xhr.responseType = "text";
xhr.send(body);
}
public load_asset_with_retry(res_path, cb_success?:Function, cb_error?:Function) {
// console.log("res_path",res_path);
let retry_count = 0;
const load_func = () => {
cc.loader.load(res_path, (err, load_data) => {
if (!err) {
cb_success && cb_success(load_data);
} else {
retry_count++;
if (retry_count < this.max_retry_count) {
load_func();
} else {
cb_error && cb_error(err);
cc.warn("加载资源文件失败", res_path);
}
}
});
};
load_func();
}
public post(url: string, body: string, success?: Function, fail?: Function) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400)) {
var response = xhr.responseText;
success && success(response);
}else{
fail && fail(xhr);
}
};
xhr.open("POST", url, true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.send(body);
}
/**@description SDK 初始化 传递当前SDK是否开启 请求当下的敏感词库 */
public init() {
this._s_content = word;
this._log("敏感词版本号" + this.version + "敏感词SDK名称 = " + this.name);
if (this._open_check) {
var data = {}
data["app_id"] = this._app_id;
data["app_secret"] = this._app_secret;
var self = this;
this.post("https://maple.icesimba.cn/maple/notoken/sensitive/word/download", JSON.stringify(data), (res) => {
var download_data = JSON.parse(res);
self.set_s_md5(download_data.obj.md5);
self.load_asset_with_retry(download_data.obj.url, function(tres) {
var sumtdata = "[\"" + tres.replace(/\r\n/g, "\",\"") + "\"]";
var tdata = sumtdata.replace("\"石国 \"", "石国 ");
// 将敏感词库换成JSON数组
self._s_content = JSON.parse(tdata);
console.log("下载词库成功");
}, function(res) {
// console.log("下载失败的字体", res);
})
}, function(res) {
// console.log("请求失败", res)
});
} else {
this._log("敏感词库初始化失败 需要开启敏感词库");
}
}
/**@description 检测敏感词 */
public check(content: string, replace: string) {
if (!this._open_check) {
return content;
}
var replace_char = "*";
if (replace) {
replace_char = replace;
}
if (!content) {
this._log("检测内容报错");
return "";
}
// 判断是否已经缓存过
if (this._had_ver[content]) {
return this._had_ver[content];
}
var _checked_str = this.replace_str(content, replace_char)
this._had_ver[content] = _checked_str
return _checked_str;
}
public had_ss_word(content: string, replace_char: string){
this._had_find = false;
var _checked_str = this.replace_str(content, replace_char);
return this._had_find;
}
public replace_str(str, replace_char) {
var len = str.length;
// 长度从len -> 1;
for (var i = len; i > 0; --i) {
// 起点从0 -> len-i;
for (var j = 0; j <= len - i; ++j) {
var substr = str.substr(j, i);
var t_data = this._s_content.indexOf(substr);
if (t_data != -1) {
var prev = str.substr(0, j);
var curr = replace_char.repeat(i);
var next = str.substr(i + j, len - i - j);
this._had_find = true;
return this.replace_str(prev, replace_char) + curr + this.replace_str(next, replace_char);
}
}
}
return str;
}
}
var ssdk = new SSdk();
ssdk.set_open_check(true);
ssdk.init();
export default ssdk;