mirror of
https://github.com/kuaifan/dootask.git
synced 2025-12-11 10:33:54 +00:00
perf: 优化会员搜索接口
This commit is contained in:
parent
0eba0c6a4b
commit
8157c27529
@ -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);
|
||||
|
||||
@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user