1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- <?php
- namespace app\cxinv\model;
- class AdminMenu extends Base
- {
- //设置字段信息
- protected $schema = [
- 'id' =>'bigint',//
- 'menu_name' =>'varchar',//菜单名称
- 'menu_code' =>'varchar',//菜单编号
- 'menu_img' =>'varchar',//菜单图片
- 'menu_route' =>'varchar',//菜单路由
- 'menu_url' =>'varchar',//菜单url
- 'pid' =>'bigint',//父级id
- 'is_show' =>'int',//是否显示1显示0 隐藏
- 'is_private' =>'int',//是否私有
- 'weight' =>'double',//权重
- 'level' =>'tinyint',//菜单等级 1只有超管看2级只有运营看 3 所有人都能看
- 'status' =>'int',//状态 0禁用1启用
- 'is_del' =>'tinyint',//是否删除 1删除0否
- 'addtime' =>'datetime',//
- 'updatetime' =>'datetime',//
- 'menu_type' =>'tinyint',//1菜单2 页面
- ];
-
- public static function GetMenu($menuid){
- $info=self::where(['id'=>$menuid,'is_del'=>0])->findOrEmpty();
- if($info->pid>0){
- $paren = self::where(['id'=>$info->pid,'is_del'=>0])->findOrEmpty();
- }
- return ['menu_name'=>$info->menu_name,'parent_name'=>$paren->menu_name??'','parent_id'=>$info->pid];
- }
-
- public static function GetMenuBySecod($menuid,$result=[]){
- $menuArr = self::where(['id'=>$menuid])->findOrEmpty();
- if(!$menuArr->isEmpty()){
- if($menuArr->pid>0) self::GetMenuBySecod($menuArr->pid,$result);
- $result[] = ['id' => $menuid, 'rename' => $menuArr->menu_name, 'pid' => $menuArr->pid];
- }
- return $result;
- }
- //通过功能集合返回菜单树状集
- public function GetMenuByActionArr($actionArr,$where=[]){
- $menuArr = Action::with(['ActionInfo'])->whereIn('id',$actionArr)->select();
- $tree=[];
- if(!$menuArr->isEmpty()){
- $menuid = [];
- foreach ($menuArr as $item){
- $menuid[$item->menuid][] =$item->action_code;
- }
- $menuArrId = array_unique(array_column($menuArr->toArray(),'menuid'));
- $menuAll=$this->where($where)->column('menu_name,menu_img,menu_route,menu_url,status,pid,weight','id');
- foreach ($menuArrId as $item){
- $this->buildForwardTree($item,$menuid[$item]??[],$menuAll,$tree);
- }
- $weight= array_column($tree,'weight');
- array_multisort($weight, SORT_DESC, $tree);
- }
- return $tree;
- }
- //有子找父级
- public function buildForwardTree($itemId, $child = [],&$items, &$menuTree=[]) {
- if(!isset($items[$itemId]))return;
- $node=$items[$itemId];
- if($node['pid']==0){
- if(!isset($menuTree[$itemId])){
- $menuTree[$itemId]=$node;
- }
- $menuTree[$itemId]['child'][]=$child;
- $weight= array_column($menuTree[$itemId]['child'],'weight');
- array_multisort($weight, SORT_DESC, $menuTree[$itemId]['child']);
- }else{
- $node['action']=$child;
- $this->buildForwardTree($node['pid'],$node,$items,$menuTree);
- }
- }
- public function GetMenuActionTree($pid=0){
- $result = $this->field('menu_name,menu_img,menu_route,menu_url,status,pid,weight,id')
- ->where(['pid'=>$pid,'is_del'=>0])
- ->order(['weight'=>'desc','id'=>'desc'])->select();
- $remp=[];
- if($result->isEmpty())return $remp;
- foreach ($result as $item){
- $item->child = $this->GetMenuActionTree($item->id);
- if($item->pid>0){
- $item->action = Action::with(['ActionInfo'])->field('id,action_code')->where
- (['menuid'=>$item->id,'is_del'=>0])->select();
- }
- $remp[] = $item;
- }
- return $remp;
- }
- }
|