AllowOriginMiddleware.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2018-2020 rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: TABLE ME
  8. // +----------------------------------------------------------------------
  9. // | Date: 2020-08-30 14:59
  10. // +----------------------------------------------------------------------
  11. namespace app\warehouse\middleware;
  12. use app\Request;
  13. use library\interfaces\MiddlewareInterface;
  14. use \think\facade\Config;
  15. use think\Response;
  16. class AllowOriginMiddleware implements MiddlewareInterface {
  17. public function handle(Request $request, \Closure $next)
  18. {
  19. $origin = $request->header('origin');
  20. $header['Access-Control-Allow-Origin'] = '*';
  21. $header['Access-Control-Allow-Methods'] = 'POST,GET,JSON,OPTIONS,DELETE';
  22. $header['Access-Control-Allow-Headers'] = 'x-requested-with,content-type,WAREHOUSE-TOKEN,SECRET-KEY,SYSTEM-ACC-TOKEN,TOKEN,'. Config::get('cookie.admin_token_name','');
  23. if ($request->method(true) == 'OPTIONS') {
  24. $response = Response::create('ok')->code(200)->header($header);
  25. } else {
  26. $response = $next($request)->header($header);
  27. }
  28. return $response;
  29. }
  30. }