VisitProductMiddleware.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\common\middleware;
  12. use app\common\repositories\user\UserHistoryRepository;
  13. use app\common\repositories\user\UserVisitRepository;
  14. use app\Request;
  15. use crmeb\jobs\VisitProductJob;
  16. use crmeb\services\SwooleTaskService;
  17. use think\facade\Queue;
  18. use think\Response;
  19. class VisitProductMiddleware extends BaseMiddleware
  20. {
  21. public function before(Request $request)
  22. {
  23. // TODO: Implement before() method.
  24. }
  25. public function after(Response $response)
  26. {
  27. $id = intval($this->request->param('id'));
  28. $type = $this->getArg(0);
  29. if ($this->request->isLogin() && $id) {
  30. $make = app()->make(UserVisitRepository::class);
  31. $count = $make->search(['uid' =>$this->request->uid(), 'type' => 'product'])
  32. ->where('type_id', $id)
  33. ->whereTime('UserVisit.create_time', '>', date('Y-m-d H:i:s', strtotime('- 300 seconds')))->count();
  34. if (!$count) SwooleTaskService::visit(intval($this->request->uid()), $id, 'product');
  35. Queue::push(VisitProductJob::class, [
  36. 'uid' => $this->request->uid(),
  37. 'res_type' => 1,
  38. 'id' => $id,
  39. 'product_type' => $type
  40. ]);
  41. }
  42. }
  43. }