MenuLogic.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-present http://www.thinkcmf.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 老猫 <thinkcmf@126.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\logic;
  12. use app\admin\model\AdminMenuModel;
  13. use app\admin\model\AuthRuleModel;
  14. use think\facade\Env;
  15. use mindplay\annotations\Annotations;
  16. class MenuLogic
  17. {
  18. /**
  19. * 导入应用后台菜单
  20. * @param $app
  21. * @return array
  22. * @throws \ReflectionException
  23. * @throws \think\Exception
  24. * @throws \think\db\exception\DataNotFoundException
  25. * @throws \think\db\exception\ModelNotFoundException
  26. * @throws \think\exception\DbException
  27. * @throws \think\exception\PDOException
  28. */
  29. public static function importMenus($app)
  30. {
  31. Annotations::$config['cache'] = false;
  32. $annotationManager = Annotations::getManager();
  33. $annotationManager->registry['adminMenu'] = 'app\admin\annotation\AdminMenuAnnotation';
  34. $annotationManager->registry['adminMenuRoot'] = 'app\admin\annotation\AdminMenuRootAnnotation';
  35. $registry = config('annotation.registry');
  36. if (empty($registry)) {
  37. $registry = ['date', 'email'];
  38. }
  39. foreach ($registry as $key => $value) {
  40. if (is_numeric($key)) {
  41. $annotationManager->registry[$value] = false;
  42. } else {
  43. $annotationManager->registry[$key] = $value;
  44. }
  45. }
  46. $newMenus = [];
  47. if ($app == 'admin') {
  48. $filePatten = CMF_ROOT . "vendor/thinkcmf/cmf-app/src/{$app}/controller/*Controller.php";
  49. $coreAppControllers = cmf_scan_dir($filePatten);
  50. $filePatten = CMF_ROOT . "vendor/thinkcmf/cmf-appstore/src/controller/*Controller.php";
  51. $appStoreControllers = cmf_scan_dir($filePatten);
  52. $filePatten = app_path() . $app . '/controller/*Controller.php';
  53. $controllers = cmf_scan_dir($filePatten);
  54. $controllers = array_merge($coreAppControllers, $appStoreControllers, $controllers);
  55. } else if ($app == 'user') {
  56. $filePatten = CMF_ROOT . "vendor/thinkcmf/cmf-app/src/{$app}/controller/Admin*Controller.php";
  57. $coreAppControllers = cmf_scan_dir($filePatten);
  58. $filePatten = app_path() . $app . '/controller/Admin*Controller.php';
  59. $controllers = cmf_scan_dir($filePatten);
  60. $controllers = array_merge($coreAppControllers, $controllers);
  61. } else {
  62. $filePatten = app_path() . $app . '/controller/Admin*Controller.php';
  63. $controllers = cmf_scan_dir($filePatten);
  64. }
  65. if (!empty($controllers)) {
  66. foreach ($controllers as $controller) {
  67. $controller = preg_replace('/\.php$/', '', $controller);
  68. $controllerName = preg_replace("/Controller$/", '', $controller);
  69. $controllerClass = "app\\$app\\controller\\$controller";
  70. $menuAnnotations = Annotations::ofClass($controllerClass, '@adminMenuRoot');
  71. if (!empty($menuAnnotations)) {
  72. foreach ($menuAnnotations as $menuAnnotation) {
  73. $name = $menuAnnotation->name;
  74. $icon = $menuAnnotation->icon;
  75. $type = 0;//1:有界面可访问菜单,2:无界面可访问菜单,0:只作为菜单
  76. $action = $menuAnnotation->action;
  77. $status = empty($menuAnnotation->display) ? 0 : 1;
  78. $listOrder = floatval($menuAnnotation->order);
  79. $param = $menuAnnotation->param;
  80. $remark = $menuAnnotation->remark;
  81. if (empty($menuAnnotation->parent)) {
  82. $parentId = 0;
  83. } else {
  84. $parent = explode('/', $menuAnnotation->parent);
  85. $countParent = count($parent);
  86. if ($countParent > 3) {
  87. throw new \Exception($controllerClass . ':' . $action . ' @adminMenuRoot parent格式不正确!');
  88. }
  89. $parentApp = $app;
  90. $parentController = $controllerName;
  91. $parentAction = '';
  92. switch ($countParent) {
  93. case 1:
  94. $parentAction = $parent[0];
  95. break;
  96. case 2:
  97. $parentController = $parent[0];
  98. $parentAction = $parent[1];
  99. break;
  100. case 3:
  101. $parentApp = $parent[0];
  102. $parentController = $parent[1];
  103. $parentAction = $parent[2];
  104. break;
  105. }
  106. $findParentAdminMenu = AdminMenuModel::where([
  107. 'app' => $parentApp,
  108. 'controller' => $parentController,
  109. 'action' => $parentAction
  110. ])->find();
  111. if (empty($findParentAdminMenu)) {
  112. $parentId = AdminMenuModel::insertGetId([
  113. 'app' => $parentApp,
  114. 'controller' => $parentController,
  115. 'action' => $parentAction,
  116. 'name' => '--new--'
  117. ]);
  118. } else {
  119. $parentId = $findParentAdminMenu['id'];
  120. }
  121. }
  122. $findAdminMenu = AdminMenuModel::where([
  123. 'app' => $app,
  124. 'controller' => $controllerName,
  125. 'action' => $action
  126. ])->find();
  127. if (empty($findAdminMenu)) {
  128. AdminMenuModel::insert([
  129. 'parent_id' => $parentId,
  130. 'type' => $type,
  131. 'status' => $status,
  132. 'list_order' => $listOrder,
  133. 'app' => $app,
  134. 'controller' => $controllerName,
  135. 'action' => $action,
  136. 'param' => $param,
  137. 'name' => $name,
  138. 'icon' => $icon,
  139. 'remark' => $remark
  140. ]);
  141. $menuName = $name;
  142. array_push($newMenus, "$app/$controllerName/$action 已导入");
  143. } else {
  144. if ($findAdminMenu['name'] == '--new--') {
  145. AdminMenuModel::where([
  146. 'app' => $app,
  147. 'controller' => $controllerName,
  148. 'action' => $action
  149. ])->update([
  150. 'parent_id' => $parentId,
  151. 'type' => $type,
  152. 'status' => $status,
  153. 'list_order' => $listOrder,
  154. 'param' => $param,
  155. 'name' => $name,
  156. 'icon' => $icon,
  157. 'remark' => $remark
  158. ]);
  159. $menuName = $name;
  160. } else {
  161. // 只关注菜单层级关系,是否有视图
  162. AdminMenuModel::where([
  163. 'app' => $app,
  164. 'controller' => $controllerName,
  165. 'action' => $action
  166. ])->update([
  167. //'parent_id' => $parentId,
  168. 'type' => $type,
  169. ]);
  170. $menuName = $findAdminMenu['name'];
  171. }
  172. array_push($newMenus, "$app/$controllerName/$action 层级关系已更新");
  173. }
  174. $authRuleName = "{$app}/{$controllerName}/{$action}";
  175. $findAuthRuleCount = AuthRuleModel::where([
  176. 'app' => $app,
  177. 'name' => $authRuleName,
  178. 'type' => 'admin_url'
  179. ])->count();
  180. if ($findAuthRuleCount == 0) {
  181. AuthRuleModel::insert([
  182. 'app' => $app,
  183. 'name' => $authRuleName,
  184. 'type' => 'admin_url',
  185. 'param' => $param,
  186. 'title' => $menuName
  187. ]);
  188. } else {
  189. AuthRuleModel::where([
  190. 'app' => $app,
  191. 'name' => $authRuleName,
  192. 'type' => 'admin_url',
  193. ])->update([
  194. 'param' => $param,
  195. 'title' => $menuName
  196. ]);
  197. }
  198. }
  199. }
  200. $reflect = new \ReflectionClass($controllerClass);
  201. $methods = $reflect->getMethods(\ReflectionMethod::IS_PUBLIC);
  202. if (!empty($methods)) {
  203. foreach ($methods as $method) {
  204. if ($method->class == $controllerClass && strpos($method->name, '_') !== 0) {
  205. $menuAnnotations = Annotations::ofMethod($controllerClass, $method->name, '@adminMenu');
  206. if (!empty($menuAnnotations)) {
  207. $menuAnnotation = $menuAnnotations[0];
  208. $name = $menuAnnotation->name;
  209. $icon = $menuAnnotation->icon;
  210. $type = $menuAnnotation->hasView ? 1 : 2;//1:有界面可访问菜单,2:无界面可访问菜单,0:只作为菜单
  211. $action = $method->name;
  212. $status = empty($menuAnnotation->display) ? 0 : 1;
  213. $listOrder = floatval($menuAnnotation->order);
  214. $param = $menuAnnotation->param;
  215. $remark = $menuAnnotation->remark;
  216. if (empty($menuAnnotation->parent)) {
  217. $parentId = 0;
  218. } else {
  219. $parent = explode('/', $menuAnnotation->parent);
  220. $countParent = count($parent);
  221. if ($countParent > 3) {
  222. throw new \Exception($controllerClass . ':' . $action . ' @menuRoot parent格式不正确!');
  223. }
  224. $parentApp = $app;
  225. $parentController = $controllerName;
  226. $parentAction = '';
  227. switch ($countParent) {
  228. case 1:
  229. $parentAction = $parent[0];
  230. break;
  231. case 2:
  232. $parentController = $parent[0];
  233. $parentAction = $parent[1];
  234. break;
  235. case 3:
  236. $parentApp = $parent[0];
  237. $parentController = $parent[1];
  238. $parentAction = $parent[2];
  239. break;
  240. }
  241. $findParentAdminMenu = AdminMenuModel::where([
  242. 'app' => $parentApp,
  243. 'controller' => $parentController,
  244. 'action' => $parentAction
  245. ])->find();
  246. if (empty($findParentAdminMenu)) {
  247. $parentId = AdminMenuModel::insertGetId([
  248. 'app' => $parentApp,
  249. 'controller' => $parentController,
  250. 'action' => $parentAction,
  251. 'name' => '--new--'
  252. ]);
  253. } else {
  254. $parentId = $findParentAdminMenu['id'];
  255. }
  256. }
  257. $findAdminMenu = AdminMenuModel::where([
  258. 'app' => $app,
  259. 'controller' => $controllerName,
  260. 'action' => $action
  261. ])->find();
  262. if (empty($findAdminMenu)) {
  263. AdminMenuModel::insert([
  264. 'parent_id' => $parentId,
  265. 'type' => $type,
  266. 'status' => $status,
  267. 'list_order' => $listOrder,
  268. 'app' => $app,
  269. 'controller' => $controllerName,
  270. 'action' => $action,
  271. 'param' => $param,
  272. 'name' => $name,
  273. 'icon' => $icon,
  274. 'remark' => $remark
  275. ]);
  276. $menuName = $name;
  277. array_push($newMenus, "$app/$controllerName/$action 已导入");
  278. } else {
  279. if ($findAdminMenu['name'] == '--new--') {
  280. AdminMenuModel::where([
  281. 'app' => $app,
  282. 'controller' => $controllerName,
  283. 'action' => $action
  284. ])->update([
  285. 'parent_id' => $parentId,
  286. 'type' => $type,
  287. 'status' => $status,
  288. 'list_order' => $listOrder,
  289. 'param' => $param,
  290. 'name' => $name,
  291. 'icon' => $icon,
  292. 'remark' => $remark
  293. ]);
  294. $menuName = $name;
  295. } else {
  296. // 只关注菜单层级关系,是否有视图
  297. AdminMenuModel::where([
  298. 'app' => $app,
  299. 'controller' => $controllerName,
  300. 'action' => $action
  301. ])->update([
  302. //'parent_id' => $parentId,
  303. 'type' => $type,
  304. ]);
  305. $menuName = $findAdminMenu['name'];
  306. }
  307. array_push($newMenus, "$app/$controllerName/$action 已更新");
  308. }
  309. $authRuleName = "{$app}/{$controllerName}/{$action}";
  310. $findAuthRuleCount = AuthRuleModel::where([
  311. 'app' => $app,
  312. 'name' => $authRuleName,
  313. 'type' => 'admin_url'
  314. ])->count();
  315. if ($findAuthRuleCount == 0) {
  316. AuthRuleModel::insert([
  317. 'app' => $app,
  318. 'name' => $authRuleName,
  319. 'type' => 'admin_url',
  320. 'param' => $param,
  321. 'title' => $menuName
  322. ]);
  323. } else {
  324. AuthRuleModel::where([
  325. 'app' => $app,
  326. 'name' => $authRuleName,
  327. 'type' => 'admin_url',
  328. ])->update([
  329. 'param' => $param,
  330. 'title' => $menuName
  331. ]);
  332. }
  333. }
  334. }
  335. }
  336. }
  337. }
  338. }
  339. return $newMenus;
  340. }
  341. }