1
0

LikeAdminAllowMiddleware.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. declare (strict_types=1);
  15. namespace app\common\http\middleware;
  16. use Closure;
  17. /**
  18. * 自定义跨域中间件
  19. * Class LikeAdminAllowMiddleware
  20. * @package app\common\http\middleware
  21. */
  22. class LikeAdminAllowMiddleware
  23. {
  24. /**
  25. * @notes 跨域处理
  26. * @param $request
  27. * @param \Closure $next
  28. * @param array|null $header
  29. * @return mixed|\think\Response
  30. * @author 令狐冲
  31. * @date 2021/7/26 11:51
  32. */
  33. public function handle($request, Closure $next, ?array $header = [])
  34. {
  35. header('Access-Control-Allow-Origin: *');
  36. header("Access-Control-Allow-Headers: Authorization, Sec-Fetch-Mode, DNT, X-Mx-ReqToken, Keep-Alive, User-Agent, If-Match, If-None-Match, If-Unmodified-Since, X-Requested-With, If-Modified-Since, Cache-Control, Content-Type, Accept-Language, Origin, Accept-Encoding,Access-Token,token,version");
  37. header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE, post');
  38. header('Access-Control-Max-Age: 1728000');
  39. header('Access-Control-Allow-Credentials:true');
  40. if (strtoupper($request->method()) == "OPTIONS") {
  41. return response();
  42. }
  43. return $next($request);
  44. }
  45. }