| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382 | 
							- <?php
 
- // +----------------------------------------------------------------------
 
- // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
 
- // +----------------------------------------------------------------------
 
- // | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
 
- // +----------------------------------------------------------------------
 
- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
 
- // +----------------------------------------------------------------------
 
- // | Author: 老猫 <thinkcmf@126.com>
 
- // +----------------------------------------------------------------------
 
- namespace app\admin\logic;
 
- use think\Db;
 
- use think\facade\Env;
 
- use mindplay\annotations\Annotations;
 
- class MenuLogic
 
- {
 
-     /**
 
-      * 导入应用后台菜单
 
-      * @param $app
 
-      * @return array
 
-      * @throws \ReflectionException
 
-      * @throws \think\Exception
 
-      * @throws \think\db\exception\DataNotFoundException
 
-      * @throws \think\db\exception\ModelNotFoundException
 
-      * @throws \think\exception\DbException
 
-      * @throws \think\exception\PDOException
 
-      */
 
-     public static function importMenus($app)
 
-     {
 
-         Annotations::$config['cache']                 = false;
 
-         $annotationManager                            = Annotations::getManager();
 
-         $annotationManager->registry['adminMenu']     = 'app\admin\annotation\AdminMenuAnnotation';
 
-         $annotationManager->registry['adminMenuRoot'] = 'app\admin\annotation\AdminMenuRootAnnotation';
 
-         $newMenus = [];
 
-         if ($app == 'admin') {
 
-             $filePatten         = Env::get('root_path') . "vendor/thinkcmf/cmf-app/src/{$app}/controller/*Controller.php";
 
-             $coreAppControllers = cmf_scan_dir($filePatten);
 
-             $filePatten  = APP_PATH . $app . '/controller/*Controller.php';
 
-             $controllers = cmf_scan_dir($filePatten);
 
-             $controllers = array_merge($coreAppControllers, $controllers);
 
-         } else if ($app == 'user') {
 
-             $filePatten         = Env::get('root_path') . "vendor/thinkcmf/cmf-app/src/{$app}/controller/Admin*Controller.php";
 
-             $coreAppControllers = cmf_scan_dir($filePatten);
 
-             $filePatten  = APP_PATH . $app . '/controller/Admin*Controller.php';
 
-             $controllers = cmf_scan_dir($filePatten);
 
-             $controllers = array_merge($coreAppControllers, $controllers);
 
-         } else {
 
-             $filePatten  = APP_PATH . $app . '/controller/Admin*Controller.php';
 
-             $controllers = cmf_scan_dir($filePatten);
 
-         }
 
-         if (!empty($controllers)) {
 
-             foreach ($controllers as $controller) {
 
-                 $controller      = preg_replace('/\.php$/', '', $controller);
 
-                 $controllerName  = preg_replace('/\Controller$/', '', $controller);
 
-                 $controllerClass = "app\\$app\\controller\\$controller";
 
-                 $menuAnnotations = Annotations::ofClass($controllerClass, '@adminMenuRoot');
 
-                 if (!empty($menuAnnotations)) {
 
-                     foreach ($menuAnnotations as $menuAnnotation) {
 
-                         $name      = $menuAnnotation->name;
 
-                         $icon      = $menuAnnotation->icon;
 
-                         $type      = 0;//1:有界面可访问菜单,2:无界面可访问菜单,0:只作为菜单
 
-                         $action    = $menuAnnotation->action;
 
-                         $status    = empty($menuAnnotation->display) ? 0 : 1;
 
-                         $listOrder = floatval($menuAnnotation->order);
 
-                         $param     = $menuAnnotation->param;
 
-                         $remark    = $menuAnnotation->remark;
 
-                         if (empty($menuAnnotation->parent)) {
 
-                             $parentId = 0;
 
-                         } else {
 
-                             $parent      = explode('/', $menuAnnotation->parent);
 
-                             $countParent = count($parent);
 
-                             if ($countParent > 3) {
 
-                                 throw new \Exception($controllerClass . ':' . $action . '  @adminMenuRoot parent格式不正确!');
 
-                             }
 
-                             $parentApp        = $app;
 
-                             $parentController = $controllerName;
 
-                             $parentAction     = '';
 
-                             switch ($countParent) {
 
-                                 case 1:
 
-                                     $parentAction = $parent[0];
 
-                                     break;
 
-                                 case 2:
 
-                                     $parentController = $parent[0];
 
-                                     $parentAction     = $parent[1];
 
-                                     break;
 
-                                 case 3:
 
-                                     $parentApp        = $parent[0];
 
-                                     $parentController = $parent[1];
 
-                                     $parentAction     = $parent[2];
 
-                                     break;
 
-                             }
 
-                             $findParentAdminMenu = Db::name('admin_menu')->where([
 
-                                 'app'        => $parentApp,
 
-                                 'controller' => $parentController,
 
-                                 'action'     => $parentAction
 
-                             ])->find();
 
-                             if (empty($findParentAdminMenu)) {
 
-                                 $parentId = Db::name('admin_menu')->insertGetId([
 
-                                     'app'        => $parentApp,
 
-                                     'controller' => $parentController,
 
-                                     'action'     => $parentAction,
 
-                                     'name'       => '--new--'
 
-                                 ]);
 
-                             } else {
 
-                                 $parentId = $findParentAdminMenu['id'];
 
-                             }
 
-                         }
 
-                         $findAdminMenu = Db::name('admin_menu')->where([
 
-                             'app'        => $app,
 
-                             'controller' => $controllerName,
 
-                             'action'     => $action
 
-                         ])->find();
 
-                         if (empty($findAdminMenu)) {
 
-                             Db::name('admin_menu')->insert([
 
-                                 'parent_id'  => $parentId,
 
-                                 'type'       => $type,
 
-                                 'status'     => $status,
 
-                                 'list_order' => $listOrder,
 
-                                 'app'        => $app,
 
-                                 'controller' => $controllerName,
 
-                                 'action'     => $action,
 
-                                 'param'      => $param,
 
-                                 'name'       => $name,
 
-                                 'icon'       => $icon,
 
-                                 'remark'     => $remark
 
-                             ]);
 
-                             $menuName = $name;
 
-                             array_push($newMenus, "$app/$controllerName/$action 已导入");
 
-                         } else {
 
-                             if ($findAdminMenu['name'] == '--new--') {
 
-                                 Db::name('admin_menu')->where([
 
-                                     'app'        => $app,
 
-                                     'controller' => $controllerName,
 
-                                     'action'     => $action
 
-                                 ])->update([
 
-                                     'parent_id'  => $parentId,
 
-                                     'type'       => $type,
 
-                                     'status'     => $status,
 
-                                     'list_order' => $listOrder,
 
-                                     'param'      => $param,
 
-                                     'name'       => $name,
 
-                                     'icon'       => $icon,
 
-                                     'remark'     => $remark
 
-                                 ]);
 
-                                 $menuName = $name;
 
-                             } else {
 
-                                 // 只关注菜单层级关系,是否有视图
 
-                                 Db::name('admin_menu')->where([
 
-                                     'app'        => $app,
 
-                                     'controller' => $controllerName,
 
-                                     'action'     => $action
 
-                                 ])->update([
 
-                                     //'parent_id' => $parentId,
 
-                                     'type' => $type,
 
-                                 ]);
 
-                                 $menuName = $findAdminMenu['name'];
 
-                             }
 
-                             array_push($newMenus, "$app/$controllerName/$action 层级关系已更新");
 
-                         }
 
-                         $authRuleName      = "{$app}/{$controllerName}/{$action}";
 
-                         $findAuthRuleCount = Db::name('auth_rule')->where([
 
-                             'app'  => $app,
 
-                             'name' => $authRuleName,
 
-                             'type' => 'admin_url'
 
-                         ])->count();
 
-                         if ($findAuthRuleCount == 0) {
 
-                             Db::name('auth_rule')->insert([
 
-                                 'app'   => $app,
 
-                                 'name'  => $authRuleName,
 
-                                 'type'  => 'admin_url',
 
-                                 'param' => $param,
 
-                                 'title' => $menuName
 
-                             ]);
 
-                         } else {
 
-                             Db::name('auth_rule')->where([
 
-                                 'app'  => $app,
 
-                                 'name' => $authRuleName,
 
-                                 'type' => 'admin_url',
 
-                             ])->update([
 
-                                 'param' => $param,
 
-                                 'title' => $menuName
 
-                             ]);
 
-                         }
 
-                     }
 
-                 }
 
-                 $reflect = new \ReflectionClass($controllerClass);
 
-                 $methods = $reflect->getMethods(\ReflectionMethod::IS_PUBLIC);
 
-                 if (!empty($methods)) {
 
-                     foreach ($methods as $method) {
 
-                         if ($method->class == $controllerClass && strpos($method->name, '_') !== 0) {
 
-                             $menuAnnotations = Annotations::ofMethod($controllerClass, $method->name, '@adminMenu');
 
-                             if (!empty($menuAnnotations)) {
 
-                                 $menuAnnotation = $menuAnnotations[0];
 
-                                 $name      = $menuAnnotation->name;
 
-                                 $icon      = $menuAnnotation->icon;
 
-                                 $type      = $menuAnnotation->hasView ? 1 : 2;//1:有界面可访问菜单,2:无界面可访问菜单,0:只作为菜单
 
-                                 $action    = $method->name;
 
-                                 $status    = empty($menuAnnotation->display) ? 0 : 1;
 
-                                 $listOrder = floatval($menuAnnotation->order);
 
-                                 $param     = $menuAnnotation->param;
 
-                                 $remark    = $menuAnnotation->remark;
 
-                                 if (empty($menuAnnotation->parent)) {
 
-                                     $parentId = 0;
 
-                                 } else {
 
-                                     $parent      = explode('/', $menuAnnotation->parent);
 
-                                     $countParent = count($parent);
 
-                                     if ($countParent > 3) {
 
-                                         throw new \Exception($controllerClass . ':' . $action . '  @menuRoot parent格式不正确!');
 
-                                     }
 
-                                     $parentApp        = $app;
 
-                                     $parentController = $controllerName;
 
-                                     $parentAction     = '';
 
-                                     switch ($countParent) {
 
-                                         case 1:
 
-                                             $parentAction = $parent[0];
 
-                                             break;
 
-                                         case 2:
 
-                                             $parentController = $parent[0];
 
-                                             $parentAction     = $parent[1];
 
-                                             break;
 
-                                         case 3:
 
-                                             $parentApp        = $parent[0];
 
-                                             $parentController = $parent[1];
 
-                                             $parentAction     = $parent[2];
 
-                                             break;
 
-                                     }
 
-                                     $findParentAdminMenu = Db::name('admin_menu')->where([
 
-                                         'app'        => $parentApp,
 
-                                         'controller' => $parentController,
 
-                                         'action'     => $parentAction
 
-                                     ])->find();
 
-                                     if (empty($findParentAdminMenu)) {
 
-                                         $parentId = Db::name('admin_menu')->insertGetId([
 
-                                             'app'        => $parentApp,
 
-                                             'controller' => $parentController,
 
-                                             'action'     => $parentAction,
 
-                                             'name'       => '--new--'
 
-                                         ]);
 
-                                     } else {
 
-                                         $parentId = $findParentAdminMenu['id'];
 
-                                     }
 
-                                 }
 
-                                 $findAdminMenu = Db::name('admin_menu')->where([
 
-                                     'app'        => $app,
 
-                                     'controller' => $controllerName,
 
-                                     'action'     => $action
 
-                                 ])->find();
 
-                                 if (empty($findAdminMenu)) {
 
-                                     Db::name('admin_menu')->insert([
 
-                                         'parent_id'  => $parentId,
 
-                                         'type'       => $type,
 
-                                         'status'     => $status,
 
-                                         'list_order' => $listOrder,
 
-                                         'app'        => $app,
 
-                                         'controller' => $controllerName,
 
-                                         'action'     => $action,
 
-                                         'param'      => $param,
 
-                                         'name'       => $name,
 
-                                         'icon'       => $icon,
 
-                                         'remark'     => $remark
 
-                                     ]);
 
-                                     $menuName = $name;
 
-                                     array_push($newMenus, "$app/$controllerName/$action 已导入");
 
-                                 } else {
 
-                                     if ($findAdminMenu['name'] == '--new--') {
 
-                                         Db::name('admin_menu')->where([
 
-                                             'app'        => $app,
 
-                                             'controller' => $controllerName,
 
-                                             'action'     => $action
 
-                                         ])->update([
 
-                                             'parent_id'  => $parentId,
 
-                                             'type'       => $type,
 
-                                             'status'     => $status,
 
-                                             'list_order' => $listOrder,
 
-                                             'param'      => $param,
 
-                                             'name'       => $name,
 
-                                             'icon'       => $icon,
 
-                                             'remark'     => $remark
 
-                                         ]);
 
-                                         $menuName = $name;
 
-                                     } else {
 
-                                         // 只关注菜单层级关系,是否有视图
 
-                                         Db::name('admin_menu')->where([
 
-                                             'app'        => $app,
 
-                                             'controller' => $controllerName,
 
-                                             'action'     => $action
 
-                                         ])->update([
 
-                                             //'parent_id' => $parentId,
 
-                                             'type' => $type,
 
-                                         ]);
 
-                                         $menuName = $findAdminMenu['name'];
 
-                                     }
 
-                                     array_push($newMenus, "$app/$controllerName/$action 已更新");
 
-                                 }
 
-                                 $authRuleName      = "{$app}/{$controllerName}/{$action}";
 
-                                 $findAuthRuleCount = Db::name('auth_rule')->where([
 
-                                     'app'  => $app,
 
-                                     'name' => $authRuleName,
 
-                                     'type' => 'admin_url'
 
-                                 ])->count();
 
-                                 if ($findAuthRuleCount == 0) {
 
-                                     Db::name('auth_rule')->insert([
 
-                                         'app'   => $app,
 
-                                         'name'  => $authRuleName,
 
-                                         'type'  => 'admin_url',
 
-                                         'param' => $param,
 
-                                         'title' => $menuName
 
-                                     ]);
 
-                                 } else {
 
-                                     Db::name('auth_rule')->where([
 
-                                         'app'  => $app,
 
-                                         'name' => $authRuleName,
 
-                                         'type' => 'admin_url',
 
-                                     ])->update([
 
-                                         'param' => $param,
 
-                                         'title' => $menuName
 
-                                     ]);
 
-                                 }
 
-                             }
 
-                         }
 
-                     }
 
-                 }
 
-             }
 
-         }
 
-         return $newMenus;
 
-     }
 
- }
 
 
  |