<?php
namespace app\api\middleware;
use think\Request;
use think\facade\Cache;
class CheckAllow
{
    /*
* 中间件验证redis中ip和token
*/
    protected $request;
    public function handle($request, \Closure $next)
    {
//        1.拿到token
        $data = $request->param();
//        2.获取ip
        $ip= $request->ip();
        
        $iip = @file_get_contents("https://opendata.baidu.com/api.php?query=".$ip."&co=&resource_id=6006&oe=utf8");

		$iip = json_decode($iip,true);
		
//        3.获取redis缓存token
       $token1 =  Cache::store('redis')->get($ip);
       if($token1){
//           存在该token,比对
           if($token1!=$data['token']){
               return json('非法请求！');
           }else{
               /*if($iip["status"]=="0"){
		            if (explode("省",$iip["data"][0]["location"])[0]!="安徽") {
				        if ($ip!="121.41.26.116"&$ip!="47.114.151.35"&$ip!="116.62.247.203") {
					        return json('非法请求！');
				            }else{
				             //               存在且相同，下一步请求
                                 return $next($request);
				        }
		        	}else{
				            return $next($request);
			       }
		}else{
		    return $next($request);
		}*/
return $next($request);
           }
       }else{
           return json('非法请求！');
       }
    }
}
