installMenu(); return true; } /** * 菜单安装 */ public function installMenu() { $sys_menu = new SysMenu(); //平台菜单 $admin_menus = $this->loadMenu(AppTypeEnum::ADMIN); $site_menus = $this->loadMenu(AppTypeEnum::SITE); $menus = array_merge($admin_menus, $site_menus); $sys_menu->where([['id', '>', 0]])->delete(); $sys_menu->replace()->insertAll($menus); // 清除缓存 Cache::tag(MenuService::$cache_tag_name)->clear(); return true; } /** * 加载菜单 * @return array */ public function loadMenu($app_type) { //加载系统 $system_tree = include root_path().str_replace('/', DIRECTORY_SEPARATOR, "app/enum/install/menu/system/".$app_type.".php"); //加载扩展插件菜单 $config_util = new ConfigUtil(root_path().str_replace('/', DIRECTORY_SEPARATOR, "app/enum/install/menu/plugin/".$app_type), $system_tree); $tree = $config_util->loadConfig(); $this->menuTreeToList($tree, '',$app_type); $menu_list = $this->menu_list; $this->menu_list = []; return $menu_list; } /** * 菜单数转为列表 * @param array $tree * @param string $parent_key * @param string $app_type */ private function menuTreeToList(array $tree, string $parent_key = '', string $app_type = AppTypeEnum::ADMIN) { if (is_array($tree)) { foreach ($tree as $key => $value) { $item = [ 'menu_name' => $value['menu_name'], 'menu_key' => $value['menu_key'], 'app_type' => $app_type, 'parent_key' => $value['parent_key'] ?? $parent_key, 'menu_type' => $value['menu_type'], 'icon' => $value['icon'] ?? '', 'api_url' => $value['api_url'] ?? '', 'router_path' => $value['router_path'] ?? '', 'view_path' => $value['view_path'] ?? '', 'methods' => $value['methods'] ?? '', 'sort' => $value['sort'] ?? '', 'status' => 1, 'is_show' => $value['is_show'] ?? 1, 'en_menu_name' => $value['en_menu_name'] ?? '', ]; $refer = $value; if (isset($refer[ 'children' ])) { unset($refer[ 'children' ]); array_push($this->menu_list, $item); $p_key = $refer[ 'menu_key' ]; $this->menuTreeToList($value[ 'children' ],$p_key, $app_type); } else { array_push($this->menu_list, $item); } } } } }