<?php
// +----------------------------------------------------------------------
// | [ 默认跨域中间链  ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2020 rights reserved.
// +----------------------------------------------------------------------
// | Author: TABLE ME
// +----------------------------------------------------------------------
// | Date: 2020-08-25 17:10
// +----------------------------------------------------------------------


namespace library\middleware;

use app\Request;
use library\interfaces\MiddlewareInterface;
use think\Response;

class AllowOriginMiddleware implements MiddlewareInterface
{


    public function handle(Request $request, \Closure $next)
    {
        $origin = $request->header('origin');
        $header['Access-Control-Allow-Origin'] = $origin;
        if ($request->method(true) == 'OPTIONS') {
            $response = Response::create('ok')->code(200)->header($header);
        } else {
            $response = $next($request)->header($header);
        }
        return $response;
    }
}