一聚教程网:一个值得你收藏的教程网站

热门教程

thinkphp框架onethink后台支持三级菜单

时间:2022-06-25 02:26:09 编辑:袖梨 来源:一聚教程网

onethink后台支持三级菜单,CMS管理后台1.0版本本身不支持三级菜单,稍加改动,即可支持无限级分类


/**
 * 获取控制器菜单数组,二级菜单元素位于一级菜单的'_child'元素中
 * @author 朱亚杰 
 */
final public function getMenus($controller=CONTROLLER_NAME){
    // $menus  =   session('ADMIN_MENU_LIST'.$controller);
 
    $flag=false;//是否开启menu三级菜单
 
    if(empty($menus)){
        // 获取主菜单
        $where['pid']   =   0;
        $where['hide']  =   0;
        if(!C('DEVELOP_MODE')){ // 是否开发者模式
            $where['is_dev']    =   0;
        }
        $menus['main']  =   M('Menu')->where($where)->order('sort asc')->select();
 
        $menus['child'] = array(); //设置子节点
 
        //高亮主菜单
        $current = M('Menu')->where("url like '%{$controller}/".ACTION_NAME."%'")->field('id')->find();
        if($current){
            $nav = D('Menu')->getPath($current['id']);
            $nav_first_title = $nav[0]['title'];
 
            foreach ($menus['main'] as $key => $item) {
                if (!is_array($item) || empty($item['title']) || empty($item['url']) ) {
                    $this->error('控制器基类$menus属性元素配置有误');
                }
                if( stripos($item['url'],MODULE_NAME)!==0 ){
                    $item['url'] = MODULE_NAME.'/'.$item['url'];
                }
                // 判断主菜单权限
                if ( !IS_ROOT && !$this->checkRule($item['url'],AuthRuleModel::RULE_MAIN,null) ) {
                    unset($menus['main'][$key]);
                    continue;//继续循环
                }
 
                // 获取当前主菜单的子菜单项
                if($item['title'] == $nav_first_title){
                    $menus['main'][$key]['class']='current';
                    //生成child树
                    $groups = M('Menu')->where("pid = {$item['id']}")->distinct(true)->field("`group`")->select();
                    if($groups){
                        $groups = array_column($groups, 'group');
                    }else{
                        $groups =   array();
                    }
 
                    //获取二级分类的合法url
                    $where          =   array();
                    $where['pid']   =   $item['id'];
                    $where['hide']  =   0;
                    if(!C('DEVELOP_MODE')){ // 是否开发者模式
                        $where['is_dev']    =   0;
                    }
                    $second_urls = M('Menu')->where($where)->getField('id,url');
                    $third_urlss=array();
 
                    if(!empty($second_urls) && $flag){
                    foreach($second_urls as $key=>$v){
 
                        $wheres          =   array();
                        $wheres['pid']   =   $key;
                        $wheres['hide']  =   0;
                        if(!C('DEVELOP_MODE')){ // 是否开发者模式
                            $wheres['is_dev']    =   0;
                        }
                        $third_urls[] = M('Menu')->where($wheres)->getField('id,url');
                        foreach($third_urls as $vv){
                             foreach($vv as $key=>$vs){
                                 $third_urlss[$key]=$vs;
                             }
                        }
                    }
                    }
 
                    if(!IS_ROOT){
                        // 检测菜单权限
                        $to_check_urls = array();
 
                        foreach ($second_urls as $key=>$to_check_url) {
                            if( stripos($to_check_url,MODULE_NAME)!==0 ){
                                $rule = MODULE_NAME.'/'.$to_check_url;
                            }else{
                                $rule = $to_check_url;
                            }
                            if($this->checkRule($rule, AuthRuleModel::RULE_URL,null))
                                $to_check_urls[] = $to_check_url;
                        }
                        if(!empty($third_urlss) && $flag){
                            foreach ($third_urlss as $key=>$to_check_urlss) {
                                if( stripos($to_check_urlss,MODULE_NAME)!==0 ){
                                    $rules = MODULE_NAME.'/'.$to_check_urlss;
                                }else{
                                    $rules = $to_check_urlss;
                                }
                                if($this->checkRule($rules, AuthRuleModel::RULE_URL,null))
 
                                    echo $rules."
";
                                    $to_check_urlsss[] = $to_check_urlss;
                            }
                        }
 
                    }
 
                    // 按照分组生成子菜单树
                    foreach ($groups as $g) {
                        $map = array('group'=>$g);
                        if(isset($to_check_urls)){
                            if(empty($to_check_urls)){
                                // 没有任何权限
                                continue;
                            }else{
                                $map['url'] = array('in', $to_check_urls);
                            }
                        }
                        $map['pid'] =   $item['id'];
                        $map['hide']    =   0;
                        if(!C('DEVELOP_MODE')){ // 是否开发者模式
                            $map['is_dev']  =   0;
                        }
                        $menuList = M('Menu')->where($map)->field('id,pid,title,url,tip')->order('sort asc')->select();
 
                        $menus['child'][$g] = list_to_tree($menuList, 'id', 'pid', 'operater', $item['id']);
                        //支持三级菜单
                        if(!empty($menus['child'][$g]) && $flag){
                            foreach($menuList as $key=>$v){
                                $maps['pid'] =   $v['id'];
                                $menuLists = M('Menu')->where($maps)->field('id,pid,title,url,tip')->order('sort asc')->select();
                                $menus['child'][$g][$key]['ttt'] = list_to_tree($menuLists, 'id', 'pid', 'operater', $v['id']);
                            }
                        }
                    }
                    if($menus['child'] === array()){
                        //$this->error('主菜单下缺少子菜单,请去系统=》后台菜单管理里添加');
                    }
                }
            }
        }
        // session('ADMIN_MENU_LIST'.$controller,$menus);
    }
    return $menus;
}

然后Public/base.html


热门栏目