| 1234567891011121314151617181920212223242526272829303132333435 |
- <?php
- namespace app\api\middleware;
- use think\Response;
- class AllowCrossDomain
- {
- public function handle($request, \Closure $next)
- {
- // 处理预检请求
- if ($request->isOptions()) {
- return response('', 204)
- ->header([
- 'Access-Control-Allow-Origin' => '*', // 或具体域名
- 'Access-Control-Allow-Methods' => 'GET,POST,PUT,DELETE,OPTIONS',
- 'Access-Control-Allow-Headers' => 'Content-Type,Authorization,Accept-Language,token',
- 'Access-Control-Max-Age' => '86400',
- ]);
- }
- // 继续后续请求
- $response = $next($request);
- // 设置跨域头
- $response->header([
- 'Access-Control-Allow-Origin' => '*', // 或 'https://sz-test-3.hxiaoju.top'
- 'Access-Control-Allow-Methods' => 'GET,POST,PUT,DELETE,OPTIONS',
- 'Access-Control-Allow-Headers' => 'Content-Type,Authorization,Accept-Language,token',
- 'Access-Control-Max-Age' => '86400',
- ]);
- return $response;
- }
- }
|