From 8a1cb521d0ed0d0e35f3d4226d7099c1d672ab0e Mon Sep 17 00:00:00 2001 From: yaooo <272523191@qq.com> Date: Sat, 28 Oct 2023 15:07:04 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E8=8E=B7=E5=8F=96=E9=83=A8?= =?UTF-8?q?=E9=97=A8=E6=9E=B6=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/controller/HomeIndex.php | 1 - app/api/controller/OaApprove.php | 1 - app/api/controller/UserDepartment.php | 107 ++++++++++++++++++++++++++ 3 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 app/api/controller/UserDepartment.php diff --git a/app/api/controller/HomeIndex.php b/app/api/controller/HomeIndex.php index b84d52b..341751e 100644 --- a/app/api/controller/HomeIndex.php +++ b/app/api/controller/HomeIndex.php @@ -16,7 +16,6 @@ use app\home\model\AdminLog; use app\user\validate\AdminCheck; use think\exception\ValidateException; use think\facade\Db; -use think\facade\View; class HomeIndex extends ApiController { diff --git a/app/api/controller/OaApprove.php b/app/api/controller/OaApprove.php index 89a7072..a37262a 100644 --- a/app/api/controller/OaApprove.php +++ b/app/api/controller/OaApprove.php @@ -16,7 +16,6 @@ use app\home\model\AdminLog; use app\user\validate\AdminCheck; use think\exception\ValidateException; use think\facade\Db; -use think\facade\View; class OaApprove extends ApiController { diff --git a/app/api/controller/UserDepartment.php b/app/api/controller/UserDepartment.php new file mode 100644 index 0000000..4d20efe --- /dev/null +++ b/app/api/controller/UserDepartment.php @@ -0,0 +1,107 @@ +field('d.*,a.name as leader') + ->alias('d') + ->join('Admin a', 'a.id = d.leader_id', 'LEFT') + ->order('d.sort desc,d.id asc') + ->select(); + $list = generateTree($cate); + $this->apiSuccess('获取成功', $list); + } + + //添加部门 + public function add() + { + $param = get_params(); + if (request()->isAjax()) { + if ($param['id'] > 0) { + try { + validate(DepartmentCheck::class)->scene('edit')->check($param); + } catch (ValidateException $e) { + // 验证失败 输出错误信息 + return to_assign(1, $e->getError()); + } + $param['update_time'] = time(); + $department_array = get_department_son($param['id']); + if (in_array($param['pid'], $department_array)) { + return to_assign(1, '上级部门不能是该部门本身或其下属部门'); + } else { + Db::name('Department')->strict(false)->field(true)->update($param); + add_log('edit', $param['id'], $param); + return to_assign(); + } + } else { + try { + validate(DepartmentCheck::class)->scene('add')->check($param); + } catch (ValidateException $e) { + // 验证失败 输出错误信息 + return to_assign(1, $e->getError()); + } + $did = Db::name('Department')->strict(false)->field(true)->insertGetId($param); + add_log('add', $did, $param); + return to_assign(); + } + } else { + $id = isset($param['id']) ? $param['id'] : 0; + $pid = isset($param['pid']) ? $param['pid'] : 0; + $department = set_recursion(get_department()); + if ($id > 0) { + $detail = Db::name('Department')->where(['id' => $id])->find(); + //获取子部门 + $department = get_department(); + $department_list = get_data_node($department, $id); + $department_array = array_column($department_list, 'id'); + //包括自己部门在内 + $department_array[] = $id; + $users = Db::name('Admin')->where([['did','in',$department_array], ['status','=',1]])->select(); + View::assign('users', $users); + View::assign('detail', $detail); + } + View::assign('department', $department); + View::assign('pid', $pid); + View::assign('id', $id); + return view(); + } + } + + //删除 + public function delete() + { + $id = get_params("id"); + $count = Db::name('Department')->where([['pid', '=', $id], ['status', '>=', 0]])->count(); + if ($count > 0) { + return to_assign(1, "该部门下还有子部门,无法删除"); + } + $users = Db::name('Admin')->where([['did', '=', $id], ['status', '>=', 0]])->count(); + if ($users > 0) { + return to_assign(1, "该部门下还有员工,无法删除"); + } + if (Db::name('Department')->delete($id) !== false) { + add_log('delete', $id); + return to_assign(0, "删除部门成功"); + } else { + return to_assign(1, "删除失败"); + } + } +}