You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UserFriendsServices.php 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace app\services\user;
  3. use app\dao\user\UserFriendsDao;
  4. use app\services\BaseServices;
  5. use app\services\user\level\SystemUserLevelServices;
  6. use crmeb\traits\ServicesTrait;
  7. /**
  8. * 获取好友列表
  9. * Class UserFriendsServices
  10. * @package app\services\user
  11. */
  12. class UserFriendsServices extends BaseServices
  13. {
  14. use ServicesTrait;
  15. public function __construct(UserFriendsDao $dao)
  16. {
  17. $this->dao = $dao;
  18. }
  19. /**
  20. * 保存好友关系
  21. * @param int $uid
  22. * @param int $friends_uid
  23. * @return bool
  24. * @throws \think\db\exception\DataNotFoundException
  25. * @throws \think\db\exception\DbException
  26. * @throws \think\db\exception\ModelNotFoundException
  27. */
  28. public function saveFriend(int $uid, int $friends_uid)
  29. {
  30. $data = [
  31. 'uid' => $uid,
  32. 'friends_uid' => $friends_uid
  33. ];
  34. $userFriend = $this->dao->get($data);
  35. $res1 = true;
  36. if (!$userFriend) {
  37. $data['add_time'] = time();
  38. $res1 = $this->dao->save($data);
  39. }
  40. return $res1;
  41. }
  42. /**
  43. * 获取好友uids 我推广的 推广我的
  44. * @param int $uid
  45. * @return array
  46. */
  47. public function getFriendUids(int $uid)
  48. {
  49. $result = [];
  50. if ($uid) {
  51. $spread = $this->dao->getColumn(['uid' => $uid], 'friends_uid');
  52. $sup_spread = $this->dao->getColumn(['friends_uid' => $uid], 'uid');
  53. $result = array_unique(array_merge($spread, $sup_spread));
  54. }
  55. return $result;
  56. }
  57. /**
  58. * 获取好友
  59. * @param int $id
  60. * @param string $field
  61. * @return array
  62. * @throws \think\db\exception\DataNotFoundException
  63. * @throws \think\db\exception\DbException
  64. * @throws \think\db\exception\ModelNotFoundException
  65. */
  66. public function getFriendList(int $uid, string $field = 'uid,nickname,level,add_time')
  67. {
  68. $uids = $this->getFriendUids($uid);
  69. $list = [];
  70. $count = 0;
  71. if ($uids) {
  72. [$page, $limit] = $this->getPageValue();
  73. /** @var UserServices $userServices */
  74. $userServices = app()->make(UserServices::class);
  75. $list = $userServices->getList(['uid' => $uids], $field, $page, $limit);
  76. /** @var SystemUserLevelServices $systemLevelServices */
  77. $systemLevelServices = app()->make(SystemUserLevelServices::class);
  78. $systemLevelList = $systemLevelServices->getWhereLevelList([], 'id,name');
  79. if ($systemLevelList) $systemLevelList = array_combine(array_column($systemLevelList, 'id'), $systemLevelList);
  80. foreach ($list as &$item) {
  81. $item['type'] = $systemLevelList[$item['level']]['name'] ?? '暂无';
  82. $item['add_time'] = $item['add_time'] && is_numeric($item['add_time']) ? date('Y-m-d H:i:s', $item['add_time']) : '';
  83. }
  84. $count = $userServices->count(['uid' => $uids]);
  85. }
  86. return compact('list', 'count');
  87. }
  88. }