8 changed files with 360 additions and 1 deletions
@ -0,0 +1,32 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace app\admin\controller\user; |
||||
|
|
||||
|
use app\admin\model\Ip as IpModel; |
||||
|
use app\admin\model\User; |
||||
|
use app\common\controller\AdminController; |
||||
|
use think\App; |
||||
|
use think\response\Json; |
||||
|
|
||||
|
class Api extends AdminController |
||||
|
{ |
||||
|
|
||||
|
public function __construct(App $app) |
||||
|
{ |
||||
|
parent::__construct($app); |
||||
|
} |
||||
|
|
||||
|
public function getUserPageList(): Json |
||||
|
{ |
||||
|
$userList = User::getPageList($this->request->get()); |
||||
|
$data = ['code' => 0, 'msg' => '']; |
||||
|
return json(array_merge($data, $userList)); |
||||
|
} |
||||
|
|
||||
|
public function getIpPageList(): Json |
||||
|
{ |
||||
|
$ipList = IpModel::getPageList($this->request->get()); |
||||
|
$data = ['code' => 0, 'msg' => '']; |
||||
|
return json(array_merge($data, $ipList)); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,124 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace app\admin\controller\user; |
||||
|
|
||||
|
use app\admin\model\User; |
||||
|
use app\common\controller\AdminController; |
||||
|
use app\admin\model\IpUser as IpUserModel; |
||||
|
use app\admin\model\Ip as IpModel; |
||||
|
use think\App; |
||||
|
use think\exception\ValidateException; |
||||
|
|
||||
|
class IpUser extends AdminController |
||||
|
{ |
||||
|
public function __construct(App $app) |
||||
|
{ |
||||
|
parent::__construct($app); |
||||
|
$this->model = new IpUserModel(); |
||||
|
} |
||||
|
|
||||
|
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('update_time, delete_time') |
||||
|
->where($where) |
||||
|
->page($page, $limit) |
||||
|
->order('id') |
||||
|
->select() |
||||
|
->each(function ($item) { |
||||
|
$userData = User::where('uid', $item['uid'])->field('nick_name, avatar')->find(); |
||||
|
$item['username'] = $userData['nick_name'] ?? '-'; |
||||
|
$item['avatar'] = $userData['avatar'] ?? ''; |
||||
|
$ipData = IpModel::where('id', $item['ip_id'])->field('ip, status')->find(); |
||||
|
$item['ip'] = $ipData['ip'] ?? '-'; |
||||
|
$item['ip_status'] = $ipData['status'] ?? '-'; |
||||
|
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 { |
||||
|
$rule = ['uid|用户' => 'require|number', 'ip_id|IP' => 'require|number']; |
||||
|
validate()->rule($rule)->scene('add')->check($post); |
||||
|
$query = IpUserModel::where(['uid' => $post['uid'], 'ip_id' => $post['ip_id']])->find(); |
||||
|
if ($query) throw new ValidateException('用户关联IP已存在'); |
||||
|
$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 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('保存成功'); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,13 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace app\admin\model; |
||||
|
|
||||
|
use app\common\model\TimeModel; |
||||
|
|
||||
|
class IpUser extends TimeModel |
||||
|
{ |
||||
|
|
||||
|
protected $deleteTime = 'delete_time'; |
||||
|
|
||||
|
protected $defaultSoftDelete = '0'; |
||||
|
} |
||||
@ -0,0 +1,38 @@ |
|||||
|
<div class="layuimini-container"> |
||||
|
<form id="app-form" class="layui-form layuimini-form"> |
||||
|
|
||||
|
|
||||
|
<div class="layui-form-item"> |
||||
|
<label class="layui-form-label required">用户</label> |
||||
|
<div class="layui-input-block"> |
||||
|
<input type="text" id="nick_name" class="layui-input" readonly lay-verify="required" lay-reqtext="请选择用户" placeholder="请选择用户" value=""> |
||||
|
<input type="hidden" name="uid" id="uid" value=""> |
||||
|
<tip>选择用户。</tip> |
||||
|
</div> |
||||
|
</div> |
||||
|
|
||||
|
<div class="layui-form-item"> |
||||
|
<label class="layui-form-label required">IP</label> |
||||
|
<div class="layui-input-block"> |
||||
|
<input type="text" id="ip" class="layui-input" readonly lay-verify="required" lay-reqtext="请选择IP" placeholder="请选择IP" value=""> |
||||
|
<input type="hidden" name="ip_id" id="ip_id" value=""> |
||||
|
<tip>选择IP。</tip> |
||||
|
</div> |
||||
|
</div> |
||||
|
|
||||
|
<div class="layui-form-item"> |
||||
|
<label class="layui-form-label required">状态</label> |
||||
|
<div class="layui-input-block"> |
||||
|
<input type="radio" name="status" value="1" title="启用" checked> |
||||
|
<input type="radio" name="status" value="0" title="禁用"> |
||||
|
</div> |
||||
|
</div> |
||||
|
|
||||
|
<div class="hr-line"></div> |
||||
|
<div class="layui-form-item text-center"> |
||||
|
<button type="submit" class="layui-btn layui-btn-normal layui-btn-sm" lay-submit>确认</button> |
||||
|
<button type="reset" class="layui-btn layui-btn-primary layui-btn-sm">重置</button> |
||||
|
</div> |
||||
|
|
||||
|
</form> |
||||
|
</div> |
||||
@ -0,0 +1,9 @@ |
|||||
|
<div class="layuimini-container"> |
||||
|
<div class="layuimini-main"> |
||||
|
<table id="currentTable" class="layui-table layui-hide" |
||||
|
data-auth-add="{:auth('user.ipuser/add')}" |
||||
|
data-auth-delete="{:auth('user.ipuser/delete')}" |
||||
|
lay-filter="currentTable"> |
||||
|
</table> |
||||
|
</div> |
||||
|
</div> |
||||
@ -0,0 +1,114 @@ |
|||||
|
define(["jquery", "easy-admin", "tableSelect"], function ($, ea) { |
||||
|
|
||||
|
let tableSelect = layui.tableSelect; |
||||
|
|
||||
|
let init = { |
||||
|
table_elem: '#currentTable', |
||||
|
table_render_id: 'currentTableRenderId', |
||||
|
index_url: 'user.ipuser/index', |
||||
|
add_url: 'user.ipuser/add', |
||||
|
delete_url: 'user.ipuser/delete', |
||||
|
modify_url: 'user.ipuser/modify', |
||||
|
user_list_url: 'user.api/getUserPageList', |
||||
|
ip_list_url: 'user.api/getIpPageList', |
||||
|
}; |
||||
|
|
||||
|
let Controller = { |
||||
|
|
||||
|
index: function () { |
||||
|
|
||||
|
ea.table.render({ |
||||
|
init: init, |
||||
|
toolbar: [ |
||||
|
'refresh', |
||||
|
'add', |
||||
|
'delete' |
||||
|
], |
||||
|
cols: [[ |
||||
|
{type: "checkbox"}, |
||||
|
{field: 'id', width: 80, title: '编号'}, |
||||
|
{field: 'username', minWidth: 80, title: '用户昵称'}, |
||||
|
{field: 'avatar', minWidth: 80, title: '头像', search: false, templet: ea.table.image}, |
||||
|
{field: 'ip', minWidth: 80, title: 'IP'}, |
||||
|
{field: 'count', minWidth: 80, title: '使用次数'}, |
||||
|
{field: 'status', title: '状态', width: 100, search: 'select', selectList: {0: '禁用', 1: '启用'}, templet: ea.table.switch}, |
||||
|
{field: 'ip_status', title: 'IP状态', width: 80, templet: function (d) { |
||||
|
let html = ''; |
||||
|
if (d.ip_status == 1) { |
||||
|
html = '<button type="button" class="layui-btn layui-bg-blue layui-btn-xs">启用</button>' |
||||
|
} else { |
||||
|
html = '<button type="button" class="layui-btn layui-bg-red layui-btn-xs">禁用</button>' |
||||
|
} |
||||
|
return html |
||||
|
}}, |
||||
|
{field: 'create_time', minWidth: 80, title: '创建时间', search: 'range'}, |
||||
|
{ |
||||
|
width: 250, |
||||
|
title: '操作', |
||||
|
templet: ea.table.tool, |
||||
|
operat: [ |
||||
|
'delete' |
||||
|
] |
||||
|
} |
||||
|
]], |
||||
|
}); |
||||
|
|
||||
|
ea.listen(); |
||||
|
}, |
||||
|
add: function () { |
||||
|
tableSelect.render({ |
||||
|
elem: '#nick_name', //定义输入框input对象
|
||||
|
checkedKey: 'uid', //表格的唯一建值,非常重要,影响到选中状态 必填
|
||||
|
searchKey: 'keyword', //搜索输入框的name值 默认keyword
|
||||
|
searchPlaceholder: '关键词搜索', //搜索输入框的提示文字 默认关键词搜索
|
||||
|
table: { //定义表格参数,与LAYUI的TABLE模块一致,只是无需再定义表格elem
|
||||
|
url: ea.url(init.user_list_url), |
||||
|
cols: [[ |
||||
|
{ type: 'radio' }, |
||||
|
{ field: 'uid', title: 'ID' }, |
||||
|
{ field: 'nick_name', title: '姓名' } |
||||
|
]] |
||||
|
}, |
||||
|
done: function (elem, data) { |
||||
|
let uidArr = [] |
||||
|
let nameArr = [] |
||||
|
layui.each(data.data, function (index, item) { |
||||
|
uidArr.push(item.uid) |
||||
|
nameArr.push(item.nick_name) |
||||
|
}) |
||||
|
$("#uid").val(uidArr.join(',')) |
||||
|
$("#nick_name").val(nameArr.join(',')) |
||||
|
} |
||||
|
}) |
||||
|
tableSelect.render({ |
||||
|
elem: '#ip', //定义输入框input对象
|
||||
|
checkedKey: 'id', //表格的唯一建值,非常重要,影响到选中状态 必填
|
||||
|
searchKey: 'keyword', //搜索输入框的name值 默认keyword
|
||||
|
searchPlaceholder: '关键词搜索', //搜索输入框的提示文字 默认关键词搜索
|
||||
|
table: { //定义表格参数,与LAYUI的TABLE模块一致,只是无需再定义表格elem
|
||||
|
url: ea.url(init.ip_list_url), |
||||
|
cols: [[ |
||||
|
{ type: 'radio' }, |
||||
|
{ field: 'id', title: 'ID' }, |
||||
|
{ field: 'ip', title: 'IP' } |
||||
|
]] |
||||
|
}, |
||||
|
done: function (elem, data) { |
||||
|
let ipArr = [] |
||||
|
let ipIdArr = [] |
||||
|
layui.each(data.data, function (index, item) { |
||||
|
ipArr.push(item.ip) |
||||
|
ipIdArr.push(item.id) |
||||
|
}) |
||||
|
$("#ip").val(ipArr.join(',')) |
||||
|
$("#ip_id").val(ipIdArr.join(',')) |
||||
|
} |
||||
|
}) |
||||
|
ea.listen(); |
||||
|
}, |
||||
|
edit: function () { |
||||
|
ea.listen(); |
||||
|
}, |
||||
|
}; |
||||
|
return Controller; |
||||
|
}); |
||||
Loading…
Reference in new issue