AdminMenu.php 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace app\cxinv\model;
  3. class AdminMenu extends Base
  4. {
  5. //设置字段信息
  6. protected $schema = [
  7. 'id' =>'bigint',//
  8. 'menu_name' =>'varchar',//菜单名称
  9. 'menu_code' =>'varchar',//菜单编号
  10. 'menu_img' =>'varchar',//菜单图片
  11. 'menu_route' =>'varchar',//菜单路由
  12. 'menu_url' =>'varchar',//菜单url
  13. 'pid' =>'bigint',//父级id
  14. 'is_show' =>'int',//是否显示1显示0 隐藏
  15. 'is_private' =>'int',//是否私有
  16. 'weight' =>'double',//权重
  17. 'level' =>'tinyint',//菜单等级 1只有超管看2级只有运营看 3 所有人都能看
  18. 'status' =>'int',//状态 0禁用1启用
  19. 'is_del' =>'tinyint',//是否删除 1删除0否
  20. 'addtime' =>'datetime',//
  21. 'updatetime' =>'datetime',//
  22. 'menu_type' =>'tinyint',//1菜单2 页面
  23. ];
  24. public static function GetMenu($menuid){
  25. $info=self::where(['id'=>$menuid,'is_del'=>0])->findOrEmpty();
  26. if($info->pid>0){
  27. $paren = self::where(['id'=>$info->pid,'is_del'=>0])->findOrEmpty();
  28. }
  29. return ['menu_name'=>$info->menu_name,'parent_name'=>$paren->menu_name??'','parent_id'=>$info->pid];
  30. }
  31. public static function GetMenuBySecod($menuid,$result=[]){
  32. $menuArr = self::where(['id'=>$menuid])->findOrEmpty();
  33. if(!$menuArr->isEmpty()){
  34. if($menuArr->pid>0) self::GetMenuBySecod($menuArr->pid,$result);
  35. $result[] = ['id' => $menuid, 'rename' => $menuArr->menu_name, 'pid' => $menuArr->pid];
  36. }
  37. return $result;
  38. }
  39. //通过功能集合返回菜单树状集
  40. public function GetMenuByActionArr($actionArr,$where=[]){
  41. $menuArr = Action::with(['ActionInfo'])->whereIn('id',$actionArr)->select();
  42. $tree=[];
  43. if(!$menuArr->isEmpty()){
  44. $menuid = [];
  45. foreach ($menuArr as $item){
  46. $menuid[$item->menuid][] =$item->action_code;
  47. }
  48. $menuArrId = array_unique(array_column($menuArr->toArray(),'menuid'));
  49. $menuAll=$this->where($where)->column('menu_name,menu_img,menu_route,menu_url,status,pid,weight','id');
  50. foreach ($menuArrId as $item){
  51. $this->buildForwardTree($item,$menuid[$item]??[],$menuAll,$tree);
  52. }
  53. $weight= array_column($tree,'weight');
  54. array_multisort($weight, SORT_DESC, $tree);
  55. }
  56. return $tree;
  57. }
  58. //有子找父级
  59. public function buildForwardTree($itemId, $child = [],&$items, &$menuTree=[]) {
  60. if(!isset($items[$itemId]))return;
  61. $node=$items[$itemId];
  62. if($node['pid']==0){
  63. if(!isset($menuTree[$itemId])){
  64. $menuTree[$itemId]=$node;
  65. }
  66. $menuTree[$itemId]['child'][]=$child;
  67. $weight= array_column($menuTree[$itemId]['child'],'weight');
  68. array_multisort($weight, SORT_DESC, $menuTree[$itemId]['child']);
  69. }else{
  70. $node['action']=$child;
  71. $this->buildForwardTree($node['pid'],$node,$items,$menuTree);
  72. }
  73. }
  74. public function GetMenuActionTree($pid=0){
  75. $result = $this->field('menu_name,menu_img,menu_route,menu_url,status,pid,weight,id')
  76. ->where(['pid'=>$pid,'is_del'=>0])
  77. ->order(['weight'=>'desc','id'=>'desc'])->select();
  78. $remp=[];
  79. if($result->isEmpty())return $remp;
  80. foreach ($result as $item){
  81. $item->child = $this->GetMenuActionTree($item->id);
  82. if($item->pid>0){
  83. $item->action = Action::with(['ActionInfo'])->field('id,action_code')->where
  84. (['menuid'=>$item->id,'is_del'=>0])->select();
  85. }
  86. $remp[] = $item;
  87. }
  88. return $remp;
  89. }
  90. }