AllowOriginMiddleware.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. User::direct_push();//直推奖励
  54. try {
  55. Db::startTrans();
  56. User::kpi();//KPi奖励
  57. Db::commit();
  58. } catch (\Exception $e) {
  59. Db::rollback();
  60. }
  61. try {
  62. Db::startTrans();
  63. User::bonus();//分红奖励
  64. Db::commit();
  65. } catch (\Exception $e) {
  66. Db::rollback();
  67. }
  68. try {
  69. Db::startTrans();
  70. User::direct_push();//直推奖励
  71. Db::commit();
  72. } catch (\Exception $e) {
  73. Db::rollback();
  74. }
  75. try {
  76. Db::startTrans();
  77. User::up_time();// 时间更新
  78. Db::commit();
  79. } catch (\Exception $e) {
  80. Db::rollback();
  81. }
  82. try {
  83. Db::startTrans();
  84. AuctionOrder::deduction();// 更新订单
  85. Db::commit();
  86. } catch (\Exception $e) {
  87. Db::rollback();
  88. }
  89. try {
  90. Db::startTrans();
  91. AuctionOrder::th();// 退回广告值
  92. Db::commit();
  93. } catch (\Exception $e) {
  94. Db::rollback();
  95. }
  96. return $response;
  97. }
  98. }