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