UserRelationService.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. declare(strict_types=1);
  3. namespace Modules\Api\Services;
  4. use App\Model\Mongo\UserRelationModel;
  5. use App\Model\Mongo\VideoLikeModel;
  6. class UserRelationService
  7. {
  8. /**
  9. * 获取关注/被关注总数
  10. * @param $type
  11. * @param $userId
  12. * @return integer
  13. */
  14. public function getTotal($type, $userId)
  15. {
  16. if ($type == 'flow') {
  17. $data = [
  18. 'from_user_id' => $userId
  19. ];
  20. } else {
  21. $data = [
  22. 'to_user_id' => $userId
  23. ];
  24. }
  25. return make(UserRelationModel::class)->db()->countDocuments($data);
  26. }
  27. /**
  28. * 检查是否关注
  29. * @param $userId
  30. * @param $toUserId
  31. * @return integer
  32. */
  33. public function check($userId, $toUserId)
  34. {
  35. $key = md5('favorite_' . $userId . '_' . $toUserId);
  36. $result = make(UserRelationModel::class)->db()
  37. ->countDocuments(['_id' => $key]);
  38. if ($result) {
  39. return 1;
  40. }
  41. return 0;
  42. }
  43. /**
  44. * 获取点赞数
  45. * @param $userId
  46. * @param $toUserId
  47. * @return integer
  48. */
  49. public function getLike($userId)
  50. {
  51. return make(VideoLikeModel::class)->db()->countDocuments(['uid' => $userId]);
  52. }
  53. }