refactor: 修改用户菜单权限输出结构

This commit is contained in:
2025-03-03 17:53:20 +08:00
parent 0d5f5448ae
commit aa921d7d76
2 changed files with 18 additions and 9 deletions

View File

@@ -10,13 +10,16 @@ if (!function_exists('array_to_tree')) {
* @param int $level 层级 * @param int $level 层级
* @return array * @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 = []; $ret = [];
foreach ($data as $item) { foreach ($data as $item) {
if ($item[$with] == $pid) { if ($item[$with] == $pid) {
$item['level'] = $level; if ($level !== false) {
$children = array_to_tree($data, $item['id'], $with, $level + 1); $item['level'] = $level;
$level = $level + 1;
}
$children = array_to_tree($data, $item['id'], $with, $level);
if ($children) { if ($children) {
$item['children'] = $children; $item['children'] = $children;
} }

View File

@@ -58,19 +58,25 @@ class User
} }
if (!empty($authoritys_map)) { if (!empty($authoritys_map)) {
$menus->each(function($item) use($authoritys_map) { $menus->each(function($item) use($authoritys_map) {
$item['hidden'] = !!$item['hidden']; $meta = [
$item['actived'] = !!$item['actived']; 'title' => $item['title'],
$item['keep_alive'] = !!$item['keep_alive']; 'icon' => $item['icon'],
$item['permissions'] = []; 'isKeepAlive' => !!$item['keep_alive'],
'actived' => !!$item['actived'],
'permissions' => []
];
unset($item['title'], $item['icon'], $item['keep_alive'], $item['actived']);
if (isset($authoritys_map[$item['id']])) { 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 $item;
}); });
} }
} }
return success('获取成功', array_to_tree($menus->toArray(), 0)); return success('获取成功', array_to_tree($menus->toArray(), 0, 'pid', false));
} }
// 用户分页数据 // 用户分页数据