超管后台 - 调整所有的fetch 请求为后封装的 apiRequest,实现cookie 跨域

This commit is contained in:
柳清爽
2025-04-28 15:39:05 +08:00
parent 0eb629df70
commit 35341335d3
15 changed files with 459 additions and 475 deletions

View File

@@ -14,46 +14,46 @@ class AllowCrossDomain
*/
public function handle($request, \Closure $next)
{
// 获取当前请求的域名
$origin = $request->header('origin');
// 当请求使用 credentials 模式时,不能使用通配符
// 必须指定具体的域名或提取请求中的 Origin
$allowOrigin = '*';
if ($origin) {
// 如果需要限制特定域名,可以在这里判断
// 以下是允许的域名列表,如果请求来自这些域名之一,则允许跨域
$allowDomains = [ /* */ ];
// 如果请求来源在允许列表中,直接使用该源
if (in_array($origin, $allowDomains)) {
$allowOrigin = $origin;
}
}
// 设置允许的请求头信息
$allowHeaders = [
'Authorization', 'Content-Type', 'If-Match', 'If-Modified-Since',
'If-None-Match', 'If-Unmodified-Since', 'X-Requested-With',
'X-Token', 'X-Api-Token', 'Accept', 'Origin'
];
$response = $next($request);
// 添加跨域响应头
$response->header([
'Access-Control-Allow-Origin' => $allowOrigin,
'Access-Control-Allow-Headers' => implode(', ', $allowHeaders),
'Access-Control-Allow-Methods' => 'GET, POST, PUT, DELETE, OPTIONS',
'Access-Control-Allow-Credentials' => 'true',
'Access-Control-Max-Age' => '86400',
]);
// 对于预检请求,直接返回成功响应
if ($request->method(true) == 'OPTIONS') {
return response()->code(200);
}
return $response;
// // 获取当前请求的域名
// $origin = $request->header('origin');
//
// // 当请求使用 credentials 模式时,不能使用通配符
// // 必须指定具体的域名或提取请求中的 Origin
// $allowOrigin = '*';
// if ($origin) {
// // 如果需要限制特定域名,可以在这里判断
// // 以下是允许的域名列表,如果请求来自这些域名之一,则允许跨域
// $allowDomains = [ /* */ ];
//
// // 如果请求来源在允许列表中,直接使用该源
// if (in_array($origin, $allowDomains)) {
// $allowOrigin = $origin;
// }
// }
//
// // 设置允许的请求头信息
// $allowHeaders = [
// 'Authorization', 'Content-Type', 'If-Match', 'If-Modified-Since',
// 'If-None-Match', 'If-Unmodified-Since', 'X-Requested-With',
// 'X-Token', 'X-Api-Token', 'Accept', 'Origin'
// ];
//
// $response = $next($request);
//
// // 添加跨域响应头
// $response->header([
// 'Access-Control-Allow-Origin' => $allowOrigin,
// 'Access-Control-Allow-Headers' => implode(', ', $allowHeaders),
// 'Access-Control-Allow-Methods' => 'GET, POST, PUT, DELETE, OPTIONS',
// 'Access-Control-Allow-Credentials' => 'true',
// 'Access-Control-Max-Age' => '86400',
// ]);
//
// // 对于预检请求,直接返回成功响应
// if ($request->method(true) == 'OPTIONS') {
// return response()->code(200);
// }
//
// return $response;
}
}