['except' => ['']], ]; /** * 获取菜单 * @param Request $request */ public function list(Request $request) { $menuJson = file_get_contents(root_path() . "config/json/menu.json"); $menu = json_decode($menuJson, true); $role_codes = []; $role_codes[] = 'home'; $role_codes[] = 'admin'; $fin_menu = $this->getFinMenu($menu, $role_codes,1); $result = [ 'menu' => $fin_menu, 'role' => $role_codes, ]; return $this->success($result); } /** * @param $menu_list * @param $role_codes * @param int $is_root * @return array */ public function getFinMenu($menu_list, $role_codes, int $is_root = 0): array { $fin_menu = []; foreach ($menu_list as $key => $val) { if (isset($val['children'])) { $child_list = $this->getChild($val['children']); $parents = false; foreach ($child_list as $kes => $ves) { if (in_array($ves['role'], $role_codes) || $is_root) { $parents = true; break; } } if (in_array($val['role'], $role_codes) || $parents) { $item = $val; $item['children'] = $this->getFinMenu($val['children'], $role_codes, $is_root); $fin_menu[] = $item; } } else { if (isset($val['role'])) { if (in_array($val['role'], $role_codes) || $is_root || $val['role'] == 'index') { $fin_menu[] = $val; } } } } return $fin_menu; } /** * 获取所有子节点 * @param array $arrData * @param string $strChild * @return array|mixed */ public function getChild(array $arrData = [], string $strChild = "children"): mixed { if (empty($arrData) || !is_array($arrData)) { return $arrData; } $arrRes = []; foreach ($arrData as $k => $v) { $arrTmp = $v; if (isset($arrTmp[$strChild])) { unset($arrTmp[$strChild]); } $arrRes[] = $arrTmp; if (isset($v[$strChild])) { if (!empty($v[$strChild])) { $arrTmp = $this->getChild($v[$strChild]); $arrRes = array_merge($arrRes, $arrTmp); } } } return $arrRes; } }