From aa921d7d7600691e7ee833dceb8c157d1c371967 Mon Sep 17 00:00:00 2001 From: jsasg <735273025@qq.com> Date: Mon, 3 Mar 2025 17:53:20 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BF=AE=E6=94=B9=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E8=8F=9C=E5=8D=95=E6=9D=83=E9=99=90=E8=BE=93=E5=87=BA?= =?UTF-8?q?=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/admin/common.php | 9 ++++++--- app/admin/controller/v1/User.php | 18 ++++++++++++------ 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/app/admin/common.php b/app/admin/common.php index 7ecd1a55..ed6d4c0f 100644 --- a/app/admin/common.php +++ b/app/admin/common.php @@ -10,13 +10,16 @@ if (!function_exists('array_to_tree')) { * @param int $level 层级 * @return array */ - function array_to_tree(array $data, int $pid, string $with = 'pid', int $level = 1) + function array_to_tree(array $data, int $pid, string $with = 'pid', int|false $level = 1) { $ret = []; foreach ($data as $item) { if ($item[$with] == $pid) { - $item['level'] = $level; - $children = array_to_tree($data, $item['id'], $with, $level + 1); + if ($level !== false) { + $item['level'] = $level; + $level = $level + 1; + } + $children = array_to_tree($data, $item['id'], $with, $level); if ($children) { $item['children'] = $children; } diff --git a/app/admin/controller/v1/User.php b/app/admin/controller/v1/User.php index bae5b4c0..205538e3 100644 --- a/app/admin/controller/v1/User.php +++ b/app/admin/controller/v1/User.php @@ -58,19 +58,25 @@ class User } if (!empty($authoritys_map)) { $menus->each(function($item) use($authoritys_map) { - $item['hidden'] = !!$item['hidden']; - $item['actived'] = !!$item['actived']; - $item['keep_alive'] = !!$item['keep_alive']; - $item['permissions'] = []; + $meta = [ + 'title' => $item['title'], + 'icon' => $item['icon'], + 'isKeepAlive' => !!$item['keep_alive'], + 'actived' => !!$item['actived'], + 'permissions' => [] + ]; + unset($item['title'], $item['icon'], $item['keep_alive'], $item['actived']); if (isset($authoritys_map[$item['id']])) { - $item['permissions'] = $authoritys_map[$item['id']]; + $meta['permissions'] = $authoritys_map[$item['id']]; } + $item['hidden'] = !!$item['hidden']; + $item['meta'] = $meta; return $item; }); } } - return success('获取成功', array_to_tree($menus->toArray(), 0)); + return success('获取成功', array_to_tree($menus->toArray(), 0, 'pid', false)); } // 用户分页数据