AdminMenuModel.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-2019 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\model;
  12. use think\Model;
  13. use think\facade\Cache;
  14. class AdminMenuModel extends Model
  15. {
  16. /**
  17. * 按父ID查找菜单子项
  18. * @param int $parentId 父菜单ID
  19. * @param boolean $withSelf 是否包括他自己
  20. * @return array|string|\think\Collection
  21. * @throws \think\db\exception\DataNotFoundException
  22. * @throws \think\db\exception\ModelNotFoundException
  23. * @throws \think\exception\DbException
  24. */
  25. public function adminMenu($parentId, $withSelf = false)
  26. {
  27. //父节点ID
  28. $parentId = intval($parentId);
  29. $result = $this->where(['parent_id' => $parentId, 'status' => 1])->order("list_order", "ASC")->select();
  30. if ($withSelf) {
  31. $result2[] = $this->where('id', $parentId)->find();
  32. $result = array_merge($result2, $result);
  33. }
  34. //权限检查
  35. if (cmf_get_current_admin_id() == 1) {
  36. //如果是超级管理员 直接通过
  37. return $result;
  38. }
  39. $array = [];
  40. foreach ($result as $v) {
  41. //方法
  42. $action = $v['action'];
  43. //public开头的通过
  44. if (preg_match('/^public_/', $action)) {
  45. $array[] = $v;
  46. } else {
  47. if (preg_match('/^ajax_([a-z]+)_/', $action, $_match)) {
  48. $action = $_match[1];
  49. }
  50. $ruleName = strtolower($v['app'] . "/" . $v['controller'] . "/" . $action);
  51. // print_r($ruleName);
  52. if (cmf_auth_check(cmf_get_current_admin_id(), $ruleName)) {
  53. $array[] = $v;
  54. }
  55. }
  56. }
  57. return $array;
  58. }
  59. /**
  60. * 获取菜单 头部菜单导航
  61. * @param string $parentId 菜单id
  62. * @param bool $bigMenu
  63. * @return array|string|\think\Collection
  64. * @throws \think\db\exception\DataNotFoundException
  65. * @throws \think\db\exception\ModelNotFoundException
  66. * @throws \think\exception\DbException
  67. */
  68. public function subMenu($parentId = '', $bigMenu = false)
  69. {
  70. $array = $this->adminMenu($parentId, 1);
  71. $numbers = count($array);
  72. if ($numbers == 1 && !$bigMenu) {
  73. return '';
  74. }
  75. return $array;
  76. }
  77. /**
  78. * 菜单树状结构集合
  79. */
  80. public function menuTree()
  81. {
  82. $data = $this->getTree(0);
  83. return $data;
  84. }
  85. /**
  86. * 取得树形结构的菜单
  87. * @param $myId
  88. * @param string $parent
  89. * @param int $Level
  90. * @return bool|null
  91. * @throws \think\db\exception\DataNotFoundException
  92. * @throws \think\db\exception\ModelNotFoundException
  93. * @throws \think\exception\DbException
  94. */
  95. public function getTree($myId, $parent = "", $Level = 1)
  96. {
  97. $data = $this->adminMenu($myId);
  98. $Level++;
  99. if (count($data) > 0) {
  100. $ret = NULL;
  101. foreach ($data as $a) {
  102. $id = $a['id'];
  103. $app = $a['app'];
  104. $controller = ucwords($a['controller']);
  105. $action = $a['action'];
  106. //附带参数
  107. $params = "";
  108. if (!empty($a['param'])) {
  109. $params = htmlspecialchars_decode($a['param']);
  110. }
  111. if (strpos($app, 'plugin/') === 0) {
  112. $pluginName = str_replace('plugin/', '', $app);
  113. $url = cmf_plugin_url($pluginName . "://{$controller}/{$action}{$params}");
  114. } else {
  115. $url = url("{$app}/{$controller}/{$action}", $params);
  116. }
  117. $app = str_replace('/', '_', $app);
  118. $array = [
  119. "icon" => $a['icon'],
  120. "id" => $id . $app,
  121. "name" => $a['name'],
  122. "parent" => $parent,
  123. "url" => $url,
  124. 'lang' => strtoupper($app . '_' . $controller . '_' . $action)
  125. ];
  126. $ret[$id . $app] = $array;
  127. $child = $this->getTree($a['id'], $id, $Level);
  128. //由于后台管理界面只支持三层,超出的不层级的不显示
  129. if ($child && $Level <= 3) {
  130. $ret[$id . $app]['items'] = $child;
  131. }
  132. }
  133. return $ret;
  134. }
  135. return false;
  136. }
  137. /**
  138. * 更新缓存
  139. * @param null $data
  140. * @return array|null
  141. */
  142. public function menuCache($data = null)
  143. {
  144. if (empty($data)) {
  145. $data = $this->order("list_order", "ASC")->column('*');
  146. Cache::set('Menu', $data, 0);
  147. } else {
  148. Cache::set('Menu', $data, 0);
  149. }
  150. return $data;
  151. }
  152. public function menu($parentId, $with_self = false)
  153. {
  154. //父节点ID
  155. $parentId = (int)$parentId;
  156. $result = $this->where('parent_id', $parentId)->select();
  157. if ($with_self) {
  158. $result2[] = $this->where('id', $parentId)->find();
  159. $result = array_merge($result2, $result);
  160. }
  161. return $result;
  162. }
  163. /**
  164. * 得到某父级菜单所有子菜单,包括自己
  165. * @param int $parentId
  166. * @return array|string|\think\Collection
  167. * @throws \think\db\exception\DataNotFoundException
  168. * @throws \think\db\exception\ModelNotFoundException
  169. * @throws \think\exception\DbException
  170. */
  171. public function getMenuTree($parentId = 0)
  172. {
  173. $menus = $this->where("parent_id", $parentId)->order(["list_order" => "ASC"])->select();
  174. if ($menus) {
  175. foreach ($menus as $key => $menu) {
  176. $children = $this->getMenuTree($menu['id']);
  177. if (!empty($children)) {
  178. $menus[$key]['children'] = $children;
  179. }
  180. unset($menus[$key]['id']);
  181. unset($menus[$key]['parent_id']);
  182. }
  183. return $menus;
  184. } else {
  185. return $menus;
  186. }
  187. }
  188. }