MenuModel.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-2017 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\Db;
  13. use think\Model;
  14. class MenuModel extends Model
  15. {
  16. protected $table = 'nw_admin_auth_rule';
  17. /**
  18. * 按父ID查找菜单子项
  19. * @param int $parentId 父菜单ID
  20. * @param boolean $withSelf 是否包括他自己
  21. * @return mixed
  22. */
  23. public function adminMenu($parentId, $withSelf = false)
  24. {
  25. //父节点ID
  26. $parentId = (int)$parentId;
  27. // 菜单显示 管理员id为1
  28. if (mg_get_current_admin_id() == 1) {
  29. $result = $this->where(['parent_id' => $parentId, 'status' => 1])->order(["list_order" => "ASC"])->select();
  30. } else {
  31. $roleIds = Db::name('nw_admin_role_user')->where(['user_id' => mg_get_current_admin_id()])->column('role_id');
  32. $authInfo = Db::name('nw_admin_role')->where(['id' => ['in', $roleIds]])->column('auth_ids');
  33. $authIds = [];
  34. foreach ($authInfo as $info) {
  35. $infoArr = explode(',', $info);
  36. foreach ($infoArr as $authId) {
  37. array_push($authIds, $authId);
  38. }
  39. }
  40. $result = $this->where(['parent_id' => $parentId, 'status' => 1, 'id' => ['in', $authIds]])->order(["list_order" => "ASC"])->select();
  41. }
  42. if ($withSelf) {
  43. $result2[] = $this->where(['id' => $parentId])->find();
  44. $result = array_merge($result2, $result);
  45. }
  46. //权限检查
  47. if (mg_get_current_admin_id() == 1) {
  48. //如果是超级管理员 直接通过
  49. return $result;
  50. }
  51. $array = [];
  52. foreach ($result as $v) {
  53. list($app, $controller, $action) = explode('/', $v['auth_name']);
  54. //public开头的通过
  55. if (preg_match('/^public_/', $action)) {
  56. $array[] = $v;
  57. } else {
  58. if (preg_match('/^ajax_([a-z]+)_/', $action, $_match)) {
  59. $action = $_match[1];
  60. }
  61. /*
  62. $rule_name = $v['auth_name'];
  63. if (cmf_auth_check(mg_get_current_admin_id(), $rule_name)) {*/
  64. $array[] = $v;
  65. //}
  66. }
  67. }
  68. return $array;
  69. }
  70. /**
  71. * 获取菜单 头部菜单导航
  72. * @param string $parentId 菜单id
  73. * @return mixed|string
  74. */
  75. public function subMenu($parentId = '', $bigMenu = false)
  76. {
  77. $array = $this->adminMenu($parentId, 1);
  78. $numbers = count($array);
  79. if ($numbers == 1 && !$bigMenu) {
  80. return '';
  81. }
  82. return $array;
  83. }
  84. /**
  85. * 菜单树状结构集合
  86. */
  87. public function menuTree()
  88. {
  89. $data = $this->getTree(0);
  90. return $data;
  91. }
  92. /**
  93. * 取得树形结构的菜单
  94. * @param $myId
  95. * @param string $parent
  96. * @param int $Level
  97. * @return bool|null
  98. */
  99. public function getTree($myId, $parent = "", $Level = 1)
  100. {
  101. $data = $this->adminMenu($myId);
  102. $Level++;
  103. if (count($data) > 0) {
  104. $ret = NULL;
  105. foreach ($data as $a) {
  106. list($app, $controller, $action) = explode('/', $a['auth_name']);
  107. $id = $a['id'];
  108. $name = $app;
  109. $controller = ucwords($controller);
  110. //附带参数
  111. $params = "";
  112. if ($a['param']) {
  113. $params = "?" . htmlspecialchars_decode($a['param']);
  114. }
  115. $array = [
  116. "icon" => $a['icon'],
  117. "id" => $id . $name,
  118. "name" => $a['name'],
  119. "parent" => $parent,
  120. "tab_type"=> $a['tab_type'],
  121. "url" => url("{$controller}/{$action}{$params}")
  122. ];
  123. $ret[$id . $name] = $array;
  124. $child = $this->getTree($a['id'], $id, $Level);
  125. //由于后台管理界面只支持三层,超出的不层级的不显示
  126. if ($child && $Level <= 3) {
  127. $ret[$id . $name]['items'] = $child;
  128. }
  129. }
  130. return $ret;
  131. }
  132. return false;
  133. }
  134. public function menu($parentId, $with_self = false)
  135. {
  136. //父节点ID
  137. $parentId = (int)$parentId;
  138. $result = $this->where(['parent_id' => $parentId])->select();
  139. if ($with_self) {
  140. $result2[] = $this->where(['id' => $parentId])->find();
  141. $result = array_merge($result2, $result);
  142. }
  143. return $result;
  144. }
  145. /**
  146. * 得到某父级菜单所有子菜单,包括自己
  147. * @param number $parentId
  148. */
  149. public function get_menu_tree($parentId = 0)
  150. {
  151. $menus = $this->where(["parent_id" => $parentId])->order(["list_order" => "ASC"])->select();
  152. if ($menus) {
  153. foreach ($menus as $key => $menu) {
  154. $children = $this->get_menu_tree($menu['id']);
  155. if (!empty($children)) {
  156. $menus[$key]['children'] = $children;
  157. }
  158. unset($menus[$key]['id']);
  159. unset($menus[$key]['parent_id']);
  160. }
  161. return $menus;
  162. } else {
  163. return $menus;
  164. }
  165. }
  166. }