No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Menu.php 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Yzncms [ 御宅男工作室 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2018 http://yzncms.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 御宅男 <530765310@qq.com>
  10. // +----------------------------------------------------------------------
  11. // +----------------------------------------------------------------------
  12. // | 菜单类
  13. // +----------------------------------------------------------------------
  14. namespace app\common\library;
  15. use app\admin\model\AuthRule as AuthRuleModel;
  16. use think\addons\Service;
  17. use think\Db;
  18. use think\exception\PDOException;
  19. use util\Tree;
  20. class Menu
  21. {
  22. /**
  23. * 创建菜单
  24. * @param array $menu
  25. * @param mixed $parent 父类的name或pid
  26. */
  27. public static function create($menu = [], $parent = 0)
  28. {
  29. $old = [];
  30. self::menuUpdate($menu, $old, $parent);
  31. //菜单刷新处理
  32. $info = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1];
  33. preg_match('/addons\\\\([a-z0-9]+)\\\\/i', $info['class'], $matches);
  34. if ($matches && isset($matches[1])) {
  35. Menu::refresh($matches[1], $menu);
  36. }
  37. }
  38. /**
  39. * 删除菜单
  40. * @param string $name 规则name
  41. * @return boolean
  42. */
  43. public static function delete($name)
  44. {
  45. $ids = self::getAuthRuleIdsByName($name);
  46. if (!$ids) {
  47. return false;
  48. }
  49. AuthRuleModel::destroy($ids);
  50. return true;
  51. }
  52. /**
  53. * 启用菜单
  54. * @param string $name
  55. * @return boolean
  56. */
  57. public static function enable($name)
  58. {
  59. $ids = self::getAuthRuleIdsByName($name);
  60. if (!$ids) {
  61. return false;
  62. }
  63. AuthRuleModel::where('id', 'in', $ids)->update(['status' => 1]);
  64. return true;
  65. }
  66. /**
  67. * 禁用菜单
  68. * @param string $name
  69. * @return boolean
  70. */
  71. public static function disable($name)
  72. {
  73. $ids = self::getAuthRuleIdsByName($name);
  74. if (!$ids) {
  75. return false;
  76. }
  77. AuthRuleModel::where('id', 'in', $ids)->update(['status' => 0]);
  78. return true;
  79. }
  80. /**
  81. * 升级菜单
  82. * @param string $name 插件名称
  83. * @param array $menu 新菜单
  84. * @return bool
  85. */
  86. public static function upgrade($name, $menu)
  87. {
  88. $ids = self::getAuthRuleIdsByName($name);
  89. $old = AuthRuleModel::where('id', 'in', $ids)->select()->toArray();
  90. $old = array_column($old, null, 'name');
  91. Db::startTrans();
  92. try {
  93. self::menuUpdate($menu, $old);
  94. $ids = [];
  95. foreach ($old as $index => $item) {
  96. if (!isset($item['keep'])) {
  97. $ids[] = $item['id'];
  98. }
  99. }
  100. if ($ids) {
  101. //旧版本的菜单需要做删除处理
  102. $config = Service::config($name);
  103. $menus = $config['menus'] ?? [];
  104. $where = ['id' => ['in', $ids]];
  105. if ($menus) {
  106. //必须是旧版本中的菜单,可排除用户自主创建的菜单
  107. $where['name'] = ['in', $menus];
  108. }
  109. AuthRuleModel::where($where)->delete();
  110. }
  111. Db::commit();
  112. } catch (PDOException $e) {
  113. Db::rollback();
  114. return false;
  115. }
  116. Menu::refresh($name, $menu);
  117. return true;
  118. }
  119. /**
  120. * 刷新插件菜单配置缓存
  121. * @param string $name
  122. * @param array $menu
  123. */
  124. public static function refresh($name, $menu = [])
  125. {
  126. if (!$menu) {
  127. // $menu为空时表示首次安装,首次安装需刷新插件菜单标识缓存
  128. $menuIds = self::getAuthRuleIdsByName($name);
  129. $menus = Db::name("auth_rule")->where('id', 'in', $menuIds)->column('name');
  130. } else {
  131. // 刷新新的菜单缓存
  132. $getMenus = function ($menu) use (&$getMenus) {
  133. $result = [];
  134. foreach ($menu as $index => $item) {
  135. $result[] = $item['name'];
  136. $result = array_merge($result, isset($item['sublist']) && is_array($item['sublist']) ? $getMenus($item['sublist']) : []);
  137. }
  138. return $result;
  139. };
  140. $menus = $getMenus($menu);
  141. }
  142. //刷新新的插件核心菜单缓存
  143. Service::config($name, ['menus' => $menus]);
  144. }
  145. /**
  146. * 菜单升级
  147. * @param array $newMenu
  148. * @param array $oldMenu
  149. * @param int $parent
  150. * @throws Exception
  151. */
  152. private static function menuUpdate($newMenu, &$oldMenu, $parent = 0)
  153. {
  154. if (!is_numeric($parent)) {
  155. $parentRule = AuthRuleModel::getByName($parent);
  156. $pid = $parentRule ? $parentRule['id'] : 0;
  157. } else {
  158. $pid = $parent;
  159. }
  160. $allow = array_flip(['name', 'title', 'url', 'icon', 'condition', 'remark', 'ismenu', 'menutype', 'extend', 'listorder', 'status']);
  161. foreach ($newMenu as $k => $v) {
  162. $hasChild = isset($v['sublist']) && $v['sublist'];
  163. $data = array_intersect_key($v, $allow);
  164. $data['ismenu'] = $data['ismenu'] ?? ($hasChild ? 1 : 0);
  165. $data['icon'] = $data['icon'] ?? ($hasChild ? 'iconfont icon-other' : 'iconfont icon-circle-line');
  166. $data['parentid'] = $pid;
  167. $data['status'] = $data['status'] ?? 1;
  168. if (!isset($oldMenu[$data['name']])) {
  169. $menu = AuthRuleModel::create($data);
  170. } else {
  171. $menu = $oldMenu[$data['name']];
  172. //更新旧菜单
  173. AuthRuleModel::update($data, ['id' => $menu['id']]);
  174. $oldMenu[$data['name']]['keep'] = true;
  175. }
  176. if ($hasChild) {
  177. self::menuUpdate($v['sublist'], $oldMenu, $menu['id']);
  178. }
  179. }
  180. }
  181. /**
  182. * 根据名称获取规则IDS
  183. * @param string $name
  184. * @return array
  185. */
  186. public static function getAuthRuleIdsByName($name)
  187. {
  188. $ids = [];
  189. $menu = AuthRuleModel::getByName($name);
  190. if ($menu) {
  191. // 必须将结果集转换为数组
  192. $ruleList = AuthRuleModel::order('listorder', 'desc')->field('id,parentid,name')->select()->toArray();
  193. // 构造菜单数据
  194. $ids = Tree::instance()->init($ruleList)->getChildrenIds($menu['id'], true);
  195. }
  196. return $ids;
  197. }
  198. }