<?
class role
{
	private $ddadmin;	
	private $duoduo;
	function __construct($ddadmin,$duoduo){
		$this->ddadmin=$ddadmin;
		$this->duoduo=$duoduo;
	}

	function get_roles($where=""){

		$roles=$this->duoduo->select_all("role","id,title","1=1 $where");
		$arr=[];
		foreach ($roles as $key => $value) {
			$arr[$value['id']]=$value['title'];
		}
		return $arr;

	}
  
   /**
    * [get_roles 获取角色内容]
    * @Author   huangxiaojian
    * @DateTime 2021-05-04T21:04:18+0800
    * @param    string                   $where [description]
    * @return   [type]                          [description]
    */
  	function get_role($id){
		return db()->get_table("role",$id);
	}
  public function qzmenu($id=0){

	$arr=$this->duoduo->select_all('menu','id,title,`parent_id`,node,`mod`,`act`,listorder','1="1" order by parent_id asc,listorder desc,`mod` desc');
	foreach($arr as $a){
		$menu=array();
		if($a['parent_id']==0){
			 $menu['title']=$a['title'];
			 $menu['spread']=0;
			 $menu['node']=$a['node'];
			 $menu['id']=$a['id'];

			 // $checkid=$this->duoduo->select("menu_access","id","role_id=".$id." and menu_id=".$a['id']);
			 // if ($checkid>0){
			 // 	 $menu['checked']=true;
			 // }
			 $menus[]=$menu;
			// $menus[$a['id']]['children']=array();
			// $menus[$a['id']]['title']=$a['title'];
			// $menus[$a['id']]['id']=$a['id'];
			// $menus[$a['id']]['node']=$a['node'];
		}
		else{

			foreach ($menus as $key => $value) {
				 if ($value['node']==$a['node'])
				 	$index=$key;
			}
		
			$menu['title']=$a['title'];
			$menu['spread']=1;
			$menu['id']=$a['id'];
			$menu['parent_id']=$a['parent_id'];
			 $checkid=$this->duoduo->select("menu_access","id","role_id=".$id." and menu_id=".$a['id']);
			 if ($checkid>0){

			 	 $menu['checked']=true;
			 }
		    $menus[$index]['children'][]=$menu;
		}
	}
	

	return $menus;
  }

  function check_auth(){
    if (MOD=="login") return true;
    if (ACT=="login") return true;
    if (ACT=="exit") return true;
    if (MOD=="index") return true;
    if (MOD=="app") return true;

 
  	$sql = "select a.* from " . BIAOTOU . "menu as a," . BIAOTOU . "menu_access as b where a.id=b.menu_id and b.role_id='" . $this->ddadmin['role_id'] . "' and a.mod<>'fun' order by a.listorder desc,a.sort desc,a.id asc";
  	
	$query = $this->duoduo->query($sql);
	while ($row = $this->duoduo->fetch_array($query)) {
		if ($row['parent_id'] == 0) {
			$menu_arr[$row['node']] = array();
			$parent_menu[$row['node']]['title']=$row['title'];
			$parent_menu[$row['node']]['hide']=$row['hide'];
		} else {
			$menu_arr[$row['node']][] = $row;
		}
	}
	$url_ok = 0;

	foreach ($menu_arr as $key => $row) {
		foreach ($row as $arr) {
			//print_r($arr);
			if (MOD==$arr['mod'] && ACT == $arr['act']) {
				$url_ok = 1;
			}
		}
	}
	
	if ($url_ok != 1) {
		return false;
	}else

	return true;
  }
}
?>