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. AuctionOrder::auction_time();
  57. try {
  58. Db::startTrans();
  59. AuctionOrder::deduction();//订单一个小时内未上传扣除广告值
  60. Db::commit();
  61. } catch (\Exception $e) {
  62. Db::rollback();
  63. }
  64. try {
  65. Db::startTrans();
  66. AuctionOrder::th();//退回广告值
  67. Db::commit();
  68. } catch (\Exception $e) {
  69. Db::rollback();
  70. }
  71. try {
  72. Db::startTrans();
  73. AuctionOrder::goods();//自动放货
  74. Db::commit();
  75. } catch (\Exception $e) {
  76. Db::rollback();
  77. }
  78. try {
  79. Db::startTrans();
  80. User::thaw();//解冻
  81. Db::commit();
  82. } catch (\Exception $e) {
  83. Db::rollback();
  84. }
  85. return $response;
  86. }
  87. }