thinkcmf后台模板layuimini菜单json格式
这两天使用thinkcmf6做后台,但是感觉其官方自带后台模板无法自适应,于是决定使用layuimini做后台模板,下载模板发下菜单是在文件中以json格式存在的,奈与thinkcmf的菜单直接在文件中显示的,不知道怎么能获的json格式菜单,只能自己写一个接口来获取了!
很简单的直接上代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
function getsubmenu($list){ $childs = []; foreach ($list as $vol){ $subs = []; if(isset($vol['items'])){ $subs = $this->getsubmenu($vol['items']); } $icon = $vol['icon'] ? 'fa fa-'.$vol['icon'] : ''; $submenu = [ 'title'=>$vol['name'], 'icon' => $icon, 'href'=>$vol['url'], 'target'=>'_self', ]; if(isset($vol['items'])){ $submenu['child'] = $subs; } $childs[] = $submenu; unset($submenu); } return $childs; } public function index() { $adminId = cmf_get_current_admin_id(); $cmfAdminThemePath = config('template.cmf_admin_theme_path'); $cmfAdminDefaultTheme = config('template.cmf_admin_default_theme'); $adminThemePath = "{$cmfAdminThemePath}{$cmfAdminDefaultTheme}"; $logo = cmf_get_root()."/".$adminThemePath.'/public/assets/themes/'.cmf_get_admin_style().'/images/logo.png'; $menus = cache('admin_menus_' . $adminId); if($menus){ $parent = $childs = $subs = []; foreach ($menus as $key=>$vo){ $parent = $submenu = $childs = []; if(isset($vo['items'])){ $childs = $this->getsubmenu($vo['items']); } $parent = [ 'title'=>$vo['name'], 'icon' => 'fa fa-'.$vo['icon'], 'href'=>'', 'target'=>'_self', 'child'=>$childs ]; $subs[] = $parent; unset($parent); } $items = [ 'homeInfo'=>[ 'title'=>'首页', 'href'=>'' ], 'logoInfo'=>[ 'title'=>'MINI', 'image'=>$logo, 'href'=>'' ], 'menuInfo'=>$subs ]; echo json_encode($items); } } |
随便搞个API接口供后台调用即可,目前官方模板使用此方式正常,特别说明一下,只有登录后台才会有菜单信息,否则将什么也不会显示,因为这里使用的是 $menus = cache('admin_menus_' . $adminId); 只有登录后才有的!
自己更换后台模板再也不是什么难题了