IpbannedController.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace app\modules\admin\controllers;
  3. use app\common\controllers\BController;
  4. use app\models\IpBanned;
  5. use Yii;
  6. class IpbannedController extends BController
  7. {
  8. public $layout = 'main';
  9. public function actionList()
  10. {
  11. $query = IpBanned::find();
  12. if (Yii::$app->request->isAjax) {
  13. $data = [];
  14. $params =[];
  15. $query = mergeParams($query,$params);
  16. $countQuery = clone $query;
  17. //分页
  18. if(isset($_GET['limit'])){ $query->limit(intval($_GET['limit']));}
  19. if(isset($_GET['offset'])){ $query->offset(intval($_GET['offset']));}
  20. //排序
  21. if(isset($_GET['sort'])&&isset($_GET['sortOrder']))
  22. {
  23. $resultList = $query->orderBy([$_GET['sort']=>($_GET['sortOrder']=='asc'?SORT_ASC:SORT_DESC)])->all();
  24. }
  25. else
  26. {
  27. $resultList = $query->orderBy(['ip_banned_id'=>SORT_ASC])->all();
  28. }
  29. foreach($resultList as $result)
  30. {
  31. $data[] = array('ip_banned_id'=>$result->ip_banned_id,'ip'=>long2ip($result->ip),'expires'=>get_date($result->expires,'Y-m-d'));
  32. }
  33. $result = ["total"=>$countQuery->count(),"totalNotFiltered"=>$countQuery->count(),"rows"=>$data];
  34. echo_json($result);
  35. }
  36. $this->tableTitle = array(
  37. array('field'=>'ip_banned_id','checkbox'=>false,'formatter'=>'checkboxFormatter','class'=>'col-md-2'),
  38. array('field'=>'ip','title'=>IpBanned::getAttributeName('ip'),'align'=>'center','class'=>'col-md-4'),
  39. array('field'=>'expires','title'=>IpBanned::getAttributeName('expires'),'align'=>'center','class'=>'col-md-4','sortable'=>true),
  40. array('field'=>'operate','title'=>IpBanned::getAttributeName('operate'),'align'=>'center','events'=>'window.operateEvents','formatter'=>'operateFormatter','class'=>'col-md-2'),
  41. );
  42. $this->tableConfig = array('table'=>IpBanned::shortTableName(),'url'=>$this->createRealUrl(['admin/ipbanned/list']),'setFieldUrl'=>$this->createRealUrl(['admin/ipbanned/setfield']),'idField'=>IpBanned::modelPrimaryKey(),'checkbox'=>1,'dropmenu'=>1,'pagination'=>true,'pagesize'=>20,'refresh'=>true);
  43. return $this->render('list',array('model'=>new IpBanned()));
  44. }
  45. //添加
  46. function actionAdd()
  47. {
  48. $model = new IpBanned();
  49. if(Yii::$app->request->isAjax&&$model->load(Yii::$app->request->post())){
  50. $exist = IpBanned::find()->where("ip='".ip2long($model->ip)."'")->exists();
  51. if($exist)
  52. {
  53. $msgdata = ['error' => 1,'msg' => '已存在该IP,添加失败'];
  54. echo_json($msgdata);
  55. }
  56. $model->ip = ip2long($model->ip);
  57. $model->expires = str_to_time(strval($model->expires));
  58. if(!$model->validate())
  59. {
  60. $msgdata = ['error' => 1,'msg' => $model->returnFirstError()];
  61. }
  62. else
  63. {
  64. if($model->save())
  65. {
  66. $msgdata = ['error' => 0,'msg' => '操作成功'];
  67. }
  68. else
  69. {
  70. $msgdata = ['error' => 1,'msg' => '操作失败'];
  71. }
  72. }
  73. echo_json($msgdata);
  74. }
  75. return $this->renderAjax('add',array('model'=>$model));
  76. }
  77. //批量删除
  78. public function actionMultidelete()
  79. {
  80. $ids = Yii::$app->request->get('ids');
  81. if(!empty($ids))
  82. {
  83. IpBanned::deleteAll("ip_banned_id in(".$ids.")");
  84. $msgdata = ['error' => 0,'msg' => '操作成功'];
  85. }
  86. else
  87. {
  88. $msgdata = ['error' => 1,'msg' => '请选择操作记录'];
  89. }
  90. echo_json($msgdata);
  91. }
  92. //删除
  93. public function actionDel()
  94. {
  95. $id = $this->getKeyId('ip_banned_id');
  96. $model = IpBanned::findOne($id);
  97. check_record_exists($model);
  98. if($model->delete())
  99. {
  100. $msgdata = ['error' => 0,'msg' => '操作成功!'];
  101. }
  102. else
  103. {
  104. $msgdata = ['error' => 1,'msg' => '操作失败!'];
  105. }
  106. echo_json($msgdata);
  107. }
  108. }