installMenu(); return true; } /** * 菜单安装 */ public function installMenu() { $sys_menu = new SysMenu(); //系统菜单 $admin_menus = $this->loadMenu(AppTypeDict::ADMIN); $site_menus = $this->loadMenu(AppTypeDict::SITE); $menus = array_merge($admin_menus, $site_menus); Db::name("sys_menu")->where([ [ 'addon', '=', '' ], ['source', '=', MenuDict::SYSTEM] ])->delete(); $sys_menu->replace()->insertAll($menus); //插件菜单 (new CoreMenuService())->refreshAllAddonMenu(); // 清除缓存 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/dict/menu/" . $app_type . ".php"); $this->menuTreeToList($system_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 = AppTypeDict::ADMIN) { if (is_array($tree)) { foreach ($tree as $key => $value) { $item = [ 'menu_name' => $value[ 'menu_name' ], 'menu_short_name' => $value[ 'menu_short_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 ]; $refer = $value; if (isset($refer[ 'children' ])) { unset($refer[ 'children' ]); $this->menu_list[] = $item; $p_key = $refer[ 'menu_key' ]; $this->menuTreeToList($value[ 'children' ], $p_key, $app_type); } else { $this->menu_list[] = $item; } } } } }