perf: 优化会员搜索接口

This commit is contained in:
kuaifan 2025-07-25 16:11:10 +08:00
parent 0eba0c6a4b
commit 8157c27529
2 changed files with 53 additions and 1 deletions

View File

@ -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);

View File

@ -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();
}
}