-
Notifications
You must be signed in to change notification settings - Fork 12
/
Sensitive.php
157 lines (147 loc) · 4.35 KB
/
Sensitive.php
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
<?php
/**
* 敏感词管理
* Date=> 2018/7/23
* Time=> 11=>41
*/
namespace RongCloud\Lib\Sensitive;
use RongCloud\Lib\Request;
use RongCloud\Lib\Utils;
class Sensitive
{
/**
* 敏感词模块路径
*
* @var string
*/
private $jsonPath = 'Lib/Sensitive/';
/**
* 请求配置文件
*
* @var string
*/
private $conf = "";
/**
* 校验配置文件
*
* @var string
*/
private $verify = "";
/**
* Sensitive constructor.
*/
function __construct()
{
$this->conf = Utils::getJson($this->jsonPath.'api.json');
$this->verify = Utils::getJson($this->jsonPath.'verify.json');
}
/**
* 敏感词添加
*
* @param $Sensitive array 敏感词添加参数
* @param
* $Sensitive = [
'replace'=> '***',//敏感词替换,最长不超过 32 个字符, 敏感词屏蔽可以为空
'keyword'=>"abc",//敏感词
'type'=>0// 0: 敏感词替换 1: 敏感词屏蔽
];
* @return array
*/
public function add(array $Sensitive=[]){
$Sensitive['replace'] = isset($Sensitive['replace'])?$Sensitive['replace']:"";
$conf = $this->conf['add'];
$verify = $this->verify['rule'];
$error = (new Utils())->check([
'api'=> $conf,
'model'=> 'rule',
'data'=> $Sensitive,
'verify'=> $verify
]);
if($error) return $error;
$Sensitive = (new Utils())->rename($Sensitive, [
'keyword'=> 'word',
'replace'=> 'replaceWord'
]);
if(!isset($Sensitive['type'])){
$Sensitive['type'] = 1;
}
if($Sensitive['type'] == 1){
unset($Sensitive['type']);
unset($Sensitive['replaceWord']);
}
$result = (new Request())->Request($conf['url'],$Sensitive);
$result = (new Utils())->responseError($result, $conf['response']['fail']);
return $result;
}
/**
* 敏感词批量添加
*
* @param $Sensitive array 敏感词添加参数
* @param
* $Sensitive = [
* [
* 'word'=>'abc',//敏感词
* 'replaceWord'=>'***'//敏感词替换,最长不超过 32 个字符, 敏感词屏蔽可以为空
* ]
* ];
* @return array
*/
public function batchAdd(array $Sensitive = []) {
$conf = $this->conf['batchAdd'];
$verify = $this->verify['batchAdd'];
$error = (new Utils())->check([
'api' => $conf,
'model' => 'sensitive',
'data' => $Sensitive,
'verify' => $verify
]);
if ($error) return $error;
$result = (new Request())->Request($conf['url'], $Sensitive, 'json');
$result = (new Utils())->responseError($result, $conf['response']['fail']);
return $result;
}
/**
* 敏感词删除
*
* @param $Sensitive array 敏感词删除参数
* @param
* $Sensitive = [
'keywords'=>["bbb"]//删除敏感词列表
];
* @return array
*/
public function remove(array $Sensitive=[]){
$conf = $this->conf['remove'];
$verify = $this->verify['sensitive'];
$error = (new Utils())->check([
'api'=> $conf,
'model'=> 'sensitive',
'data'=> $Sensitive,
'verify'=> $verify
]);
if($error) return $error;
$Sensitive = (new Utils())->rename($Sensitive, [
'keywords'=> 'words',
]);
$result = (new Request())->Request($conf['url'],$Sensitive);
$result = (new Utils())->responseError($result, $conf['response']['fail']);
return $result;
}
/**
* 敏感词列表
*
* @param $Sensitive array 敏感词列表参数
* @param
* $Sensitive = [
'type'=> 1,//敏感词类型,0: 敏感词替换, 1: 敏感词屏蔽, 为空获取全部
];
* @return array
*/
public function getList(array $Sensitive=[]){
$Sensitive['type'] = isset($Sensitive['type'])?intval($Sensitive['type']):2;
$conf = $this->conf['getList'];
$result = (new Request())->Request($conf['url'],$Sensitive);
$result = (new Utils())->responseError($result, $conf['response']['fail']);
return $result;
}
}