AllowOriginMiddleware.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace app\http\middleware;
  3. use app\models\auction\Auction;
  4. use app\models\auction\AuctionOrder;
  5. use app\models\auction\AuctionProduct;
  6. use app\models\auction\AuctionTime;
  7. use app\Request;
  8. use crmeb\interfaces\MiddlewareInterface;
  9. use think\facade\Config;
  10. use think\facade\Db;
  11. use think\Response;
  12. /**
  13. * 跨域中间件
  14. * Class AllowOriginMiddleware
  15. * @package app\http\middleware
  16. */
  17. class AllowOriginMiddleware implements MiddlewareInterface
  18. {
  19. /**
  20. * header头
  21. * @var array
  22. */
  23. protected $header = [
  24. 'Access-Control-Allow-Origin' => '*',
  25. 'Access-Control-Allow-Headers' => 'Authori-zation,Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, X-Requested-With',
  26. 'Access-Control-Allow-Methods' => 'GET,POST,PATCH,PUT,DELETE,OPTIONS,DELETE',
  27. 'Access-Control-Max-Age' => '1728000'
  28. ];
  29. /**
  30. * 允许跨域的域名
  31. * @var string
  32. */
  33. protected $cookieDomain;
  34. /**
  35. * @param Request $request
  36. * @param \Closure $next
  37. * @return Response
  38. */
  39. public function handle(Request $request, \Closure $next)
  40. {
  41. $this->cookieDomain = Config::get('cookie.domain', '');
  42. $header = $this->header;
  43. $origin = $request->header('origin');
  44. if ($origin && ('' != $this->cookieDomain && strpos($origin, $this->cookieDomain)))
  45. $header['Access-Control-Allow-Origin'] = $origin;
  46. if ($request->method(true) == 'OPTIONS') {
  47. $response = Response::create('ok')->code(200)->header($header);
  48. } else {
  49. $response = $next($request)->header($header);
  50. }
  51. $request->filter(['htmlspecialchars', 'strip_tags', 'addslashes', 'trim']);
  52. Auction::frequency(); // 更新场次
  53. AuctionProduct::off_the_shelf(); // 下架未出售商品
  54. try {
  55. Db::startTrans();
  56. AuctionOrder::deduction();//订单一个小时内未上传扣除预约卷
  57. Db::commit();
  58. } catch (\Exception $e) {
  59. Db::rollback();
  60. }
  61. try {
  62. Db::startTrans();
  63. AuctionOrder::th();//退回预约卷
  64. Db::commit();
  65. } catch (\Exception $e) {
  66. Db::rollback();
  67. }
  68. return $response;
  69. }
  70. }