| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 | 
							- <?php
 
- namespace app\admin\controller;
 
- use app\admin\middleware\Login;
 
- use app\Request;
 
- class Menu extends Base
 
- {
 
-     /**
 
-      * 中间件校验
 
-      * @var array[]
 
-      */
 
-     protected $middleware = [
 
-         Login::class => ['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;
 
-     }
 
- }
 
 
  |