AllowOriginMiddleware.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | [ 默认跨域中间链 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2018-2020 rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: TABLE ME
  8. // +----------------------------------------------------------------------
  9. // | Date: 2020-08-25 17:10
  10. // +----------------------------------------------------------------------
  11. namespace library\middleware;
  12. use app\Request;
  13. use library\interfaces\MiddlewareInterface;
  14. use think\Response;
  15. class AllowOriginMiddleware implements MiddlewareInterface
  16. {
  17. public function handle(Request $request, \Closure $next)
  18. {
  19. $origin = $request->header('origin');
  20. $header['Access-Control-Allow-Origin'] = $origin;
  21. if ($request->method(true) == 'OPTIONS') {
  22. $response = Response::create('ok')->code(200)->header($header);
  23. } else {
  24. $response = $next($request)->header($header);
  25. }
  26. return $response;
  27. }
  28. }