增加跨域支持

This commit is contained in:
545522390@qq.com 2019-01-17 16:22:29 +08:00
parent 4646375db5
commit fe1972c6f6
2 changed files with 38 additions and 15 deletions

View File

@ -0,0 +1,38 @@
<?php
namespace app\common\middleware;
use think\Request;
/**
* 跨域处理
* Class Auth
* @package app\admin\middleware
*/
class CORS
{
/**
* @param Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, \Closure $next)
{
$Origin = $request->header('Origin');
$Headers = $request->header('Access-Control-Request-Headers');
header('Access-Control-Allow-Origin: ' . $Origin);
header('Access-Control-Allow-Headers: ' . $Headers);
header('Access-Control-Allow-Credentials:true');
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
header('Access-Control-Max-Age: 1728000');
header('Content-Type:text/plain charset=UTF-8');
if (strtoupper($request->method()) == "OPTIONS") {
header('HTTP/1.0 204 No Content');
header('Content-Length:0');
header('status:204');
exit();
}
return $next($request);
}
}

View File

@ -25,21 +25,6 @@ class Auth
*/
public function handle($request, \Closure $next)
{
// $Origin = $request->header('Origin');
// $Headers = $request->header('Access-Control-Request-Headers');
// header('Access-Control-Allow-Origin: ' . $Origin);
// header('Access-Control-Allow-Headers: ' . $Headers);
// header('Access-Control-Allow-Credentials:true');
// header('Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE');
// header('Access-Control-Max-Age: 1728000');
// header('Content-Type:text/plain charset=UTF-8');
//
// if (strtoupper($request->method()) == "OPTIONS") {
// header('HTTP/1.0 204 No Content');
// header('Content-Length:0');
// header('status:204');
// exit();
// }
list($module, $controller, $action) = [$request->module(), $request->controller(), $request->action()];
$access = $this->buildAuth($node = NodeService::parseNodeStr("{$module}/{$controller}/{$action}"));
$currentOrganizationCode = $request->header('organizationCode');