61 lines
2.1 KiB
PHP
61 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace app\common\middleware;
|
|
|
|
use think\Response;
|
|
use think\Request;
|
|
|
|
/**
|
|
* 公共跨域中间件
|
|
* 供所有应用共用
|
|
*/
|
|
class AllowCrossDomain
|
|
{
|
|
public function handle(Request $request, \Closure $next): Response
|
|
{
|
|
$origin = $request->header('origin', '');
|
|
|
|
$allowedDomains = [
|
|
'http://localhost:3000',
|
|
'http://localhost:8000',
|
|
'http://127.0.0.1:3000',
|
|
'http://127.0.0.1:8000',
|
|
'http://localhost:5173',
|
|
'http://backapi.yunzer.cn',
|
|
'http://www.yunzer.cn',
|
|
'https://www.yunzer.cn',
|
|
'http://yunzer.cn',
|
|
'https://yunzer.cn',
|
|
'http://back.yunzer.cn',
|
|
'https://back.yunzer.cn',
|
|
'http://backend.yunzer.cn',
|
|
'https://backend.yunzer.cn',
|
|
];
|
|
|
|
if (in_array($origin, $allowedDomains)) {
|
|
$header['Access-Control-Allow-Origin'] = $origin;
|
|
} else {
|
|
if (!empty($origin)) {
|
|
$header['Access-Control-Allow-Origin'] = $origin;
|
|
} else {
|
|
$header['Access-Control-Allow-Origin'] = '*';
|
|
}
|
|
}
|
|
|
|
if ($request->method() === 'OPTIONS') {
|
|
$header['Access-Control-Allow-Credentials'] = 'true';
|
|
$header['Access-Control-Max-Age'] = 1800;
|
|
$header['Access-Control-Allow-Methods'] = 'GET, POST, PATCH, PUT, DELETE, OPTIONS';
|
|
$header['Access-Control-Allow-Headers'] = 'Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, X-CSRF-TOKEN, X-Requested-With';
|
|
return response('', 200, $header);
|
|
}
|
|
|
|
$header['Access-Control-Allow-Credentials'] = 'true';
|
|
$header['Access-Control-Max-Age'] = 1800;
|
|
$header['Access-Control-Allow-Methods'] = 'GET, POST, PATCH, PUT, DELETE, OPTIONS';
|
|
$header['Access-Control-Allow-Headers'] = 'Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, X-CSRF-TOKEN, X-Requested-With';
|
|
|
|
return $next($request)->header($header);
|
|
}
|
|
}
|