From d21adf60042c61e4b063d53b0da8baf28b97cfb4 Mon Sep 17 00:00:00 2001 From: kuaifan Date: Mon, 14 Jul 2025 22:29:50 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E8=8E=B7=E5=8F=96=E6=88=91=E7=9A=84?= =?UTF-8?q?=E9=83=A8=E9=97=A8=E5=88=97=E8=A1=A8=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 | 45 ++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/app/Http/Controllers/Api/UsersController.php b/app/Http/Controllers/Api/UsersController.php index 822b8de74..d5314c8a4 100755 --- a/app/Http/Controllers/Api/UsersController.php +++ b/app/Http/Controllers/Api/UsersController.php @@ -352,6 +352,51 @@ class UsersController extends AbstractController return Base::retSuccess('success', $data); } + /** + * @api {get} api/users/info/departments 08. 获取我的部门列表 + * + * @apiDescription 需要token身份 + * @apiVersion 1.0.0 + * @apiGroup users + * @apiName info__departments + * + * @apiSuccess {Number} ret 返回状态码(1正确、0错误) + * @apiSuccess {String} msg 返回信息(错误描述) + * @apiSuccess {Object} data 返回数据 + * @apiSuccessExample {json} data: + [ + { + "id": 1, + "name": "部门1", + "parent_id": 0, + "owner_userid": 1 + }, + ] + */ + public function info__departments() + { + $user = User::auth(); + + // 获取部门列表 + $list = UserDepartment::select(['id', 'owner_userid', 'parent_id', 'name']) + ->whereIn('id', $user->department) + ->take(10) + ->get() + ->toArray(); + + // 将 owner_userid 等于当前用户的部门排在前面 + usort($list, function($a, $b) use ($user) { + if ($a['owner_userid'] == $user->userid && $b['owner_userid'] != $user->userid) { + return -1; + } elseif ($a['owner_userid'] != $user->userid && $b['owner_userid'] == $user->userid) { + return 1; + } + return 0; + }); + + return Base::retSuccess('success', $list); + } + /** * @api {get} api/users/editdata 09. 修改自己的资料 *