Iplimit.php 6.8 KB

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