You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
160 lines
4.9 KiB
160 lines
4.9 KiB
<?php
|
|
|
|
namespace app\admin\controller\user;
|
|
|
|
use app\admin\service\IpService;
|
|
use app\admin\validate\IpValidate;
|
|
use app\common\controller\AdminController;
|
|
use app\admin\model\Ip as IpModel;
|
|
use think\App;
|
|
use think\exception\ValidateException;
|
|
|
|
class Ip extends AdminController
|
|
{
|
|
public function __construct(App $app)
|
|
{
|
|
parent::__construct($app);
|
|
$this->model = new IpModel();
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
if ($this->request->isAjax()) {
|
|
if (input('selectFields')) {
|
|
return $this->selectList();
|
|
}
|
|
list($page, $limit, $where) = $this->buildTableParames();
|
|
$count = $this->model
|
|
->where($where)
|
|
->count();
|
|
$list = $this->model
|
|
->withoutField('delete_time')
|
|
->where($where)
|
|
->page($page, $limit)
|
|
->order('id desc')
|
|
->select()
|
|
->each(function ($item) {
|
|
$item['expire_time'] = date("Y-m-d H:i:s", $item['expire_time']);
|
|
return $item;
|
|
});
|
|
$data = [
|
|
'code' => 0,
|
|
'msg' => '',
|
|
'count' => $count,
|
|
'data' => $list
|
|
];
|
|
return json($data);
|
|
}
|
|
return $this->fetch();
|
|
}
|
|
|
|
/**
|
|
* @NodeAnotation(title="添加")
|
|
*/
|
|
public function add()
|
|
{
|
|
if ($this->request->isPost()) {
|
|
$post = $this->request->post();
|
|
try {
|
|
validate(IpValidate::class)->scene('add')->check($post);
|
|
$exists = IpValidate::checkExistsIp($post['ip'], $post['port']);
|
|
if ($exists !== true) throw new ValidateException($exists);
|
|
$post['expire_time'] = strtotime($post['expire_time']);
|
|
$save = $this->model->save($post);
|
|
} catch (ValidateException $e) {
|
|
$this->error($e->getMessage());
|
|
} catch (\Exception $e) {
|
|
$this->error('保存失败');
|
|
}
|
|
$save ? $this->success('保存成功') : $this->error('保存失败');
|
|
}
|
|
return $this->fetch();
|
|
}
|
|
|
|
/**
|
|
* @NodeAnotation(title="编辑")
|
|
*/
|
|
public function edit($id)
|
|
{
|
|
$row = $this->model->find($id);
|
|
empty($row) && $this->error('数据不存在');
|
|
if ($this->request->isPost()) {
|
|
$post = $this->request->post();
|
|
try {
|
|
validate(IpValidate::class)->scene('edit')->check($post);
|
|
$exists = IpValidate::checkExistsIp($post['ip'], $post['port'], $post['id']);
|
|
if ($exists !== true) throw new ValidateException($exists);
|
|
$post['expire_time'] = strtotime($post['expire_time']);
|
|
$save = $row->save($post);
|
|
} catch (ValidateException $e) {
|
|
$this->error($e->getMessage());
|
|
} catch (\Exception $e) {
|
|
$this->error('保存失败');
|
|
}
|
|
$save ? $this->success('保存成功') : $this->error('保存失败');
|
|
}
|
|
$this->assign('row', $row);
|
|
return $this->fetch();
|
|
}
|
|
|
|
/**
|
|
* @NodeAnotation(title="删除")
|
|
*/
|
|
public function delete($id)
|
|
{
|
|
$this->checkPostRequest();
|
|
$row = $this->model->whereIn('id', $id)->select();
|
|
$row->isEmpty() && $this->error('数据不存在');
|
|
try {
|
|
$save = false;
|
|
foreach ($row as $value) {
|
|
$save = $this->model->where('id', $value['id'])->useSoftDelete('delete_time', time())->delete();
|
|
}
|
|
} catch (\Exception $e) {
|
|
$this->error('删除失败');
|
|
}
|
|
$save ? $this->success('删除成功') : $this->error('删除失败');
|
|
}
|
|
|
|
/**
|
|
* @NodeAnotation(title="属性修改")
|
|
*/
|
|
public function modify()
|
|
{
|
|
$this->checkPostRequest();
|
|
$post = $this->request->post();
|
|
$rule = [
|
|
'id|ID' => 'require',
|
|
'field|字段' => 'require',
|
|
'value|值' => 'require',
|
|
];
|
|
$this->validate($post, $rule);
|
|
if (!in_array($post['field'], $this->allowModifyFields)) {
|
|
$this->error('该字段不允许修改:' . $post['field']);
|
|
}
|
|
$row = $this->model->find($post['id']);
|
|
empty($row) && $this->error('数据不存在');
|
|
try {
|
|
$row->save([
|
|
$post['field'] => $post['value'],
|
|
]);
|
|
} catch (\Exception $e) {
|
|
$this->error($e->getMessage());
|
|
}
|
|
$this->success('保存成功');
|
|
}
|
|
|
|
/**
|
|
* 更新ip
|
|
*/
|
|
public function updateIp()
|
|
{
|
|
try {
|
|
$res = IpService::proxy();
|
|
if (!$res) throw new \Exception('');
|
|
} catch (\Exception $e) {
|
|
$this->error('更新失败');
|
|
}
|
|
$this->success('更新成功');
|
|
}
|
|
}
|
|
|