VisitProductMiddleware.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. namespace app\common\middleware;
  3. use app\common\repositories\user\UserHistoryRepository;
  4. use app\common\repositories\user\UserVisitRepository;
  5. use app\Request;
  6. use ln\services\SwooleTaskService;
  7. use think\Response;
  8. class VisitProductMiddleware extends BaseMiddleware
  9. {
  10. public function before(Request $request)
  11. {
  12. // TODO: Implement before() method.
  13. }
  14. public function after(Response $response)
  15. {
  16. $id = intval($this->request->param('id'));
  17. $type = $this->getArg(0);
  18. if ($this->request->isLogin() && $id) {
  19. $uid = $this->request->uid();
  20. $make = app()->make(UserHistoryRepository::class);
  21. $data = [
  22. 'uid' => $uid,
  23. 'res_type' => 1,
  24. 'id' => $id,
  25. 'product_type' => $type
  26. ];
  27. $spu = $make->createOrUpdate($data);
  28. if ($spu) {
  29. $make = app()->make(UserVisitRepository::class);
  30. $count = $make->search(['uid' => $uid, 'type' => 'product'])->where('type_id', $spu['product_id'])->whereTime('create_time', '>', date('Y-m-d H:i:s', strtotime('- 300 seconds')))->count();
  31. if (!$count) {
  32. SwooleTaskService::visit(intval($uid), $spu['product_id'], 'product');
  33. }
  34. }
  35. }
  36. }
  37. }