Iplimit.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. namespace app\admin\controller;
  3. class Iplimit extends Base
  4. {
  5. //访问ip管理
  6. public function index()
  7. {
  8. if (request()->isAjax()) {
  9. $param = input('param.');
  10. $limit = $param['pageSize'];
  11. $offset = (($param['pageNumber'] - 1) * $limit);
  12. $where['object'] = $param['type'];
  13. if (strlen($param['searchText'])) {
  14. $ip = bindec(decbin(ip2long($param['searchText'])));
  15. $where['ip'] = ['like', '%' . $param['searchText'] . '%'];
  16. $res = db('iplimit')->where('start_ip','<=',$ip)->where('end_ip','>=',$ip)->where('object',$param['type'])->limit($offset, $limit)->select();
  17. }
  18. $result = db('iplimit')->where($where)->limit($offset, $limit)->select();
  19. if(!empty($res)){
  20. for($i=0;$i<count($res);$i++){
  21. $result[] = $res[$i];
  22. }
  23. }
  24. $admins = db('admins')->select();
  25. foreach ($result as $key => $vo) {
  26. for($i=0;$i<count($admins);$i++){
  27. if($vo['admin_id'] == $admins[$i]['id']){
  28. $result[$key]['admin'] = $admins[$i]['user_name'];
  29. }
  30. }
  31. if($vo['ip'] == ''){
  32. $result[$key]['ip'] = $result[$key]['start_ip'].' - '.$result[$key]['end_ip'];
  33. }
  34. // 生成操作按钮.
  35. $result[$key]['operate'] = $this->makeBtn($vo['id']);
  36. }
  37. // 总数据.
  38. $return['total'] = db('iplimit')->where($where)->count(); //总数据
  39. $return['rows'] = $result;
  40. return json($return);
  41. }
  42. return $this->fetch();
  43. }
  44. //访问ip添加
  45. public function add(){
  46. if(request()->isPost()){
  47. $param = input('post.');
  48. $findWhere = [
  49. 'ip' => $param['ip'],
  50. 'object' => $param['object'],
  51. ];
  52. $has = db('iplimit')->where($findWhere)->find();
  53. if (empty($has) === false) {
  54. return json(['code' => -1, 'data' => '', 'msg' => '该ip已经存在']);
  55. }
  56. if(strpos($param['ip'], '-') !== false){
  57. $ip = explode('-',$param['ip']);
  58. //将ip地址转换成int型
  59. $param['start_ip'] = bindec(decbin(ip2long($ip[0])));
  60. $param['end_ip'] = bindec(decbin(ip2long($ip[1])));
  61. }
  62. $param['ctime'] = date('Y-m-d H:i:s',time());
  63. $param['utime'] = date('Y-m-d H:i:s',time());
  64. $param['admin_id'] = session('user_id');
  65. try {
  66. db('iplimit')->insert($param);
  67. } catch(\Exception $e) {
  68. return json(['code' => -2, 'data' => '', 'msg' => $e->getMessage()]);
  69. }
  70. return json(['code' => 1, 'data' => '', 'msg' => '添加访问ip成功']);
  71. }
  72. $this->assign([
  73. 'status' => config('kf_status')
  74. ]);
  75. return $this->fetch();
  76. }
  77. //访问ip编辑
  78. public function edit(){
  79. if(request()->isAjax()){
  80. $param = input('post.');
  81. // 检测用户修改的管理员是否重复
  82. $has = db('iplimit')->where('ip', $param['ip'])->where('id', $param['id'])->find();
  83. if(!empty($has)){
  84. return json(['code' => -1, 'data' => '', 'msg' => '该访问ip已经存在']);
  85. }
  86. if(strpos($param['ip'], '-') !== false){
  87. $ip = explode('-',$param['ip']);
  88. //将ip地址转换成int型
  89. $param['start_ip'] = bindec(decbin(ip2long($ip[0])));
  90. $param['end_ip'] = bindec(decbin(ip2long($ip[1])));
  91. }
  92. $param['ctime'] = date('Y-m-d H:i:s',time());
  93. $param['utime'] = date('Y-m-d H:i:s',time());
  94. $param['admin_id'] = session('user_id');
  95. try{
  96. db('iplimit')->where('id', $param['id'])->update($param);
  97. }catch(\Exception $e){
  98. return json(['code' => -2, 'data' => '', 'msg' => $e->getMessage()]);
  99. }
  100. return json(['code' => 1, 'data' => '', 'msg' => '编辑访问ip成功']);
  101. }
  102. $id = input('param.id/d');
  103. $info = db('iplimit')->where('id', $id)->find();
  104. $this->assign([
  105. 'info' => $info,
  106. 'status' => config('kf_status')
  107. ]);
  108. return $this->fetch();
  109. }
  110. //删除
  111. public function del(){
  112. if(request()->isAjax()){
  113. $id = input('param.id/d');
  114. // return $id;
  115. try{
  116. db('iplimit')->where('id', $id)->delete();
  117. }catch(\Exception $e){
  118. return json(['code' => -1, 'data' => '', 'msg' => $e->getMessage()]);
  119. }
  120. return json(['code' => 1, 'data' => '', 'msg' => '删除ip成功']);
  121. }
  122. }
  123. //开启/关闭ip限制
  124. public function disjunctor(){
  125. if(request()->isAjax()){
  126. $disjunctor = input('param.disjunctor');
  127. $type = input('param.type');
  128. try{
  129. if($disjunctor == 'on' && $type == 'kf'){
  130. $msg = '开启客服白名单';
  131. db('settings')->where('id', 1)->update(['kf_white_list'=>$disjunctor]);
  132. }
  133. if($disjunctor == 'off' && $type == 'kf'){
  134. $msg = '关闭客服白名单';
  135. db('settings')->where('id', 1)->update(['kf_white_list'=>$disjunctor]);
  136. }
  137. if($disjunctor == 'on' && $type == 'account'){
  138. $msg = '开启用户黑名单';
  139. db('settings')->where('id', 1)->update(['account_black_list'=>$disjunctor]);
  140. }
  141. if($disjunctor == 'off' && $type == 'account'){
  142. $msg = '关闭用户黑名单';
  143. db('settings')->where('id', 1)->update(['account_black_list'=>$disjunctor]);
  144. }
  145. }catch(\Exception $e){
  146. return json(['code' => -1, 'data' => '', 'msg' => $e->getMessage()]);
  147. }
  148. return json(['code' => 1, 'data' => '', 'msg' => $msg.'成功']);
  149. }
  150. }
  151. // 生成按钮
  152. private function makeBtn($id)
  153. {
  154. $operate = '<a href="' . url('iplimit/edit', ['id' => $id]) . '">';
  155. $operate .= '<button type="button" class="btn btn-primary btn-sm"><i class="fa fa-paste"></i> 编辑</button></a> ';
  156. $operate .= '<a href="javascript:Del(' . $id . ')"><button type="button" class="btn btn-danger btn-sm">';
  157. $operate .= '<i class="fa fa-trash-o"></i> 删除</button></a> ';
  158. return $operate;
  159. }
  160. }