AllowOriginMiddleware.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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\models\user\User;
  7. use app\Request;
  8. use crmeb\interfaces\MiddlewareInterface;
  9. use think\facade\Config;
  10. use think\facade\Db;
  11. use think\Response;
  12. /**
  13. * 跨域中间件
  14. * Class AllowOriginMiddleware
  15. * @package app\http\middleware
  16. */
  17. class AllowOriginMiddleware implements MiddlewareInterface
  18. {
  19. /**
  20. * header头
  21. * @var array
  22. */
  23. protected $header = [
  24. 'Access-Control-Allow-Origin' => '*',
  25. 'Access-Control-Allow-Headers' => 'Authori-zation,Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, X-Requested-With',
  26. 'Access-Control-Allow-Methods' => 'GET,POST,PATCH,PUT,DELETE,OPTIONS,DELETE',
  27. 'Access-Control-Max-Age' => '1728000'
  28. ];
  29. /**
  30. * 允许跨域的域名
  31. * @var string
  32. */
  33. protected $cookieDomain;
  34. /**
  35. * @param Request $request
  36. * @param \Closure $next
  37. * @return Response
  38. */
  39. public function handle(Request $request, \Closure $next)
  40. {
  41. $this->cookieDomain = Config::get('cookie.domain', '');
  42. $header = $this->header;
  43. $origin = $request->header('origin');
  44. if ($origin && ('' != $this->cookieDomain && strpos($origin, $this->cookieDomain)))
  45. $header['Access-Control-Allow-Origin'] = $origin;
  46. if ($request->method(true) == 'OPTIONS') {
  47. $response = Response::create('ok')->code(200)->header($header);
  48. } else {
  49. $response = $next($request)->header($header);
  50. }
  51. $request->filter(['htmlspecialchars', 'strip_tags', 'addslashes', 'trim']);
  52. Auction::frequency(); // 更新场次
  53. try {
  54. Db::startTrans();
  55. User::kpi();//KPi奖励
  56. Db::commit();
  57. } catch (\Exception $e) {
  58. Db::rollback();
  59. }
  60. try {
  61. Db::startTrans();
  62. User::bonus();//分红奖励
  63. Db::commit();
  64. } catch (\Exception $e) {
  65. Db::rollback();
  66. }
  67. try {
  68. Db::startTrans();
  69. User::direct_push();//直推奖励
  70. Db::commit();
  71. } catch (\Exception $e) {
  72. Db::rollback();
  73. }
  74. try {
  75. Db::startTrans();
  76. User::up_time();// 时间更新
  77. Db::commit();
  78. } catch (\Exception $e) {
  79. Db::rollback();
  80. }
  81. try {
  82. Db::startTrans();
  83. AuctionOrder::deduction();// 更新订单
  84. Db::commit();
  85. } catch (\Exception $e) {
  86. Db::rollback();
  87. }
  88. try {
  89. Db::startTrans();
  90. AuctionOrder::th();// 退回广告值
  91. Db::commit();
  92. } catch (\Exception $e) {
  93. Db::rollback();
  94. }
  95. try {
  96. Db::startTrans();
  97. Auction::recovery(); // 收单
  98. Db::commit();
  99. } catch (\Exception $e) {
  100. Db::rollback();
  101. }
  102. return $response;
  103. }
  104. }