You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

44 lines
1.1 KiB

<?php
declare (strict_types=1);
namespace app\middleware;
use think\App;
class AllowCrossDomain
{
protected $app;
public function __construct(App $app)
{
$this->app = $app;
}
/**
* 处理请求
*
* @param \think\Request $request
* @param \Closure $next
* @return Response
*/
public function handle($request, \Closure $next)
{
/** @var Response $response */
$response = $next($request);
// 支持跨域请求的host数组['a.cn', 'b.cn']
$corsHost = ['api.base.ahbmz.com', 'api.ahbmz.com', 'cms.ahbmz.com'];
// if (!empty($corsHost) && is_array($corsHost) && in_array($request->host(), $corsHost)) {
$response->header([
'Access-Control-Allow-Origin' => '*',
'Access-Control-Max-Age' => 1800,
'Access-Control-Allow-Methods' => 'GET, POST, PATCH, PUT, DELETE',
'Access-Control-Allow-Headers' => 'Authorization, Token, Content-Type, If-Match,If-Modified-Since, If-None-Match, If-Unmodified-Since, X-Requested-With',
]);
// }
return $response;
}
}