Browse Source

部门用户列表

kely 9 months ago
parent
commit
d6202b5b4b
3 changed files with 17 additions and 1 deletions
  1. 11 1
      app/Http/Controllers/API/DepartmentController.php
  2. 4 0
      app/Models/Department.php
  3. 2 0
      routes/api.php

+ 11 - 1
app/Http/Controllers/API/DepartmentController.php

@@ -48,7 +48,6 @@ class DepartmentController extends Controller
     }
 
 
-
     public function update(CreateOrUpdateRequest $request,string $id)
     {
         $department=Department::findOrFail($id);
@@ -70,4 +69,15 @@ class DepartmentController extends Controller
 
         return $this->noContent();
     }
+
+    public function departmentUserIndex(){
+        $companyId=Auth::user()->company_id;
+        $department=Department::query()->where('company_id',$companyId)->with(['users'=>function($query){
+            $query->select('id','name','department_id');
+        }])->get(['id','name','parent_id']);
+        return $this->success([
+            'data' => make_tree($department->toArray())
+        ]);
+
+    }
 }

+ 4 - 0
app/Models/Department.php

@@ -32,4 +32,8 @@ class Department extends Model
     {
         return $this->hasMany(Department::class, 'parent_id');
     }
+
+    public function users(){
+        return $this->hasMany(User::class, 'department_id');
+    }
 }

+ 2 - 0
routes/api.php

@@ -195,5 +195,7 @@ Route::middleware(['auth:sanctum'])->group(function () {
             ->name("share-file.destroy");
         Route::get("share-file/{object_type}/{object_id}", [API\ShareFileController::class, "byObject"])
             ->name("share-file.by-object");
+
+        Route::get("department-user-index",[API\DepartmentController::class,"departmentUserIndex"])->name("department.user-index");;
     });
 });