| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 | <?phpnamespace app\common\model;use think\Db;use tools\Phptree;class Menu extends Base{    protected $autoWriteTimestamp = false;    public function updates(){        $data = request()->post();        return $this->updateInfo($data,'Menu','');    }    // 获取所有权限    public function getAllMenuTree(){        $map[] = ['id','not in',[37,38,39,47,48]];        $map[] = ['del','=',0];        $lists = Db::name('menu')            ->where('enable',1)            ->where($map)            ->field('id,title,pid')            ->order('sort asc,id asc')            ->select();        $lists = $lists?$lists:[];        foreach ($lists as $k=>$v){            $pids = [];            $pid = $v['pid'];            while (true){                if($pid == 0){                    break;                }else{                    $pids[] = 'sub_'.$pid;                    $cc = Db::name('menu')                        ->where('id',$pid)                        ->find();                    if(!$cc){                        break;                    }                    $pid = $cc['pid'];                }            }            $lists[$k]['pids'] = $pids?implode(' ',$pids):'';        }        $tree = [];        if($lists){            $tree = Phptree::makeTree(($lists), array(                'primary_key'=>'id',                'parent_key'=>'pid',                'expanded' => true,                'children_key' => 'children'            ));        }        return $tree;    }    // 获取某组织的所有权限    public function getOrgAllMenuTree($orgId){        $auths = model('Org')->getOrgAuths($orgId,1);        $lists = [];        if($auths){            $map[] = ['id','not in',[37,38,39,47,48]];            $map[] = ['del','=',0];            $lists = Db::name('menu')                ->where('enable',1)                ->where('id','in',$auths)                ->where($map)                ->field('id,title,pid')                ->order('sort asc,id asc')                ->select();            $lists = $lists?$lists:[];            foreach ($lists as $k=>$v){                $pids = [];                $pid = $v['pid'];                while (true){                    if($pid == 0){                        break;                    }else{                        $pids[] = 'sub_'.$pid;                        $cc = Db::name('menu')                            ->where('id',$pid)                            ->find();                        if(!$cc){                            break;                        }                         $pid = $cc['pid'];                    }                }                $lists[$k]['pids'] = $pids?implode(' ',$pids):'';            }        }        $tree = [];        if($lists){            $tree = Phptree::makeTree(($lists), array(                'primary_key'=>'id',                'parent_key'=>'pid',                'expanded' => true,                'children_key' => 'children'            ));        }        return $tree;    }    public function getOrgAllMenuTree_new($orgId){        $auths = model('Org')->getOrgAuths($orgId,1);        $lists = [];        if($auths){            $map[] = ['id','not in',[37,38,39,47,48]];            $map[] = ['del','=',0];            $lists = Db::name('menu')                ->where('enable',1)                ->where('id','in',$auths)                ->where($map)                ->field('id,title,pid')                ->order('sort asc,id asc')                ->select();            $lists = $lists?$lists:[];//            foreach ($lists as $k=>$v){//                $pids = [];//                $pid = $v['pid'];//                while (true){//                    if($pid == 0){//                        break;//                    }else{//                        $pids[] = 'sub_'.$pid;//                        $cc = Db::name('menu')//                            ->where('id',$pid)//                            ->find();//                        if(!$cc){//                            break;//                        }//                        $pid = $cc['pid'];//                    }//                }//                $lists[$k]['pids'] = $pids?implode(' ',$pids):'';//            }        }        $tree = [];        if($lists){            $tree = Phptree::makeTree(($lists), array(                'primary_key'=>'id',                'parent_key'=>'pid',                'expanded' => true,                'children_key' => 'children'            ));        }        return $tree;    }    // 根据角色和组织获取用户菜单项    public function getMenuTree($rolesId,$orgId){        $map[] = ['enable','=',1];        $map[] = ['del','=',0];        $map[] = ['is_btn','=',0];        if($rolesId == 1){ // 超级管理员        }else if($rolesId == 2){ //总公司管理员            $auths = model('Org')->getOrgAuths($orgId,1);            if($auths){                $map[] = ['id','in',$auths];            }else{                $map[] = ['id','=',0];            }        }else if ($rolesId == 537){ //陪护管理员            $auths = Db::name('menu')->where('pid',191)->where('del',0)->where('enable',1)->column('id');            $auths[] = 191;            $map[] = ['id','in',$auths];        }        else{ // 项目管理员            $auths = model('Roles')->getRolesAuths($rolesId,1);            if($auths){                $map[] = ['id','in',$auths];            }else{                $map[] = ['id','=',0];            }        }        if($rolesId!=1){            /*3.只有admin管理员可见            系统设置: 菜单管理、配置管理、安卓版本管理、苹果版本管理、            模块管理、这些项目,在权限分配里面就不要出现了,不需要分配*/            $map[] = ['id','not in',[37,38,39,47,48]];        }        $lists = Db::name('menu')            ->where($map)            ->field('id,title,url,icons,pid')            ->order('sort asc,id asc')            ->select();        $lists = $lists?$lists:[];        $first = [            'id' => -1,            'title' => '系统首页',            'url' => $this->getNavUrl('Index/def'),            'icons' => 'fa fa-home',            'pid' => 0,        ];        $arr[] = $first;        foreach ($lists as $k=>$v){            $v['url'] = $this->getNavUrl($v['url']);            $arr[] = $v;        }        $tree = list_to_tree($arr, 'pid', 'child');        return $tree;    }    /**     * 获取导航URL     * @param  string $url 导航URL     * @return string      解析或的url     */    public function getNavUrl($url){        switch ($url) {            case 'http://' === substr($url, 0, 7):            case 'https://' === substr($url, 0, 8):            case '#' === substr($url, 0, 1):                break;            default:                $url = url($url);                break;        }        return $url;    }}
 |