SensitiveWord.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. /**
  3. * 敏感词管理
  4. *
  5. */
  6. namespace app\admin\controller;
  7. use think\Db;
  8. use think\Exception;
  9. use app\common\library\MakeReport;
  10. use think\File;
  11. class SensitiveWord extends Admin
  12. {
  13. protected $model = null;
  14. protected function _initialize()
  15. {
  16. parent::_initialize();
  17. $this->model = model('SensitiveWord');
  18. }
  19. public function index()
  20. {
  21. // 查询
  22. $where = [];
  23. // 规则名
  24. if (input('request.word') != '') {
  25. $where['word'] =['like', '%' . input('request.word') .'%'];
  26. }
  27. $infoList = $this->model
  28. ->where($where)
  29. ->order('create_time', 'desc')
  30. ->paginate(10, false, ['query' => input('get.')])
  31. ->each(function($item, $key){
  32. $item['creator'] = model('admin')->where('id',$item['creator'])->value('username');
  33. return $item;
  34. });
  35. $this->assign('list', $infoList);
  36. $this->assign('page', $infoList->render());
  37. return $this->fetch();
  38. }
  39. public function add()
  40. {
  41. if (request()->isPost()){
  42. $word = input('post.')['word'];
  43. $word = trim($word);
  44. // 字段验证
  45. $verify = $this->verify($word);
  46. if ( !$verify['code']) {
  47. $this->error( $verify['msg']);
  48. }
  49. $data = [
  50. 'word' => $word,
  51. 'creator' => session('ADMIN_ID'),
  52. 'create_time' => time(),
  53. ];
  54. $res = $this->model->insert($data);
  55. if ( $res) {
  56. $this->success('添加成功',url('index'));
  57. }else{
  58. $this->error('添加失败');
  59. }
  60. }
  61. return $this->fetch();
  62. }
  63. public function edit()
  64. {
  65. $id = $this->request->param('id',0,'intval');
  66. if (!$id){
  67. $this->error('ID不能为空');
  68. }
  69. if (request()->isPost()){
  70. $word = input('post.')['word'];
  71. $word = trim($word);
  72. // 字段验证
  73. $verify = $this->verify($word,$id);
  74. if ( !$verify['code']) {
  75. $this->error( $verify['msg']);
  76. }
  77. $data = [
  78. 'id' => $id,
  79. 'word' => $word,
  80. 'update_time' => time(),
  81. 'creator' => session('ADMIN_ID'),
  82. ];
  83. $res = $this->model->update($data);
  84. if ( $res !== false) {
  85. $this->success('编辑成功',url('index'));
  86. }else{
  87. $this->error('编辑失败');
  88. }
  89. }
  90. $info = $this->model->find($id);
  91. $this->assign('info',$info);
  92. return $this->fetch();
  93. }
  94. // 删除
  95. public function del()
  96. {
  97. $id = $this->request->param('id',0,'intval');
  98. if (!$id){
  99. $this->error('ID不能为空');
  100. }
  101. $res = $this->model->where('id',$id)->delete();
  102. if ($res) {
  103. $this->success('删除成功', url('index'));
  104. }
  105. $this->error('删除失败');
  106. }
  107. // 批量删除
  108. public function batchDel()
  109. {
  110. if (request()->isPost()){
  111. $ids = input('post.')['ids'];
  112. if (!$ids){
  113. $this->error('未选择数据');
  114. }
  115. $array = explode(',',$ids);
  116. $res = $this->model->where('id','in',$array)->delete();
  117. if ($res) {
  118. $this->success('批量删除成功', url('index'));
  119. }
  120. $this->error('批量删除失败');
  121. }
  122. $this->error('参数错误!');
  123. }
  124. public function import()
  125. {
  126. if (request()->isPost()){
  127. $file = request()->file('file');
  128. if($file){
  129. $data = [];
  130. $error = [];
  131. $info = $file->move(ROOT_PATH . 'public/upload/txt');
  132. if($info){
  133. $exfile=ROOT_PATH.'/public/upload/txt/'.date('Ymd').'/'.$info->getFilename();
  134. $filetxt = file_get_contents($exfile); //文件路径
  135. unset($info); //一定要unset之后才能进行删除操作,否则请求会被拒绝
  136. unlink($exfile); //删除上传失败文件
  137. $rep = str_replace("\r\n", ',', $filetxt);
  138. $cont = explode(',', $rep);
  139. foreach ($cont as $key => $value) {
  140. $value = iconv('GB2312', 'UTF-8', $value);
  141. $verify = $this->verify($value);
  142. if ( $verify['code']) {
  143. $data = [
  144. 'word' => $value,
  145. 'creator' => session('ADMIN_ID'),
  146. 'create_time' => time(),
  147. ];
  148. try {
  149. $res = $this->model->insert($data);
  150. } catch (\Exception $e) {
  151. $error[] = [
  152. 'msg' => $value . ':导入失败,原因:' . $e->getMessage() . '。'
  153. ];
  154. }
  155. }else{
  156. $error[] = [
  157. 'msg' => $value . ':导入失败,原因:' . $verify['msg'] . '。'
  158. ];
  159. }
  160. }
  161. unset($cont);
  162. if ( !empty($error)) {
  163. return json(['code'=>0,'msg'=>'部分导入成功','data' => $error]);
  164. }
  165. return json(['code'=>200,'msg'=>'导入成功']);
  166. }else{
  167. // 上传失败获取错误信息
  168. return json(['code'=>500,'msg'=>$file->getError()]);
  169. }
  170. }
  171. }
  172. return $this->fetch();
  173. }
  174. // 数据验证
  175. private function verify($word,$id = 0)
  176. {
  177. $result = ['code' => 1,'msg' => ''];
  178. if ( empty($word)) {
  179. $result = [
  180. 'code' => 0,
  181. 'msg' => '敏感词不能为空'
  182. ];
  183. return $result;
  184. }
  185. $str = (strlen($word) - mb_strlen($word,'UTF8'))/2;
  186. if ( strlen($word) - $str > 30) {
  187. $result = [
  188. 'code' => 0,
  189. 'msg' => '超出30个字符'
  190. ];
  191. return $result;
  192. }
  193. if ( $this->model->existsWord($word,$id)) {
  194. $result = [
  195. 'code' => 0,
  196. 'msg' => '已存在该敏感词'
  197. ];
  198. return $result;
  199. }
  200. return $result;
  201. }
  202. }