UserVisitServices.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\services\user;
  13. use app\services\BaseServices;
  14. use app\dao\user\UserVisitDao;
  15. use think\facade\Log;
  16. /**
  17. *
  18. * Class UserVisitServices
  19. * @package app\services\user
  20. * @method count(array $where)
  21. * @method getDistinctCount(array $where, $field, ?bool $search = true)
  22. * @method sum(array $where, string $field)
  23. * @method getTrendData($time, $type, $timeType, $str)
  24. * @method getRegion($time, $channelType)
  25. * @method int groupCount(array $where, string $group = 'uid') 根据分组获取记录条数
  26. */
  27. class UserVisitServices extends BaseServices
  28. {
  29. /**
  30. * UserVisitServices constructor.
  31. * @param UserVisitDao $dao
  32. */
  33. public function __construct(UserVisitDao $dao)
  34. {
  35. $this->dao = $dao;
  36. }
  37. /**
  38. * 登录后记录访问记录
  39. * @param array|object $user
  40. * @return mixed
  41. */
  42. public function loginSaveVisit($user)
  43. {
  44. try {
  45. $data = [
  46. 'url' => '/pages/index/index',
  47. 'uid' => $user['uid'] ?? 0,
  48. 'ip' => request()->ip(),
  49. 'add_time' => time(),
  50. 'province' => $user['province'] ?? '',
  51. 'channel_type' => $user['user_type'] ?? 'h5'
  52. ];
  53. if (!$data['uid']) {
  54. return false;
  55. }
  56. return $this->dao->save($data);
  57. } catch (\Throwable $e) {
  58. Log::error('登录记录访问日志错误,错误原因:' . $e->getMessage());
  59. }
  60. }
  61. }