From 77a235a792721b6b6cd4be83ae9f56c74b7d9f42 Mon Sep 17 00:00:00 2001 From: yaooo <272523191@qq.com> Date: Mon, 30 Oct 2023 11:01:30 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=B2=97=E4=BD=8D=E5=A2=9E?= =?UTF-8?q?=E5=88=A0=E6=94=B9=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/controller/UserPosition.php | 127 ++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 app/api/controller/UserPosition.php diff --git a/app/api/controller/UserPosition.php b/app/api/controller/UserPosition.php new file mode 100644 index 0000000..c7bdbea --- /dev/null +++ b/app/api/controller/UserPosition.php @@ -0,0 +1,127 @@ + ['except' => []] + ]; + + public function index() + { + $this->checkAuth(); + $list = Db::name('Position')->where('status', 1)->where('id', '>', 1)->order('create_time asc')->select()->toArray(); + foreach ($list as &$val) { + $groupId = Db::name('PositionGroup')->where(['pid' => $val['id']])->column('group_id'); + $groupName = Db::name('AdminGroup')->where('id', 'in', $groupId)->column('title'); + $val['groupId'] = $groupId; + $val['groupName'] = implode(',', $groupName); + } + $this->apiSuccess('获取成功', $list); + } + + public function group() + { + $group = Db::name('AdminGroup')->where('status', 1)->where('id', '>', 1)->order('create_time asc')->field(['id', 'title'])->select()->toArray(); + $this->apiSuccess('获取成功', $group); + } + + //添加&编辑 + public function add() + { + $this->checkAuth(); + $param = get_params(); + if (!empty($param['id']) && $param['id'] > 0) { + try { + validate(PositionCheck::class)->scene('edit')->check($param); + } catch (ValidateException $e) { + $this->apiError($e->getError()); + } + // 启动事务 + Db::startTrans(); + try { + Db::name('Position')->where(['id' => $param['id']])->strict(false)->field(true)->update($param); + Db::name('PositionGroup')->where(['pid' => $param['id']])->delete(); + foreach ($param['group_id'] as $k => $v) { + $data[$k] = [ + 'pid' => $param['id'], + 'group_id' => $v, + 'create_time' => time(), + ]; + } + Db::name('PositionGroup')->strict(false)->field(true)->insertAll($data); + add_log('edit', $param['id'], $param); + //清除菜单\权限缓存 + clear_cache('adminMenu'); + clear_cache('adminRules'); + // 提交事务 + Db::commit(); + } catch (\Exception $e) { + // 回滚事务 + Db::rollback(); + $this->apiError($e->getError()); + } + } else { + try { + validate(PositionCheck::class)->scene('add')->check($param); + } catch (ValidateException $e) { + $this->apiError($e->getError()); + } + // 启动事务 + Db::startTrans(); + try { + $uid = Db::name('Position')->strict(false)->field(true)->insertGetId($param); + foreach ($param['group_id'] as $k => $v) { + $data[$k] = [ + 'pid' => $uid, + 'group_id' => $v, + 'create_time' => time(), + ]; + } + Db::name('PositionGroup')->strict(false)->field(true)->insertAll($data); + add_log('add', $uid, $param); + // 提交事务 + Db::commit(); + } catch (\Exception $e) { + // 回滚事务 + Db::rollback(); + $this->apiError($e->getError()); + } + } + $this->apiSuccess('操作成功'); + } + + //删除 + public function delete() + { + $this->checkAuth(); + $id = get_params("id"); + if ($id == 1) { + $this->apiError("超级岗位不能删除"); + } + $data['status'] = '-1'; + $data['id'] = $id; + $data['update_time'] = time(); + if (Db::name('Position')->update($data) !== false) { + add_log('delete', $id); + return to_assign(0, "删除成功"); + } else { + $this->apiError("删除失败"); + } + } +}