MenuWidget.class.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. /**
  3. * lionfish 商城系统
  4. *
  5. * ==========================================================================
  6. * @link http://www.liofis.com/
  7. * @copyright Copyright (c) 2015 liofis.com.
  8. * @license http://www.liofis.com/license.html License
  9. * ==========================================================================
  10. *
  11. * @author fish
  12. *
  13. */
  14. namespace Admin\Widget;
  15. use Think\Controller;
  16. /**
  17. * 后台菜单
  18. */
  19. class MenuWidget extends Controller{
  20. function menu_show(){
  21. $menu=M('Menu')->order('sort_order')->select();
  22. $tree=list_to_tree($menu,'id','pid','children',0);
  23. if(!$_SESSION[C('ADMIN_AUTH_KEY')])
  24. {
  25. $authId = $_SESSION[C('USER_AUTH_KEY')];
  26. //先查询出有权限的 node ,拼接node的链接,查询出所有的菜单可用性,接着,反向查询,顶层无链接的菜单
  27. $show_menu_arr = array();//需要显示的菜单
  28. $user_auth_arr = session('user_auth');
  29. $user_role_id = $user_auth_arr['role_id'];
  30. $access_action_arr = S('ACCESS_ACTION_LIST_ROLE'.$user_role_id);
  31. if(empty($access_action_arr))
  32. {
  33. $sql = "select n.* from ".C('DB_PREFIX')."access as a left join ".C('DB_PREFIX')."node as n on a.node_id = n.id where a.role_id = {$user_role_id} order by pid asc";
  34. $node_list = M()->query($sql);
  35. $node_list_tree = list_to_tree($node_list,'id','pid','children',0);
  36. $access_action_arr = array();//被允许的模块
  37. foreach($node_list_tree as $first)
  38. {
  39. foreach($first['children'] as $second)
  40. {
  41. foreach($second['children'] as $third)
  42. {
  43. $access_action_arr[] = strtoupper($second['name'].'/'.$third['name']);
  44. }
  45. }
  46. }
  47. S('ACCESS_ACTION_LIST_ROLE'.$user_role_id,$access_action_arr);
  48. }
  49. $child_menu = array();
  50. foreach($menu as $val)
  51. {
  52. if( in_array(strtoupper($val['url']),$access_action_arr) )
  53. {
  54. $child_menu[] = $val['id'];
  55. $this->_get_parent_menu($val,$child_menu);
  56. }
  57. }
  58. $child_menu_str = implode(',',$child_menu);
  59. $menu=M('Menu')->where( 'id in ('.$child_menu_str.')' )->order('sort_order')->select();
  60. $tree=list_to_tree($menu,'id','pid','children',0);
  61. }
  62. $this->admin_menu=$tree;
  63. $this->display('Widget:menu');
  64. }
  65. /**
  66. * 获取上级菜单
  67. */
  68. private function _get_parent_menu($menu,&$child_menu)
  69. {
  70. if($menu['pid'] !=0)
  71. {
  72. $tmp_menu = M('menu')->where( array('id' => $menu['pid']) )->find();
  73. $child_menu[] = $tmp_menu['id'];
  74. $this->_get_parent_menu($tmp_menu,$child_menu);
  75. }
  76. }
  77. /**
  78. * 得到菜单
  79. * @return array
  80. */
  81. protected function getMenu() {
  82. $menu = C('MENU');
  83. // 主菜单
  84. $mainMenu = array();
  85. // 已被映射过的键值
  86. $mapped = array();
  87. // 访问权限
  88. $access = $_SESSION['_ACCESS_LIST'];
  89. //var_dump($access);die();
  90. if (empty($access)) {
  91. $authId = $_SESSION[C('USER_AUTH_KEY')];
  92. $access = \Org\Util\Rbac::getAccessList($authId);
  93. }
  94. $authGroup = strtoupper(C('GROUP_AUTH_NAME'));
  95. // 处理主菜单
  96. foreach ($menu as $key => $menuItem) {
  97. // 不显示无权限访问的主菜单
  98. if (!$_SESSION[C('ADMIN_AUTH_KEY')]
  99. && !array_key_exists(strtoupper($key), $access[$authGroup])) {
  100. continue ;
  101. }
  102. // 主菜单是否存在映射
  103. if (isset($menuItem['mapping'])) {
  104. // 映射名
  105. $mapping = $menuItem['mapping'];
  106. // 新的菜单键值
  107. if (!empty($mapped[$mapping])) {
  108. $key = "{$mapped[$mapping]}-{$key}";
  109. $mapping = $mapped[$mapping];
  110. } else {
  111. $key = "{$mapping}-{$key}";
  112. }
  113. // 需要映射的键值已存在,则删除
  114. if (isset($mainMenu[$mapping])) {
  115. $mainMenu[$key]['name'] = $mainMenu[$mapping]['name'];
  116. $mainMenu[$key]['target'] = $mainMenu[$mapping]['target'];
  117. unset($mainMenu[$mapping]);
  118. $mapped[$mapping] = $key;
  119. }
  120. continue ;
  121. }
  122. $mainMenu[$key]['name'] = $menuItem['name'];
  123. $mainMenu[$key]['target'] = $menuItem['target'];
  124. //如果默认的target用户无权访问,则显示sub_menu中的用户有权访问的第一个页面
  125. $actions = $access[$authGroup][strtoupper($key)];
  126. $action = explode('/', strtoupper($mainMenu[$key]['target']));
  127. while (!$_SESSION[C('ADMIN_AUTH_KEY')] && !array_key_exists($action[1], $actions)) {
  128. $nextSubMenu = next($menu[$key]['sub_menu']);
  129. if (empty($nextSubMenu)) break;
  130. $mainMenu[$key]['target'] = key(current($nextSubMenu));
  131. $action = explode('/', strtoupper($mainMenu[$key]['target']));
  132. }
  133. }
  134. // 子菜单
  135. $subMenu = array();
  136. $ctrlName = $this->getCtrName();
  137. if (isset($menu[$ctrlName]['mapping'])) {
  138. $ctrlName = $menu[$ctrlName]['mapping'];
  139. }
  140. $actions = $access[$authGroup];
  141. // 主菜单如果为隐藏,则子菜单也不被显示
  142. foreach ($menu[$ctrlName]['sub_menu'] as $item) {
  143. // 子菜单是否需要显示
  144. if (isset($item['hidden']) && true === $item['hidden']) {
  145. continue ;
  146. }
  147. $route = array_shift(array_keys($item['item']));
  148. $action = explode('/', strtoupper($route));
  149. // 不显示无权限访问的子菜单
  150. if (!$_SESSION[C('ADMIN_AUTH_KEY')]
  151. && (!array_key_exists($action[0], $actions)
  152. || !array_key_exists($action[1], $actions[$action[0]]))) {
  153. continue ;
  154. }
  155. // 子菜单是否有配置
  156. if (!isset($item['item']) || empty($item['item'])) {
  157. continue ;
  158. }
  159. $routes = array_keys($item['item']);
  160. $itemNames = array_values($item['item']);
  161. $subMenu[$routes[0]] = $itemNames[0];
  162. }
  163. unset($menu);
  164. return array(
  165. 'main_menu' => $mainMenu,
  166. 'sub_menu' => $subMenu
  167. );
  168. }
  169. protected function getCtrName() {
  170. $ctrName = CONTROLLER_NAME;
  171. if(strpos($ctrName, '.') !== false && strtoupper($ctrName[0]) === $ctrName[0]) {
  172. $ctrName[0] = strtolower($ctrName[0]);
  173. }
  174. return $ctrName;
  175. }
  176. }