Menu.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. // 根据角色和组织获取用户菜单项
  94. public function getMenuTree($rolesId,$orgId){
  95. $map[] = ['enable','=',1];
  96. $map[] = ['del','=',0];
  97. $map[] = ['is_btn','=',0];
  98. if($rolesId == 1){ // 超级管理员
  99. }else if($rolesId == 2){ //总公司管理员
  100. $auths = model('Org')->getOrgAuths($orgId,1);
  101. if($auths){
  102. $map[] = ['id','in',$auths];
  103. }else{
  104. $map[] = ['id','=',0];
  105. }
  106. }else{ // 项目管理员
  107. $auths = model('Roles')->getRolesAuths($rolesId,1);
  108. if($auths){
  109. $map[] = ['id','in',$auths];
  110. }else{
  111. $map[] = ['id','=',0];
  112. }
  113. }
  114. if($rolesId!=1){
  115. /*3.只有admin管理员可见
  116. 系统设置: 菜单管理、配置管理、安卓版本管理、苹果版本管理、
  117. 模块管理、这些项目,在权限分配里面就不要出现了,不需要分配*/
  118. $map[] = ['id','not in',[37,38,39,47,48]];
  119. }
  120. $lists = Db::name('menu')
  121. ->where($map)
  122. ->field('id,title,url,icons,pid,img,des')
  123. ->order('sort asc,id asc')
  124. ->select();
  125. $lists = $lists?$lists:[];
  126. // $first = [
  127. // 'id' => -1,
  128. // 'title' => '系统首页',
  129. // 'url' => $this->getNavUrl('Index/def'),
  130. // 'icons' => 'fa fa-home',
  131. // 'pid' => 0,
  132. // ];
  133. $arr = [];
  134. foreach ($lists as $k=>$v){
  135. $v['url'] = $this->getNavUrl($v['url']);
  136. $arr[] = $v;
  137. }
  138. $tree = list_to_tree($arr, 'pid', 'child');
  139. return $tree;
  140. }
  141. /**
  142. * 获取导航URL
  143. * @param string $url 导航URL
  144. * @return string 解析或的url
  145. */
  146. public function getNavUrl($url){
  147. switch ($url) {
  148. case 'http://' === substr($url, 0, 7):
  149. case 'https://' === substr($url, 0, 8):
  150. case '#' === substr($url, 0, 1):
  151. break;
  152. default:
  153. $url = url($url);
  154. break;
  155. }
  156. return $url;
  157. }
  158. // 根据角色和组织获取用户菜单项
  159. public function getMenuTree1($rolesId,$orgId,$search){
  160. $map[] = ['enable','=',1];
  161. $map[] = ['del','=',0];
  162. $map[] = ['is_btn','=',0];
  163. if($rolesId == 1){ // 超级管理员
  164. }else if($rolesId == 2){ //总公司管理员
  165. $auths = model('Org')->getOrgAuths($orgId,1);
  166. if($auths){
  167. $map[] = ['id','in',$auths];
  168. }else{
  169. $map[] = ['id','=',0];
  170. }
  171. }else{ // 项目管理员
  172. $auths = model('Roles')->getRolesAuths($rolesId,1);
  173. if($auths){
  174. $map[] = ['id','in',$auths];
  175. }else{
  176. $map[] = ['id','=',0];
  177. }
  178. }
  179. if($rolesId!=1){
  180. /*3.只有admin管理员可见
  181. 系统设置: 菜单管理、配置管理、安卓版本管理、苹果版本管理、
  182. 模块管理、这些项目,在权限分配里面就不要出现了,不需要分配*/
  183. $map[] = ['id','not in',[37,38,39,47,48]];
  184. }
  185. if($search !=''){
  186. $map[] = ['title','like','%'.$search.'%'];
  187. }
  188. $lists = Db::name('menu')
  189. ->where($map)
  190. ->field('id,title,url,icons,pid')
  191. ->order('sort asc,id asc')
  192. ->select();
  193. $lists = $lists?$lists:[];
  194. // $first = [
  195. // 'id' => -1,
  196. // 'title' => '系统首页',
  197. // 'url' => $this->getNavUrl('Index/def'),
  198. // 'icons' => 'fa fa-home',
  199. // 'pid' => 0,
  200. // ];
  201. // $arr[] = $first;
  202. $arr = [];
  203. foreach ($lists as $k=>$v){
  204. $v['url'] = $this->getNavUrl($v['url']);
  205. $arr[] = $v;
  206. }
  207. // $tree = list_to_tree($arr, 'pid', 'child');
  208. return $arr;
  209. }
  210. // 根据角色和组织获取用户菜单项
  211. public function getMenuTree2($rolesId,$orgId,$search,$pid=0){
  212. $map[] = ['enable','=',1];
  213. $map[] = ['del','=',0];
  214. $map[] = ['is_btn','=',0];
  215. if($pid == 500){
  216. $map[] = ['pid','=',500];
  217. }
  218. if($rolesId == 1){ // 超级管理员
  219. }else if($rolesId == 2){ //总公司管理员
  220. $auths = model('Org')->getOrgAuths($orgId,1);
  221. if($auths){
  222. $map[] = ['id','in',$auths];
  223. }else{
  224. $map[] = ['id','=',0];
  225. }
  226. }else{ // 项目管理员
  227. $auths = model('Roles')->getRolesAuths($rolesId,1);
  228. if($auths){
  229. $map[] = ['id','in',$auths];
  230. }else{
  231. $map[] = ['id','=',0];
  232. }
  233. }
  234. if($rolesId!=1){
  235. /*3.只有admin管理员可见
  236. 系统设置: 菜单管理、配置管理、安卓版本管理、苹果版本管理、
  237. 模块管理、这些项目,在权限分配里面就不要出现了,不需要分配*/
  238. $map[] = ['id','not in',[37,38,39,47,48]];
  239. }
  240. if($search !=''){
  241. $map[] = ['title','like','%'.$search.'%'];
  242. }
  243. $lists = Db::name('menu')
  244. ->where($map)
  245. ->field('id,title,url,icons,pid')
  246. ->order('sort asc,id asc')
  247. ->fetchSql()
  248. ->select();
  249. halt($lists);
  250. $lists = $lists?$lists:[];
  251. // $first = [
  252. // 'id' => -1,
  253. // 'title' => '系统首页',
  254. // 'url' => $this->getNavUrl('Index/def'),
  255. // 'icons' => 'fa fa-home',
  256. // 'pid' => 0,
  257. // ];
  258. // $arr[] = $first;
  259. $arr = [];
  260. foreach ($lists as $k=>$v){
  261. $v['url'] = $this->getNavUrl($v['url']);
  262. $arr[] = $v;
  263. }
  264. // $tree = list_to_tree($arr, 'pid', 'child');
  265. return $arr;
  266. }
  267. }