|
@@ -0,0 +1,65 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+ * Created by PhpStorm.
|
|
|
+ * User: Administrator
|
|
|
+ * Date: 2021/4/9
|
|
|
+ * Time: 17:51
|
|
|
+ */
|
|
|
+declare (strict_types = 1);
|
|
|
+
|
|
|
+namespace app\middleware;
|
|
|
+
|
|
|
+use Closure;
|
|
|
+use think\Config;
|
|
|
+use think\Request;
|
|
|
+use think\Response;
|
|
|
+
|
|
|
+class AllowCrossDomain
|
|
|
+{
|
|
|
+
|
|
|
+ protected $cookieDomain;
|
|
|
+
|
|
|
+
|
|
|
+ protected $header = [
|
|
|
+ 'Access-Control-Allow-Credentials' => 'true',
|
|
|
+ 'Access-Control-Max-Age' => 1800,
|
|
|
+ 'Access-Control-Allow-Methods' => 'GET, POST, PATCH, PUT, DELETE, OPTIONS',
|
|
|
+ 'Access-Control-Allow-Origin' =>'*'
|
|
|
+
|
|
|
+ ];
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ * AllowCrossDomain constructor.
|
|
|
+ * @param Config $config
|
|
|
+ */
|
|
|
+ public function __construct(Config $config)
|
|
|
+ {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 允许跨域请求
|
|
|
+ * @access public
|
|
|
+ * @param 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);
|
|
|
+ }
|
|
|
+}
|