AllowCrossDomain.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace cores\middleware;
  13. use Closure;
  14. use think\Config;
  15. use think\Request;
  16. use think\Response;
  17. /**
  18. * 跨域请求支持
  19. * Class AllowCrossDomain
  20. * @package cores\middleware
  21. */
  22. class AllowCrossDomain
  23. {
  24. // cookie的所属域名
  25. protected $cookieDomain;
  26. /**
  27. * 构造方法
  28. * AllowCrossDomain constructor.
  29. * @param Config $config
  30. */
  31. public function __construct(Config $config)
  32. {
  33. $this->cookieDomain = $config->get('cookie.domain', '');
  34. }
  35. /**
  36. * 获取允许跨域的header参数 [自定义]
  37. * @return array
  38. */
  39. private function getCustomHeader()
  40. {
  41. return [
  42. 'Access-Token',
  43. 'storeId',
  44. 'platform',
  45. ];
  46. }
  47. /**
  48. * 获取允许跨域的header参数
  49. * @return array
  50. */
  51. private function getHeader()
  52. {
  53. $headers = array_merge([
  54. 'Authorization', 'Content-Type', 'X-CSRF-TOKEN', 'X-Requested-With',
  55. 'If-Match', 'If-Modified-Since', 'If-None-Match', 'If-Unmodified-Since'
  56. ], $this->getCustomHeader());
  57. return [
  58. // 允许所有域名访问
  59. 'Access-Control-Allow-Origin' => '*',
  60. // 允许cookie跨域访问
  61. 'Access-Control-Allow-Credentials' => 'true',
  62. // 预检请求的有效期
  63. 'Access-Control-Max-Age' => 1800,
  64. // 允许跨域的方法
  65. 'Access-Control-Allow-Methods' => 'GET, POST, PATCH, PUT, DELETE, OPTIONS',
  66. // 跨域请求header头
  67. 'Access-Control-Allow-Headers' => implode(',', $headers),
  68. ];
  69. }
  70. /**
  71. * 允许跨域请求
  72. * @access public
  73. * @param Request $request
  74. * @param Closure $next
  75. * @param array|null $header
  76. * @return Response
  77. */
  78. public function handle(Request $request, Closure $next, ?array $header = [])
  79. {
  80. $header = !empty($header) ? array_merge($this->getHeader(), $header) : $this->getHeader();
  81. if (!isset($header['Access-Control-Allow-Origin'])) {
  82. $origin = $request->header('origin');
  83. if ($origin && ('' == $this->cookieDomain || strpos($origin, $this->cookieDomain))) {
  84. $header['Access-Control-Allow-Origin'] = $origin;
  85. } else {
  86. $header['Access-Control-Allow-Origin'] = '*';
  87. }
  88. }
  89. return $next($request)->header($header);
  90. }
  91. }