AllowOriginMiddleware.php 2.3 KB

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