<?php
//后台中间件  验证一切未登录判断。

namespace app\admin\middleware;
use think\facade\Session;

class Admin{
	
    public function handle($request, \Closure $next){
        // 获取当前用户
		$admin_id = Session::get('admin_id');
		if (empty($admin_id)) {
            echo "<script>top.location.href='/admin/Login/index.html';</script>";
			exit;
            //return redirect((string)url('Login/index'));
        }
		if($admin_id){
			$tq_info=\app\admin\model\Admin::admin_info(1,$admin_id);
			if($tq_info['is_effect']==2){
				Session::delete('admin_id');
			}
		}

		//后台节点权限,普通用户
		if($tq_info['is_super']==1){
			$auth_id=$tq_info['auth_id'];
			$auth_rid=\app\admin\model\Common::info($auth_id,'auth_group','rid');
			foreach($auth_rid['rid'] as $k=>$c){
				$auth_url=\app\admin\model\Common::info($c,'auth_rule');	
				$uu[$k]=$auth_url['url'];
			}
			$uu=implode("|", $uu);
			$server=$request->server();
			$url=$server['REDIRECT_URL'];
			$url_arr=explode('/',$url);
			if(isset($url_arr[2])==true){
				$url2=$url_arr[2];
				
				if($url2=='Rule'){
					$url2='Admin';
				}
				
				$is_in=strpos($uu,$url2);
				if($url2=='Index' or $url2=='Common'){
				}else{
					if($is_in===false){
						echo '您没有权限访问';exit;
					}
				}
			}
		}
		
		return $next($request);
	}
	
	
	
}