From 8157c275293400c35225360e1d5ef67740ef2536 Mon Sep 17 00:00:00 2001 From: kuaifan Date: Fri, 25 Jul 2025 16:11:10 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=E4=BC=9A=E5=91=98?= =?UTF-8?q?=E6=90=9C=E7=B4=A2=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Controllers/Api/UsersController.php | 10 ++++- app/Models/UserDepartment.php | 44 ++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/app/Http/Controllers/Api/UsersController.php b/app/Http/Controllers/Api/UsersController.php index f7cf0538d..1a6b1440e 100755 --- a/app/Http/Controllers/Api/UsersController.php +++ b/app/Http/Controllers/Api/UsersController.php @@ -545,12 +545,16 @@ class UsersController extends AbstractController * - keys.project_id 在指定项目ID * - keys.no_project_id 不在指定项目ID * - keys.dialog_id 在指定对话ID + * - keys.departments 部门ID(多个用逗号分隔) * @apiParam {Object} sorts 排序方式 * - sorts.az 按字母:asc|desc * @apiParam {Number} updated_time 在这个时间戳之后更新的 * @apiParam {Number} state 获取在线状态 * - 0: 不获取(默认) * - 1: 获取会员在线状态,返回数据多一个online值 + * @apiParam {Number} [with_department] 是否返回部门信息 + * - 0: 不返回部门信息(默认) + * - 1: 返回部门信息(返回数据多一个department_info字段),department_info={id, name, parent_id, owner_userid} * * @apiParam {Number} [take] 获取数量,10-100 * @apiParam {Number} [page] 当前页,默认:1(赋值分页模式,take参数无效) @@ -573,6 +577,7 @@ class UsersController extends AbstractController $sorts = Request::input('sorts'); $updatedTime = intval(Request::input('updated_time')); $state = intval(Request::input('state', 0)); + $withDepartment = intval(Request::input('with_department', 0)); $keys = is_array($keys) ? $keys : []; $sorts = is_array($sorts) ? $sorts : []; // @@ -644,7 +649,7 @@ class UsersController extends AbstractController $list = $builder->orderBy('userid')->take(Base::getPaginate(100, 10, 'take'))->get(); } // - $list->transform(function (User $userInfo) use ($user, $state) { + $list->transform(function (User $userInfo) use ($user, $state, $withDepartment) { $tags = []; $dep = $userInfo->getDepartmentName(); $dep = array_values(array_filter(explode(",", $dep), function($item) { @@ -669,6 +674,9 @@ class UsersController extends AbstractController if ($state === 1) { $userInfo->online = $userInfo->getOnlineStatus(); } + if ($withDepartment) { + $userInfo->department_info = UserDepartment::getDepartmentsByIds($userInfo->department); + } return $userInfo; }); return Base::retSuccess('success', $list); diff --git a/app/Models/UserDepartment.php b/app/Models/UserDepartment.php index 319f6bed2..2b386e045 100644 --- a/app/Models/UserDepartment.php +++ b/app/Models/UserDepartment.php @@ -3,6 +3,7 @@ namespace App\Models; use App\Exceptions\ApiException; +use Cache; /** * App\Models\UserDepartment @@ -168,4 +169,47 @@ class UserDepartment extends AbstractModel } }); } + + /** + * 获取部门基本信息(缓存时间1小时) + * @param int|array $ids + * @return \Illuminate\Support\Collection|static|null + */ + public static function getDepartmentsByIds($ids) + { + $ids = is_array($ids) ? $ids : [$ids]; + $departments = collect(); + $uncachedIds = []; + + foreach ($ids as $id) { + $cacheKey = "department_info_{$id}"; + $department = Cache::get($cacheKey); + if ($department) { + $departments->push($department); + } else { + $uncachedIds[] = $id; + } + } + + if (!empty($uncachedIds)) { + $dbDepartments = self::select(['id', 'name', 'parent_id', 'owner_userid'])->whereIn('id', $uncachedIds)->get(); + foreach ($dbDepartments as $department) { + $cacheKey = "department_info_{$department->id}"; + Cache::put($cacheKey, $department, 60 * 60); // 1小时 + $departments->push($department); + } + } + + // 保持返回顺序与传入ids一致 + $departments = $departments->keyBy('id'); + $result = collect(); + foreach ($ids as $id) { + if ($departments->has($id)) { + $result->push($departments->get($id)); + } + } + + return is_array($ids) ? $result : $result->first(); + } + }