diff --git a/app/admin/controller/user/Ipuser.php b/app/admin/controller/user/Ipuser.php index 66c1b5d..5b0e33e 100644 --- a/app/admin/controller/user/Ipuser.php +++ b/app/admin/controller/user/Ipuser.php @@ -52,12 +52,11 @@ class Ipuser extends AdminController ->withoutField('update_time, delete_time') ->where($where) ->page($page, $limit) - ->order('id') + ->order('id desc') ->select() ->each(function ($item) { - $userData = User::where('uid', $item['uid'])->field('nick_name, avatar')->find(); - $item['username'] = $userData['nick_name'] ?? '-'; - $item['avatar'] = $userData['avatar'] ?? ''; + $item['username'] = User::getUserValue($item['uid']); + $item['avatar'] = User::getUserValue($item['uid'], 'avatar'); $ipData = IpModel::where('id', $item['ip_id'])->field('ip, status')->find(); $item['ip'] = $ipData['ip'] ?? '-'; $item['ip_status'] = $ipData['status'] ?? '-'; diff --git a/app/admin/controller/user/Useragent.php b/app/admin/controller/user/Useragent.php new file mode 100644 index 0000000..d1b49ca --- /dev/null +++ b/app/admin/controller/user/Useragent.php @@ -0,0 +1,132 @@ +model = new UserAgentModel(); + } + + protected function indexWhere(&$where) + { + if ($where) { + foreach ($where as $key => $w) { + if ($w[0] == 'username') { + $uid = User::where('nick_name', $w[1], $w[2])->column('uid'); + $where[$key] = ['uid', 'in', $uid ?: [0]]; + } + } + } + } + + public function index() + { + if ($this->request->isAjax()) { + if (input('selectFields')) { + return $this->selectList(); + } + list($page, $limit, $where) = $this->buildTableParames(); + $this->indexWhere($where); + $count = $this->model + ->where($where) + ->count(); + $list = $this->model + ->withoutField('update_time, delete_time') + ->where($where) + ->page($page, $limit) + ->order('id desc') + ->select() + ->each(function ($item) { + $item['username'] = User::getUserValue($item['uid']); + $item['avatar'] = User::getUserValue($item['uid'], 'avatar'); + }); + $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', 'status|状态' => 'require|in:0,1']; + validate()->rule($rule)->check($post); + $query = $this->model->where('uid', $post['uid'])->find(); + if ($query) throw new ValidateException('代理已存在'); + $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('保存成功'); + } +} diff --git a/app/admin/model/UserAgent.php b/app/admin/model/UserAgent.php new file mode 100644 index 0000000..e4da3d7 --- /dev/null +++ b/app/admin/model/UserAgent.php @@ -0,0 +1,15 @@ + +
+ + +
+ +
+ + + 选择用户。 +
+
+ +
+ +
+ + +
+
+ +
+
+ + +
+ +
+ diff --git a/app/admin/view/user/useragent/index.html b/app/admin/view/user/useragent/index.html new file mode 100644 index 0000000..f7d4c99 --- /dev/null +++ b/app/admin/view/user/useragent/index.html @@ -0,0 +1,9 @@ +
+
+ +
+
+
diff --git a/app/api/controller/User.php b/app/api/controller/User.php index 9679917..8ba7282 100644 --- a/app/api/controller/User.php +++ b/app/api/controller/User.php @@ -4,6 +4,7 @@ namespace app\api\controller; use app\admin\model\Task; use app\admin\model\User as UserModel; +use app\admin\model\UserAgent; use app\api\middleware\LcJWTAuth; use think\response\Json; @@ -15,10 +16,9 @@ class User extends ApiController if (!$this->request->isPost()) { return $this->renderError('不支持GET请求'); } - $data = $this->postData(); $model = new UserModel(); $field = 'password,salt,last_login_time,create_time,update_time,delete_time'; - $row = $model->where('uid', $data['uid'])->withoutField($field)->find(); + $row = $model->where('uid', $this->request->uid)->withoutField($field)->find(); if ($row->isEmpty()) { return $this->renderError($model->getError() ?: '用户不存在'); } @@ -28,6 +28,7 @@ class User extends ApiController $row['gender'] = $model->genderArr[$row['gender']] ?? ''; } $row['task_count'] = Task::where([['assign_uid' , '=', $row['uid']], ['status' ,'in', [1,2]]])->count(); + $row['agent'] = (new UserAgent)->where(['uid' => $row['uid'], 'status' => 1])->count(); return $this->renderSuccess($row->toArray()); } diff --git a/public/static/admin/js/user/useragent.js b/public/static/admin/js/user/useragent.js new file mode 100644 index 0000000..85cbba9 --- /dev/null +++ b/public/static/admin/js/user/useragent.js @@ -0,0 +1,78 @@ +define(["jquery", "easy-admin", "tableSelect"], function ($, ea) { + + let tableSelect = layui.tableSelect; + + let init = { + table_elem: '#currentTable', + table_render_id: 'currentTableRenderId', + index_url: 'user.useragent/index', + add_url: 'user.useragent/add', + delete_url: 'user.useragent/delete', + modify_url: 'user.useragent/modify', + user_list_url: 'user.api/getUserPageList', + }; + + 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: 'status', title: '状态', width: 100, search: 'select', selectList: {0: '禁用', 1: '启用'}, templet: ea.table.switch}, + {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(',')) + } + }) + ea.listen(); + }, + edit: function () { + ea.listen(); + }, + }; + return Controller; +});