AllowOriginMiddleware.php 2.2 KB

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