kefu/application/admin/controller/Ip.php

134 lines
3.7 KiB
PHP

<?php
/**
* IP白名单
*/
namespace app\admin\controller;
use Repository\LogRepository;
class Ip extends Base
{
/**
* IP白名单列表
*/
public function index(){
if(request()->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'] = '<span class="label label-primary">启用</span>';
}else{
$result[$key]['status'] = '<span class="label label-danger">禁用</span>';
}
// 生成操作按钮
$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 = '<a href="' . url('ip/editIp', ['id' => $id]) . '">';
$operate .= '<button type="button" class="btn btn-primary btn-sm"><i class="fa fa-paste"></i> 编辑</button></a> ';
$operate .= '<a href="javascript:deleteIp(' . $id . ')"><button type="button" class="btn btn-danger btn-sm">';
$operate .= '<i class="fa fa-trash-o"></i> 删除</button></a> ';
return $operate;
}
}