nk-lihaink-cn/app/api/middleware/AllowOriginMiddleware.php

60 lines
1.9 KiB
PHP
Raw Normal View History

2023-01-30 16:33:01 +08:00
<?php
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
declare(strict_types=1);
namespace app\api\middleware;
use Closure;
use think\Config;
use think\Request;
use think\Response;
/**
* 跨域中间件
* Class AllowOriginMiddleware
* @package app\http\middleware
*/
class AllowOriginMiddleware
{
protected $cookieDomain;
protected $header = [
'Access-Control-Allow-Origin'=>'*',
'Access-Control-Max-Age' => 1800,
'Access-Control-Allow-Methods' => 'GET,POST,PATCH,PUT,DELETE,OPTIONS',
'Access-Control-Allow-Headers' => 'X-Token, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, X-Requested-With,Form-type,Referer,Connection,Content-Length,Host,Origin,Authorization,Authori-zation,Accept,Accept-Encoding',
];
public function __construct(\think\Config $config)
{
$this->cookieDomain = $config->get('cookie.domain', '');
}
/**
* 允许跨域请求
* @access public
* @param \think\Request $request
* @param Closure $next
* @param array $header
* @return Response
*/
public function handle($request, Closure $next, ? array $header = [])
{
$header = !empty($header) ? array_merge($this->header, $header) : $this->header;
return $next($request)->header($header);
}
}