BadwordController.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace app\modules\admin\controllers;
  3. use app\common\controllers\BController;
  4. use app\models\BadWord;
  5. use app\modules\admin\models\Config;
  6. use Yii;
  7. class BadwordController extends BController
  8. {
  9. public $layout = 'main';
  10. public function actionList()
  11. {
  12. $query = BadWord::find();
  13. if (Yii::$app->request->isAjax) {
  14. $data = [];
  15. $params = Yii::$app->request->get('BadWord',[]);
  16. $query = mergeParams($query,$params);
  17. $countQuery = clone $query;
  18. //分页
  19. if(isset($_GET['limit'])){ $query->limit(intval($_GET['limit']));}
  20. if(isset($_GET['offset'])){ $query->offset(intval($_GET['offset']));}
  21. //排序
  22. if(isset($_GET['sort'])&&isset($_GET['sortOrder']))
  23. {
  24. $resultList = $query->orderBy([$_GET['sort']=>($_GET['sortOrder']=='asc'?SORT_ASC:SORT_DESC)])->all();
  25. }
  26. else
  27. {
  28. $resultList = $query->orderBy(['id'=>SORT_ASC])->all();
  29. }
  30. foreach($resultList as $result)
  31. {
  32. $data[] = array('id'=>$result->id,'bad_word'=>$result->bad_word,'type'=>BadWord::typeOptions($result->type),'replace_word'=>$result->replace_word,'list_order'=>$result->list_order,'create_time'=>get_date($result->create_time,'Y-m-d H:i'));
  33. }
  34. $result = ["total"=>$countQuery->count(),"totalNotFiltered"=>$countQuery->count(),"rows"=>$data];
  35. echo_json($result);
  36. }
  37. $this->tableTitle = array(
  38. array('field'=>'id','checkbox'=>false,'formatter'=>'checkboxFormatter','class'=>'col-md-1'),
  39. array('field'=>'bad_word','title'=>BadWord::getAttributeName('bad_word'),'align'=>'center','class'=>'col-md-2'),
  40. array('field'=>'type','title'=>BadWord::getAttributeName('type'),'align'=>'center','class'=>'col-md-2'),
  41. array('field'=>'replace_word','title'=>BadWord::getAttributeName('replace_word'),'align'=>'center','class'=>'col-md-2'),
  42. array('field'=>'list_order','title'=>BadWord::getAttributeName('list_order'),'align'=>'center','sortable'=>true,'formatter'=>'editFormatter','class'=>'col-md-1'),
  43. array('field'=>'create_time','title'=>BadWord::getAttributeName('create_time'),'align'=>'center','sortable'=>true,'class'=>'col-md-2'),
  44. array('field'=>'operate','title'=>BadWord::getAttributeName('operate'),'align'=>'center','events'=>'window.operateEvents','formatter'=>'operateFormatter','class'=>'col-md-2'),
  45. );
  46. $this->tableConfig = array('table'=>BadWord::shortTableName(),'url'=>$this->createRealUrl(['admin/badword/list']),'setFieldUrl'=>$this->createRealUrl(['admin/badword/setfield']),'idField'=>BadWord::modelPrimaryKey(),'checkbox'=>1,'dropmenu'=>1,'pagination'=>true,'pagesize'=>20,'refresh'=>true);
  47. return $this->render('list',array('model'=>new BadWord()));
  48. }
  49. //添加
  50. function actionAdd()
  51. {
  52. $model = new BadWord();
  53. if(Yii::$app->request->isAjax&&$model->load(Yii::$app->request->post())){
  54. $exist = BadWord::find()->where("bad_word='".$model->bad_word."' and type=".$model->type)->exists();
  55. if($exist)
  56. {
  57. $msgdata = ['error' => 1,'msg' => '已存在该敏感词,添加失败'];
  58. echo_json($msgdata);
  59. }
  60. $model->create_time = TIMESTAMP;
  61. if(!$model->validate())
  62. {
  63. $msgdata = ['error' => 1,'msg' => $model->returnFirstError()];
  64. }
  65. else
  66. {
  67. if($model->save())
  68. {
  69. $msgdata = ['error' => 0,'msg' => '操作成功'];
  70. }
  71. else
  72. {
  73. $msgdata = ['error' => 1,'msg' => '操作失败'];
  74. }
  75. }
  76. echo_json($msgdata);
  77. }
  78. return $this->renderAjax('add',array('model'=>$model));
  79. }
  80. //批量删除
  81. public function actionMultidelete()
  82. {
  83. $ids = Yii::$app->request->get('ids');
  84. if(!empty($ids))
  85. {
  86. BadWord::deleteAll("id in(".$ids.")");
  87. $msgdata = ['error' => 0,'msg' => '操作成功'];
  88. }
  89. else
  90. {
  91. $msgdata = ['error' => 1,'msg' => '请选择操作记录'];
  92. }
  93. echo_json($msgdata);
  94. }
  95. //删除
  96. public function actionDel()
  97. {
  98. $id = $this->getKeyId();
  99. $model = BadWord::findOne($id);
  100. check_record_exists($model);
  101. if($model->delete())
  102. {
  103. $msgdata = ['error' => 0,'msg' => '操作成功!'];
  104. }
  105. else
  106. {
  107. $msgdata = ['error' => 1,'msg' => '操作失败!'];
  108. }
  109. echo_json($msgdata);
  110. }
  111. //配置
  112. function actionConfig()
  113. {
  114. $configModel = Config::find()->where("name='badword'")->limit(1)->one();
  115. check_record_exists($configModel);
  116. $config = string2array($configModel->value);
  117. $post = Yii::$app->request->post();
  118. if(Yii::$app->request->isAjax&&!empty($post)){
  119. $post['open_pc'] = $post['open_pc']?$post['open_pc']:0;
  120. $post['open_wap'] = $post['open_wap']?$post['open_wap']:0;
  121. $post['open_spider'] = $post['open_spider']?$post['open_spider']:0;
  122. $post['open'] = $post['open']?$post['open']:0;
  123. $configModel->value = array2string($post);
  124. if($configModel->save())
  125. {
  126. $msgdata = ['error' => 0,'msg' => '操作成功'];
  127. }
  128. else
  129. {
  130. $msgdata = ['error' => 1,'msg' => '操作失败'];
  131. }
  132. echo_json($msgdata);
  133. }
  134. return $this->renderAjax('config',array('config'=>$config));
  135. }
  136. }