isAjax()){ $where = isset($this->where['group_id']) ? ['id' => $this->where['group_id']] : []; $result = db('ip_whitelist')->order('id desc')->select(); foreach($result as $key=>$vo){ // 优化显示状态 if(1 == $vo['status']){ $result[$key]['status'] = '启用'; }else{ $result[$key]['status'] = '禁用'; } // 生成操作按钮 $result[$key]['operate'] = $this->makeBtn($vo['id']); } $return['total'] = db('ip_whitelist')->where($where)->count(); //总数据 $return['rows'] = $result; return json($return); } return $this->fetch(); } /** * 添加IP */ public function addIP() { $adb = db('admins'); if(request()->isPost()){ $param = input('post.'); $param['ip_addr'] = htmlentities($param['ip_addr']); $has = db('ip_whitelist')->field('id')->where('ip_addr', $param['ip_addr'])->find(); if(!empty($has)){ return json(['code' => -1, 'data' => '', 'msg' => '该IP已经存在']); } try{ db('ip_whitelist')->insert($param); }catch(\Exception $e){ return json(['code' => -2, 'data' => '', 'msg' => $e->getMessage()]); } LogRepository::write('白名单管理', '成功添加IP--'.$param['ip_addr']); return json(['code' => 1, 'data' => '', 'msg' => '添加IP成功']); } $this->assign([ 'status' => [ 1 => '启用', 0 => '禁用' ] ]); return $this->fetch('addIp'); } /** * 编辑IP */ public function editIp() { if(request()->isAjax()){ $param = input('post.'); try{ db('ip_whitelist')->where('id', $param['id'])->update(['status' => $param['status']]); }catch(\Exception $e){ return json(['code' => -2, 'data' => '', 'msg' => $e->getMessage()]); } return json(['code' => 1, 'data' => '', 'msg' => '编辑成功']); } $id = input('param.id/d'); $info = db('ip_whitelist')->where(['id' => $id])->find(); $this->assign([ 'info' => $info, 'status' => [1 => '启用',0 => '禁用'] ]); return $this->fetch('editIp'); } /** * 删除IP */ public function delIp() { if(request()->isAjax()){ $id = input('param.id/d'); try{ db('groups')->where('id', $id)->delete(); }catch(\Exception $e){ return json(['code' => -1, 'data' => '', 'msg' => $e->getMessage()]); } return json(['code' => 1, 'data' => '', 'msg' => '删除IP成功']); } } // 生成按钮 private function makeBtn($id) { $operate = ''; $operate .= ' '; $operate .= ' '; return $operate; } }