<?php
/**
 * @package merchant
 *
 * @author xaboy
 * @day 2020-03-24
 *
 *
 */

namespace app\common\middleware;


use app\Request;
use think\exception\ValidateException;
use think\facade\Log;
use think\Response;

class IpCheckMiddleware extends BaseMiddleware
{
    //地址列表,生产环境中通常会在redis中
    private $ipList = ['39.105.36.78','39.106.2.244'];

    public function before(Request $request)
    {
        //得到当前IP
//        $ip = $request->ip();
        $ip = $request->header('x-forwarded-for');
        Log::info("IP".$ip);
        //判断是否在列表中
        if(!in_array($ip,$this->ipList)){
            throw new ValidateException('IP非法23');
        }

    }

    public function after(Response $response)
    {
        // TODO: Implement after() method.
    }
}