Menu.php 6.7 KB

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