AllowOriginMiddleware.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace app\http\middleware;
  3. use app\models\many\Many;
  4. use app\models\many\ManyOrder;
  5. use app\models\user\User;
  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. try {
  51. Db::startTrans();
  52. Many::second();//自动增长
  53. Db::commit();
  54. } catch (\Exception $e) {
  55. Db::rollback();
  56. }
  57. try {
  58. Db::startTrans();
  59. User::agent();//区域代理发放
  60. Db::commit();
  61. } catch (\Exception $e) {
  62. Db::rollback();
  63. }
  64. try {
  65. Db::startTrans();
  66. ManyOrder::push(); //团队奖励
  67. Db::commit();
  68. } catch (\Exception $e) {
  69. Db::rollback();
  70. }
  71. try {
  72. Db::startTrans();
  73. ManyOrder::flowing_water(); //流水分红
  74. Db::commit();
  75. } catch (\Exception $e) {
  76. Db::rollback();
  77. }
  78. try {
  79. Db::startTrans();
  80. Many::fail();//众筹失败
  81. Db::commit();
  82. } catch (\Exception $e) {
  83. Db::rollback();
  84. }
  85. try {
  86. Db::startTrans();
  87. ManyOrder::suc_return(); // 众筹成功订单返还
  88. Db::commit();
  89. } catch (\Exception $e) {
  90. Db::rollback();
  91. }
  92. ManyOrder::time(); //流水分红
  93. $request->filter(['htmlspecialchars', 'strip_tags', 'addslashes', 'trim']);
  94. return $response;
  95. }
  96. }