VisitProductMiddleware.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 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\services\SwooleTaskService;
  16. use think\Response;
  17. class VisitProductMiddleware extends BaseMiddleware
  18. {
  19. public function before(Request $request)
  20. {
  21. // TODO: Implement before() method.
  22. }
  23. public function after(Response $response)
  24. {
  25. $id = intval($this->request->param('id'));
  26. $type = $this->getArg(0);
  27. if ($this->request->isLogin() && $id) {
  28. $uid = $this->request->uid();
  29. $make = app()->make(UserHistoryRepository::class);
  30. $data = [
  31. 'uid' => $uid,
  32. 'res_type' => 1,
  33. 'id' => $id,
  34. 'product_type' => $type
  35. ];
  36. $spu = $make->createOrUpdate($data);
  37. if ($spu) {
  38. $make = app()->make(UserVisitRepository::class);
  39. $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();
  40. if (!$count) {
  41. SwooleTaskService::visit(intval($uid), $spu['product_id'], 'product');
  42. }
  43. }
  44. }
  45. }
  46. }