From 5450777bd7b1182aada5651dad69f623f84142c3 Mon Sep 17 00:00:00 2001 From: molong Date: Wed, 18 Feb 2026 16:17:11 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E7=94=A8=E6=88=B7=E6=9D=83?= =?UTF-8?q?=E9=99=90=E6=A8=A1=E5=9D=97=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Services/Auth/DepartmentService.php | 36 ++++ app/Services/Auth/UserService.php | 37 +++- .../roles/components/PermissionDialog.vue | 6 +- .../auth/users/components/BatchRoleDialog.vue | 104 +++++++++++ .../users/components/DepartmentDialog.vue | 101 ++++++++++ .../auth/users/components/RoleDialog.vue | 173 +++++++++--------- .../admin/src/pages/auth/users/index.vue | 43 ++++- 7 files changed, 404 insertions(+), 96 deletions(-) create mode 100644 resources/admin/src/pages/auth/users/components/BatchRoleDialog.vue create mode 100644 resources/admin/src/pages/auth/users/components/DepartmentDialog.vue diff --git a/app/Services/Auth/DepartmentService.php b/app/Services/Auth/DepartmentService.php index d0153c0..75c648b 100644 --- a/app/Services/Auth/DepartmentService.php +++ b/app/Services/Auth/DepartmentService.php @@ -324,4 +324,40 @@ class DepartmentService } return $this->isDescendant($id, $child->parent_id); } + + /** + * 获取部门及所有子部门的ID列表 + * + * @param int $departmentId 部门ID + * @param array $departments 所有部门数据 + * @return array + */ + public function getDepartmentAndChildrenIds(int $departmentId, array $departments = null): array + { + if ($departments === null) { + $departments = Department::where('status', 1)->get()->keyBy('id')->toArray(); + } + + $ids = [$departmentId]; + $this->collectChildrenIds($departmentId, $departments, $ids); + + return $ids; + } + + /** + * 递归收集子部门ID + * + * @param int $parentId 父部门ID + * @param array $departments 所有部门数据 + * @param array &$ids ID收集数组 + */ + private function collectChildrenIds(int $parentId, array $departments, array &$ids): void + { + foreach ($departments as $department) { + if ($department['parent_id'] == $parentId) { + $ids[] = $department['id']; + $this->collectChildrenIds($department['id'], $departments, $ids); + } + } + } } diff --git a/app/Services/Auth/UserService.php b/app/Services/Auth/UserService.php index ae44bd1..a1335b2 100644 --- a/app/Services/Auth/UserService.php +++ b/app/Services/Auth/UserService.php @@ -3,6 +3,7 @@ namespace App\Services\Auth; use App\Models\Auth\User; +use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Hash; use Illuminate\Validation\ValidationException; @@ -14,6 +15,20 @@ use App\Jobs\Auth\UserExportJob; class UserService { + protected $departmentService; + + public function __construct(DepartmentService $departmentService) + { + $this->departmentService = $departmentService; + } + + /** + * 获取当前登录用户ID + */ + protected function getCurrentUserId(): int + { + return Auth::guard('admin')->id(); + } /** * 获取用户列表 */ @@ -32,7 +47,9 @@ class UserService } if (!empty($params['department_id'])) { - $query->where('department_id', $params['department_id']); + // 获取部门及所有子部门的ID + $departmentIds = $this->departmentService->getDepartmentAndChildrenIds($params['department_id']); + $query->whereIn('department_id', $departmentIds); } if (isset($params['status']) && $params['status'] !== '') { @@ -226,6 +243,15 @@ class UserService */ public function batchDelete(array $ids): int { + $currentUserId = $this->getCurrentUserId(); + + // 检查是否包含当前用户 + if (in_array($currentUserId, $ids)) { + throw ValidationException::withMessages([ + 'ids' => ['不能删除当前登录用户'], + ]); + } + return User::whereIn('id', $ids)->delete(); } @@ -234,6 +260,15 @@ class UserService */ public function batchUpdateStatus(array $ids, int $status): int { + $currentUserId = $this->getCurrentUserId(); + + // 如果是禁用操作,检查是否包含当前用户 + if ($status === 0 && in_array($currentUserId, $ids)) { + throw ValidationException::withMessages([ + 'ids' => ['不能禁用当前登录用户'], + ]); + } + return User::whereIn('id', $ids)->update(['status' => $status]); } diff --git a/resources/admin/src/pages/auth/roles/components/PermissionDialog.vue b/resources/admin/src/pages/auth/roles/components/PermissionDialog.vue index 4c58c75..221b1ad 100644 --- a/resources/admin/src/pages/auth/roles/components/PermissionDialog.vue +++ b/resources/admin/src/pages/auth/roles/components/PermissionDialog.vue @@ -5,8 +5,8 @@ -