Commit 3ecaa715 by 郭有超

fix bug

0 parents
No preview for this file type
## 敏感词转换SDK (文本太大 暂时处理会很慢)
###### cocos-creator 版本(默认敏感词库是关闭的) ######
使用步骤:
第一步: 导入sdk
1): 将sdk目录下 ssdk.cocos-creator.js 放到自己的目录 并使用 require
例子: window.ssdk = require("ssdk.cocos-creator.js");
第二步: 开启敏感词库 并初始化(开启要在初始化之前)
2): 开启敏感词检测. 例: ssdk.set_open_check(true)
3): 设置应用的app_id和app_secret 例: ssdk.set_appid("应用的ID") ssdk.set_app_seret("应用的appsecret")
4): 初始化SDK. 例: ssdk.init()
第三步: 使用check来检测并返回check后的字符串
5): check(content = "需要检测的字符串" , replace = "替换成的字符串")
注意: 没有开启敏感词库的话 通过check方法将返回原本的content内容 可以安装 uglifyjs 下来直接进行修改打包
__________________________________________________________________
\ No newline at end of file
No preview for this file type
var SSdk = (function() {
/**@description SDK 版本 */
this.version = "1.0.0";
/**@description SDK 名称 */
this.name = "cocos-creator 敏感词屏蔽SDK";
/**@description 是否开启敏感词检测 */
this._open_check = false;
/**@description 应用的ID */
this._app_id = "test";
/**@description 应用的密钥 */
this._app_secret = "test";
/**@description 敏感词词库 */
this._s_content = [];
/**@description 是否开启log */
this._console_log = true;
/**@description 数据校验的md5 */
this._s_md5 = "";
/**@description 缓存已经验证过的文本 */
this._had_ver = {};
/**@description 设置md5 */
this.set_s_md5 = function(_s_md5) {
this._s_md5 = _s_md5;
}
/**@description 设置应用的Secret */
this.set_app_secret = function(_app_secret) {
this._app_secret = _app_secret;
}
/**@description 设置应用ID */
this.set_app_id = function(_app_id) {
this._app_id = _app_id;
}
/**@description 开启log */
this.close_log = function() {
this._console_log = false;
}
/**@description 打印日志 */
this._log = function(content) {
if (this._console_log) {
console.log(content);
}
}
/**@description 开启敏感词检测 true 开启检车 false 关闭检测 */
this.set_open_check = function(_open_check) {
this._open_check = _open_check;
}
this.get = function(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);
}
this.post = function(url, body, success, fail) {
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是否开启 请求当下的敏感词库 */
this.init = function() {
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-test.icesimba.cn/maple/notoken/sensitive/word/download", JSON.stringify(data), function(res) {
var download_data = JSON.parse(res);
self.set_s_md5(download_data.obj.md5);
this.get(download_data.obj.url, "", function(tres) {
var sumtdata = "[\"" + tres.replace(/\r\n/g, "\",\"") + "\"]";
var tdata = sumtdata.replace("\"石国 \"", "石国 ");
// 将敏感词库换成JSON数组
self._s_content = JSON.parse(tdata);
}, function(res) {
console.log("下载失败的字体", res);
})
}.bind(this), function(res) { console.log("请求失败", res) });
} else {
this._log("敏感词库初始化失败 需要开启敏感词库");
}
}
/**@description 检测敏感词 */
this.check = function(content, replace) {
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;
}
this.replace_str = function(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);
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();
module.exports = ssdk;
\ No newline at end of file
This diff could not be displayed because it is too large.
.PHONY: cocos-creator
cocos-creator:
uglifyjs cocos-creator-ssdk/word.js cocos-creator-ssdk/SSdk.js -c -m -o sdk/ssdk.cocos-creator.js
\ No newline at end of file
No preview for this file type
This diff could not be displayed because it is too large.
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!