AllowOriginMiddleware.php 2.7 KB

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