Menu.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. use tools\Phptree;
  5. class Menu extends Base
  6. {
  7. protected $autoWriteTimestamp = false;
  8. public function updates(){
  9. $data = request()->post();
  10. return $this->updateInfo($data,'Menu','');
  11. }
  12. // 获取所有权限
  13. public function getAllMenuTree(){
  14. $map[] = ['id','not in',[37,38,39,47,48]];
  15. $map[] = ['del','=',0];
  16. $lists = Db::name('menu')
  17. ->where('enable',1)
  18. ->where($map)
  19. ->field('id,title,pid')
  20. ->order('sort asc,id asc')
  21. ->select();
  22. $lists = $lists?$lists:[];
  23. foreach ($lists as $k=>$v){
  24. $pids = [];
  25. $pid = $v['pid'];
  26. while (true){
  27. if($pid == 0){
  28. break;
  29. }else{
  30. $pids[] = 'sub_'.$pid;
  31. $cc = Db::name('menu')
  32. ->where('id',$pid)
  33. ->find();
  34. if(!$cc){
  35. break;
  36. }
  37. $pid = $cc['pid'];
  38. }
  39. }
  40. $lists[$k]['pids'] = $pids?implode(' ',$pids):'';
  41. }
  42. $tree = Phptree::makeTree(($lists), array(
  43. 'primary_key'=>'id',
  44. 'parent_key'=>'pid',
  45. 'expanded' => true,
  46. 'children_key' => 'children'
  47. ));
  48. return $tree;
  49. }
  50. // 获取某组织的所有权限
  51. public function getOrgAllMenuTree($orgId){
  52. $auths = model('Org')->getOrgAuths($orgId,1);
  53. $lists = [];
  54. if($auths){
  55. $map[] = ['id','not in',[37,38,39,47,48]];
  56. $map[] = ['del','=',0];
  57. $lists = Db::name('menu')
  58. ->where('enable',1)
  59. ->where('id','in',$auths)
  60. ->where($map)
  61. ->field('id,title,pid')
  62. ->order('sort asc,id asc')
  63. ->select();
  64. $lists = $lists?$lists:[];
  65. foreach ($lists as $k=>$v){
  66. $pids = [];
  67. $pid = $v['pid'];
  68. while (true){
  69. if($pid == 0){
  70. break;
  71. }else{
  72. $pids[] = 'sub_'.$pid;
  73. $cc = Db::name('menu')
  74. ->where('id',$pid)
  75. ->find();
  76. if(!$cc){
  77. break;
  78. }
  79. $pid = $cc['pid'];
  80. }
  81. }
  82. $lists[$k]['pids'] = $pids?implode(' ',$pids):'';
  83. }
  84. }
  85. $tree = Phptree::makeTree(($lists), array(
  86. 'primary_key'=>'id',
  87. 'parent_key'=>'pid',
  88. 'expanded' => true,
  89. 'children_key' => 'children'
  90. ));
  91. return $tree;
  92. }
  93. public function getOrgAllMenuTree_new($orgId){
  94. $auths = model('Org')->getOrgAuths($orgId,1);
  95. $lists = [];
  96. if($auths){
  97. $map[] = ['id','not in',[37,38,39,47,48]];
  98. $map[] = ['del','=',0];
  99. $lists = Db::name('menu')
  100. ->where('enable',1)
  101. ->where('id','in',$auths)
  102. ->where($map)
  103. ->field('id,title,pid')
  104. ->order('sort asc,id asc')
  105. ->select();
  106. $lists = $lists?$lists:[];
  107. // foreach ($lists as $k=>$v){
  108. // $pids = [];
  109. // $pid = $v['pid'];
  110. // while (true){
  111. // if($pid == 0){
  112. // break;
  113. // }else{
  114. // $pids[] = 'sub_'.$pid;
  115. // $cc = Db::name('menu')
  116. // ->where('id',$pid)
  117. // ->find();
  118. // if(!$cc){
  119. // break;
  120. // }
  121. // $pid = $cc['pid'];
  122. // }
  123. // }
  124. // $lists[$k]['pids'] = $pids?implode(' ',$pids):'';
  125. // }
  126. }
  127. $tree = Phptree::makeTree(($lists), array(
  128. 'primary_key'=>'id',
  129. 'parent_key'=>'pid',
  130. 'expanded' => true,
  131. 'children_key' => 'children'
  132. ));
  133. return $tree;
  134. }
  135. // 根据角色和组织获取用户菜单项
  136. public function getMenuTree($rolesId,$orgId){
  137. $map[] = ['enable','=',1];
  138. $map[] = ['del','=',0];
  139. $map[] = ['is_btn','=',0];
  140. if($rolesId == 1){ // 超级管理员
  141. }else if($rolesId == 2){ //总公司管理员
  142. $auths = model('Org')->getOrgAuths($orgId,1);
  143. if($auths){
  144. $map[] = ['id','in',$auths];
  145. }else{
  146. $map[] = ['id','=',0];
  147. }
  148. }else{ // 项目管理员
  149. $auths = model('Roles')->getRolesAuths($rolesId,1);
  150. if($auths){
  151. $map[] = ['id','in',$auths];
  152. }else{
  153. $map[] = ['id','=',0];
  154. }
  155. }
  156. if($rolesId!=1){
  157. /*3.只有admin管理员可见
  158. 系统设置: 菜单管理、配置管理、安卓版本管理、苹果版本管理、
  159. 模块管理、这些项目,在权限分配里面就不要出现了,不需要分配*/
  160. $map[] = ['id','not in',[37,38,39,47,48]];
  161. }
  162. $lists = Db::name('menu')
  163. ->where($map)
  164. ->field('id,title,url,icons,pid')
  165. ->order('sort asc,id asc')
  166. ->select();
  167. $lists = $lists?$lists:[];
  168. $first = [
  169. 'id' => -1,
  170. 'title' => '系统首页',
  171. 'url' => $this->getNavUrl('Index/def'),
  172. 'icons' => 'fa fa-home',
  173. 'pid' => 0,
  174. ];
  175. $arr[] = $first;
  176. foreach ($lists as $k=>$v){
  177. $v['url'] = $this->getNavUrl($v['url']);
  178. $arr[] = $v;
  179. }
  180. $tree = list_to_tree($arr, 'pid', 'child');
  181. return $tree;
  182. }
  183. /**
  184. * 获取导航URL
  185. * @param string $url 导航URL
  186. * @return string 解析或的url
  187. */
  188. public function getNavUrl($url){
  189. switch ($url) {
  190. case 'http://' === substr($url, 0, 7):
  191. case 'https://' === substr($url, 0, 8):
  192. case '#' === substr($url, 0, 1):
  193. break;
  194. default:
  195. $url = url($url);
  196. break;
  197. }
  198. return $url;
  199. }
  200. }