From 961fe95eb9e059bcda2b5ade2c285f88c2761bcf Mon Sep 17 00:00:00 2001 From: weiz Date: Mon, 11 Dec 2023 10:22:16 +0800 Subject: [PATCH 1/3] update --- app/adminapi/logic/auth/AdminLogic.php | 104 +++---------------- app/adminapi/validate/auth/AdminValidate.php | 42 ++++++++ 2 files changed, 59 insertions(+), 87 deletions(-) diff --git a/app/adminapi/logic/auth/AdminLogic.php b/app/adminapi/logic/auth/AdminLogic.php index b52918753..31425188d 100755 --- a/app/adminapi/logic/auth/AdminLogic.php +++ b/app/adminapi/logic/auth/AdminLogic.php @@ -24,6 +24,9 @@ use app\common\model\auth\AdminOrgs; use app\common\model\auth\AdminRole; use app\common\model\auth\AdminSession; use app\common\cache\AdminTokenCache; +use app\common\model\dept\Dept; +use app\common\model\dept\Jobs; +use app\common\model\dept\Orgs; use app\common\service\FileService; use think\facade\Config; use think\facade\Db; @@ -50,7 +53,6 @@ class AdminLogic extends BaseLogic $password = create_password($params['password'], $passwordSalt); $defaultAvatar = config('project.default_image.admin_avatar'); $avatar = !empty($params['avatar']) ? FileService::setFileUrl($params['avatar']) : $defaultAvatar; - $admin = Admin::create([ 'name' => $params['name'], 'account' => $params['account'], @@ -59,17 +61,12 @@ class AdminLogic extends BaseLogic 'create_time' => time(), 'disable' => $params['disable'], 'multipoint_login' => $params['multipoint_login'], + 'org_id' => $params['org_id'], + 'dept_id' => $params['dept_id'], + 'job_id' => $params['job_id'], ]); - // 角色 self::insertRole($admin['id'], $params['role_id'] ?? []); - // 组织 - self::insertOrg($admin['id'], $params['org_id'] ?? 0); - // 部门 - self::insertDept($admin['id'], $params['dept_id'] ?? 0); - // 岗位 - self::insertJobs($admin['id'], $params['jobs_id'] ?? 0); - Db::commit(); return true; } catch (\Exception $e) { @@ -97,7 +94,10 @@ class AdminLogic extends BaseLogic 'name' => $params['name'], 'account' => $params['account'], 'disable' => $params['disable'], - 'multipoint_login' => $params['multipoint_login'] + 'multipoint_login' => $params['multipoint_login'], + 'org_id' => $params['org_id'], + 'dept_id' => $params['dept_id'], + 'job_id' => $params['job_id'], ]; // 头像 @@ -115,31 +115,18 @@ class AdminLogic extends BaseLogic if (!empty(array_diff_assoc($roleId, $params['role_id']))) { $editRole = true; } - if ($params['disable'] == 1 || $editRole) { $tokenArr = AdminSession::where('admin_id', $params['id'])->select()->toArray(); foreach ($tokenArr as $token) { self::expireToken($token['token']); } } - Admin::update($data); (new AdminAuthCache($params['id']))->clearAuthCache(); - // 删除旧的关联信息 AdminRole::delByUserId($params['id']); - AdminOrgs::delByUserId($params['id']); - AdminDept::delByUserId($params['id']); - AdminJobs::delByUserId($params['id']); // 角色 self::insertRole($params['id'], $params['role_id']); - // 组织 - self::insertOrg($params['id'], $params['org_id'] ?? 0); - // 部门 - self::insertDept($params['id'], $params['dept_id'] ?? 0); - // 岗位 - self::insertJobs($params['id'], $params['jobs_id'] ?? 0); - Db::commit(); return true; } catch (\Exception $e) { @@ -176,10 +163,6 @@ class AdminLogic extends BaseLogic // 删除旧的关联信息 AdminRole::delByUserId($params['id']); - AdminOrgs::delByUserId($params['id']); - AdminDept::delByUserId($params['id']); - AdminJobs::delByUserId($params['id']); - Db::commit(); return true; } catch (\Exception $e) { @@ -229,9 +212,15 @@ class AdminLogic extends BaseLogic public static function detail($params, $action = 'detail'): array { $admin = Admin::field([ - 'id', 'account', 'name', 'disable', 'root', + 'id', 'account', 'name', 'disable', 'root', 'org_id', 'dept_id', 'job_id', 'multipoint_login', 'avatar', ])->findOrEmpty($params['id'])->toArray(); + $job = Jobs::field('name')->where('id',$admin['job_id'])->findOrEmpty(); + $dept = Dept::field('name')->where('id',$admin['dept_id'])->findOrEmpty(); + $org = Orgs::field('name')->where('id',$admin['org_id'])->findOrEmpty(); + $admin['job_name'] = $job->isEmpty() ? '' : $job['name']; + $admin['dept_name'] = $dept->isEmpty() ? '' : $dept['name']; + $admin['org_name'] = $org->isEmpty() ? '' : $org['name']; if ($action == 'detail') { return $admin; } @@ -292,63 +281,4 @@ class AdminLogic extends BaseLogic (new AdminRole())->saveAll($roleData); } } - - /** - * @notes 新增组织 - * @param $adminId - * @param $orgIds - * @throws \Exception - * @author 段誉 - * @date 2022/11/25 14:22 - */ - public static function insertOrg($adminId, $orgIds) - { - // 岗位 - if (!empty($orgsIds)) { - (new AdminOrgs())->save([ - 'admin_id' => $adminId, - 'org_id' => $orgsIds - ]); - } - } - - /** - * @notes 新增部门 - * @param $adminId - * @param $deptIds - * @throws \Exception - * @author 段誉 - * @date 2022/11/25 14:22 - */ - public static function insertDept($adminId, $deptIds) - { - // 部门 - if (!empty($deptIds)) { - (new AdminDept())->save([ - 'admin_id' => $adminId, - 'dept_id' => $deptIds - ]); - } - } - - - /** - * @notes 新增岗位 - * @param $adminId - * @param $jobsIds - * @throws \Exception - * @author 段誉 - * @date 2022/11/25 14:22 - */ - public static function insertJobs($adminId, $jobsIds) - { - // 岗位 - if (!empty($jobsIds)) { - (new AdminJobs())->save([ - 'admin_id' => $adminId, - 'jobs_id' => $jobsIds - ]); - } - } - } \ No newline at end of file diff --git a/app/adminapi/validate/auth/AdminValidate.php b/app/adminapi/validate/auth/AdminValidate.php index ad0deea17..864a0d69e 100755 --- a/app/adminapi/validate/auth/AdminValidate.php +++ b/app/adminapi/validate/auth/AdminValidate.php @@ -14,6 +14,9 @@ namespace app\adminapi\validate\auth; +use app\common\model\dept\Dept; +use app\common\model\dept\Jobs; +use app\common\model\dept\Orgs; use app\common\validate\BaseValidate; use app\common\model\auth\Admin; @@ -33,6 +36,9 @@ class AdminValidate extends BaseValidate 'role_id' => 'require', 'disable' => 'require|in:0,1|checkAbleDisable', 'multipoint_login' => 'require|in:0,1', + 'org_id' => 'checkOrg', + 'dept_id' => 'requireWith:org_id|checkDept', + 'job_id' => 'requireWith:dept_id|checkJob' ]; protected $message = [ @@ -52,6 +58,8 @@ class AdminValidate extends BaseValidate 'disable.in' => '状态值错误', 'multipoint_login.require' => '请选择是否支持多处登录', 'multipoint_login.in' => '多处登录状态值为误', + 'dept_id.requireWith' => '请选择部门', + 'job_id.requireWith' => '请选择岗位', ]; /** @@ -164,5 +172,39 @@ class AdminValidate extends BaseValidate } return true; } + + //验证组织 + public function checkOrg($value): bool|string + { + $org = Orgs::where('id',$value)->findOrEmpty(); + if($org->isEmpty()){ + return '组织不存在'; + } + return true; + } + + public function checkDept($value, $rule, $data): bool|string + { + $dept = Dept::where('id',$value)->findOrEmpty(); + if($dept->isEmpty()){ + return '部门不存在'; + } + if($dept['org_id'] != $data['org_id']){ + return '该部门不属于当前选择的组织'; + } + return true; + } + + public function checkJob($value, $rule, $data): bool|string + { + $job = Jobs::where('id',$value)->findOrEmpty(); + if($job->isEmpty()){ + return '岗位不存在'; + } + if($job['dept_id'] != $data['dept_id']){ + return '该岗位不属于当前选择的部门'; + } + return true; + } } \ No newline at end of file From 3dc5e2e47fc2522c8cac2634f5ebb0b207b80648 Mon Sep 17 00:00:00 2001 From: weiz Date: Mon, 11 Dec 2023 10:54:36 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E5=85=A8=E9=83=A8=E4=BA=BA=E5=91=98=E3=80=81=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E6=9F=90=E4=B8=AA=E9=83=A8=E9=97=A8=E4=B8=8B=E7=9A=84=E4=BA=BA?= =?UTF-8?q?=E5=91=98=E3=80=81=E8=8E=B7=E5=8F=96=E6=9F=90=E4=B8=AA=E5=B2=97?= =?UTF-8?q?=E4=BD=8D=E4=B8=8B=E7=9A=84=E4=BA=BA=E5=91=98=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/auth/AdminController.php | 65 +++++++++++++++++++ app/adminapi/logic/dept/JobsLogic.php | 4 ++ 2 files changed, 69 insertions(+) diff --git a/app/adminapi/controller/auth/AdminController.php b/app/adminapi/controller/auth/AdminController.php index dd718713f..2def5328f 100755 --- a/app/adminapi/controller/auth/AdminController.php +++ b/app/adminapi/controller/auth/AdminController.php @@ -134,4 +134,69 @@ class AdminController extends BaseAdminController $result = AdminLogic::editSelf($params); return $this->success('操作成功', [], 1, 1); } + + //获取所有人员 + public function getAdminsByAll(): \think\response\Json + { + $data = Admin::field('id,name,avatar,org_id,dept_id,job_id')->select()->each(function($item){ + $job = Jobs::field('name')->where('id',$item['job_id'])->findOrEmpty(); + $dept = Dept::field('name')->where('id',$item['dept_id'])->findOrEmpty(); + $org = Orgs::field('name')->where('id',$item['org_id'])->findOrEmpty(); + $item['job_name'] = $job->isEmpty() ? '' : $job['name']; + $item['dept_name'] = $dept->isEmpty() ? '' : $dept['name']; + $item['org_name'] = $org->isEmpty() ? '' : $org['name']; + unset($item['org_id'],$item['dept_id'],$item['job_id']); + return $item; + })->toArray(); + foreach($data as $k=>$v){ + unset($data[$k]['role_id']); + } + return $this->success('请求成功',$data); + } + + //获取某个部门下的所有人员 + public function getAdminsByDept(): \think\response\Json + { + $dept_id = $this->request->get('dept_id'); + if(empty($dept_id)){ + return $this->fail('参数错误'); + } + $data = Admin::field('id,name,avatar,org_id,dept_id,job_id')->where('dept_id',$dept_id)->select()->each(function($item){ + $job = Jobs::field('name')->where('id',$item['job_id'])->findOrEmpty(); + $dept = Dept::field('name')->where('id',$item['dept_id'])->findOrEmpty(); + $org = Orgs::field('name')->where('id',$item['org_id'])->findOrEmpty(); + $item['job_name'] = $job->isEmpty() ? '' : $job['name']; + $item['dept_name'] = $dept->isEmpty() ? '' : $dept['name']; + $item['org_name'] = $org->isEmpty() ? '' : $org['name']; + unset($item['org_id'],$item['dept_id'],$item['job_id']); + return $item; + })->toArray(); + foreach($data as $k=>$v){ + unset($data[$k]['role_id']); + } + return $this->success('请求成功',$data); + } + + //获取某个岗位下的所有人员 + public function getAdminsByJob(): \think\response\Json + { + $job_id = $this->request->get('job_id'); + if(empty($job_id)){ + return $this->fail('参数错误'); + } + $data = Admin::field('id,name,avatar,org_id,dept_id,job_id')->where('job_id',$job_id)->select()->each(function($item){ + $job = Jobs::field('name')->where('id',$item['job_id'])->findOrEmpty(); + $dept = Dept::field('name')->where('id',$item['dept_id'])->findOrEmpty(); + $org = Orgs::field('name')->where('id',$item['org_id'])->findOrEmpty(); + $item['job_name'] = $job->isEmpty() ? '' : $job['name']; + $item['dept_name'] = $dept->isEmpty() ? '' : $dept['name']; + $item['org_name'] = $org->isEmpty() ? '' : $org['name']; + unset($item['org_id'],$item['dept_id'],$item['job_id']); + return $item; + })->toArray(); + foreach($data as $k=>$v){ + unset($data[$k]['role_id']); + } + return $this->success('请求成功',$data); + } } \ No newline at end of file diff --git a/app/adminapi/logic/dept/JobsLogic.php b/app/adminapi/logic/dept/JobsLogic.php index ed368ca60..001efe937 100755 --- a/app/adminapi/logic/dept/JobsLogic.php +++ b/app/adminapi/logic/dept/JobsLogic.php @@ -19,6 +19,7 @@ use app\common\logic\BaseLogic; use app\common\model\article\Article; use app\common\model\dept\Dept; use app\common\model\dept\Jobs; +use app\common\model\dept\Orgs; use app\common\service\FileService; @@ -99,7 +100,10 @@ class JobsLogic extends BaseLogic return []; } $dept = Dept::where('id',$jobs['dept_id'])->findOrEmpty(); + $org = Orgs::where('id',$dept['org_id'])->findOrEmpty(); $jobs['dept_name'] = $dept->isEmpty() ? '' : $dept['name']; + $jobs['org_id'] = $org->isEmpty() ? 0 : $org['id']; + $jobs['org_name'] = $org->isEmpty() ? '' : $org['name']; $jobs['status_text'] = $jobs->status_text; return $jobs->toArray(); } From 1e0ceaa66bae1c2fc394c278889d809fde8f3c0a Mon Sep 17 00:00:00 2001 From: weiz Date: Mon, 11 Dec 2023 11:25:15 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E6=89=80=E6=9C=89=E9=83=A8=E9=97=A8=E3=80=81=E6=89=80=E6=9C=89?= =?UTF-8?q?=E5=B2=97=E4=BD=8D=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/adminapi/controller/dept/DeptController.php | 16 ++++++++++++++++ app/adminapi/controller/dept/JobsController.php | 11 +++++++++++ app/common.php | 9 +++++++++ 3 files changed, 36 insertions(+) diff --git a/app/adminapi/controller/dept/DeptController.php b/app/adminapi/controller/dept/DeptController.php index bc078a1db..ea5f136c8 100755 --- a/app/adminapi/controller/dept/DeptController.php +++ b/app/adminapi/controller/dept/DeptController.php @@ -18,7 +18,9 @@ use app\adminapi\controller\BaseAdminController; use app\adminapi\lists\dept\DeptLists; use app\adminapi\logic\dept\DeptLogic; use app\adminapi\validate\dept\DeptValidate; +use app\common\model\auth\Admin; use app\common\model\dept\Dept; +use app\common\model\dept\Orgs; /** * 部门管理控制器 @@ -107,4 +109,18 @@ class DeptController extends BaseAdminController $data = Dept::field('id,name,leader,mobile,status')->where('org_id',$params['org_id'])->select()->toArray(); return $this->success('请求成功',$data); } + + //获取所有部门 + public function getAllDept(): \think\response\Json + { + $data = Dept::field('id,name,org_id')->select()->each(function($item){ + $org = Orgs::field('name')->where('id',$item['org_id'])->findOrEmpty(); + $item['org_name'] = $org->isEmpty() ? '' : $org['name']; + $item['admin_num'] = Admin::where('dept_id',$item['id'])->count(); + unset($item['org_id']); + return $item; + })->toArray(); + $result = group_by($data, 'org_name'); + return $this->success('请求成功',$result); + } } \ No newline at end of file diff --git a/app/adminapi/controller/dept/JobsController.php b/app/adminapi/controller/dept/JobsController.php index 0d28ec879..807570e75 100755 --- a/app/adminapi/controller/dept/JobsController.php +++ b/app/adminapi/controller/dept/JobsController.php @@ -18,6 +18,7 @@ use app\adminapi\controller\BaseAdminController; use app\adminapi\lists\dept\JobsLists; use app\adminapi\logic\dept\JobsLogic; use app\adminapi\validate\dept\JobsValidate; +use app\common\model\auth\Admin; use app\common\model\dept\Jobs; @@ -109,5 +110,15 @@ class JobsController extends BaseAdminController $data = Jobs::field('id,name,status,sort,create_time')->where('dept_id',$params['dept_id'])->select()->toArray(); return $this->success('请求成功',$data); } + + //获取所有岗位 + public function getAllJobs(): \think\response\Json + { + $data = Jobs::field('id,name')->select()->each(function($item){ + $item['admin_num'] = Admin::where('job_id',$item['id'])->count(); + return $item; + })->toArray(); + return $this->success('请求成功',$data); + } } \ No newline at end of file diff --git a/app/common.php b/app/common.php index 1d9f5349b..218c9c287 100755 --- a/app/common.php +++ b/app/common.php @@ -303,3 +303,12 @@ function project_code(): string { return 'P'.date('Ymd',time()).'-'.mt_rand(100000, 999999); } + +function group_by($array, $key): array +{ + $result = []; + foreach ($array as $item) { + $result[$item[$key]][] = $item; + } + return $result; +}