UserFriendService.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. namespace App\Services;
  3. use App\Exceptions\UserFriendException;
  4. use App\Models\FriendGroup;
  5. use App\Models\User;
  6. use App\Models\UserFriend;
  7. use Illuminate\Support\Arr;
  8. use Illuminate\Support\Facades\DB;
  9. use phpDocumentor\Reflection\Types\Self_;
  10. class UserFriendService
  11. {
  12. /*
  13. * 通过类型获取对应列表
  14. */
  15. const FRIEND_USER_LIST_FUNCTION = [
  16. UserFriend::FRIEND_TYPE_100 => 'getFriendsByAll',
  17. UserFriend::FRIEND_TYPE_101 => 'getFriendsByAddrBook',
  18. UserFriend::FRIEND_TYPE_102 => 'getFriendsByAll',
  19. ];
  20. public function bind(User $user, $toUserId)
  21. {
  22. //记录 A TO B
  23. try {
  24. $friendUser = User::query()->where('tencent_im_user_id', $toUserId)->first();
  25. if (!$friendUser) {
  26. throw new UserFriendException('好友不存在');
  27. }
  28. if ($user->id == $friendUser->id) {
  29. throw new UserFriendException('不能添加自己为好友!');
  30. }
  31. if (!$friendUser) {
  32. throw new UserFriendException('好友不存在');
  33. }
  34. DB::beginTransaction();
  35. $fromUser = UserFriend::firstOrCreate(
  36. ['user_id' => $user->id, 'friend_user_id' => $friendUser->id, 'wh_group_id' => FriendGroup::SOCIETY_GROUP]
  37. );
  38. //记录 B TO A
  39. $toUser = UserFriend::firstOrCreate(
  40. ['user_id' => $friendUser->id, 'friend_user_id' => $user->id, 'wh_group_id' => FriendGroup::SOCIETY_GROUP]
  41. );
  42. if (!$fromUser || !$toUser) {
  43. throw new UserFriendException('记录好友关系失败!');
  44. }
  45. DB::commit();
  46. return true;
  47. } catch (UserFriendException $e) {
  48. DB::rollBack();
  49. return ['error' => $e->getMessage()];
  50. } catch (\Exception $e) {
  51. DB::rollBack();
  52. return ['error' => $e->getMessage()];
  53. }
  54. }
  55. public function lists(User $user, int $listType, array $queryOptions)
  56. {
  57. try {
  58. self::verifyListType($listType);
  59. $action = self::FRIEND_USER_LIST_FUNCTION[$listType];
  60. $lists = $this->{$action}($user, $queryOptions);
  61. return $lists;
  62. } catch (UserFriendException $e) {
  63. return ['error' => $e->getMessage()];
  64. } catch (\Exception $e) {
  65. return ['error' => $e->getMessage()];
  66. }
  67. }
  68. /**
  69. * 获取所有好友列表 可通过分圈筛选
  70. * @param User $user
  71. * @param array $queryOptions
  72. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
  73. */
  74. static public function getFriendsByAll(User $user, array $queryOptions)
  75. {
  76. $lists = UserFriend::query()
  77. ->where('user_id', $user->id)
  78. ->when($queryOptions['friend_group_id'] ?? false, function ($res) use ($queryOptions) {
  79. return $res->where('wh_group_id', $queryOptions['friend_group_id']);
  80. })
  81. ->with('friend_to_user:id,tencent_im_user_id,nick_name_id,avatar,mobile,created_at,updated_at')
  82. ->orderByDesc('updated_at')
  83. ->paginate(request('perPage', 10));
  84. return $lists;
  85. }
  86. // static public function getFriendsByWhGroup(User $user, array $queryOptions)
  87. // {
  88. //
  89. // }
  90. /**
  91. * 通过通讯录获取好友列表
  92. * @param User $user
  93. * @param array $addrBooks
  94. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
  95. */
  96. static public function getFriendsByAddrBook(User $user, array $queryOptions)
  97. {
  98. $addrBooks = self::formatAddrBookData($queryOptions['addr_book'] ?? '');
  99. $userMobile = $user->mobile;
  100. //删除自己
  101. unset($addrBooks[array_search($userMobile, $addrBooks, true)]);
  102. $lists = User::query()
  103. ->whereIn('mobile', $addrBooks)
  104. ->select('id', 'tencent_im_user_id', 'nick_name_id', 'avatar', 'mobile', 'created_at', 'updated_at')
  105. ->with(['user_friends' => function ($res) use ($user) {
  106. return $res->where('user_id', $user->id);
  107. }])
  108. ->orderByDesc('updated_at')
  109. // ->get();
  110. ->paginate(request('perPage', 10));
  111. return $lists;
  112. }
  113. /**
  114. * 验证好友列表请求类型
  115. * @param int $listType
  116. * @return bool
  117. * @throws UserFriendException
  118. */
  119. static public function verifyListType(int $listType)
  120. {
  121. if (!array_key_exists($listType, self::FRIEND_USER_LIST_FUNCTION)) {
  122. throw new UserFriendException('获取好友列表类型错误');
  123. }
  124. return true;
  125. }
  126. /**
  127. * 格式化通讯录数据
  128. * @param string $addrBookJsonStr
  129. * @return array
  130. * @throws UserFriendException
  131. */
  132. static public function formatAddrBookData(string $addrBookJsonStr)
  133. {
  134. $addrBook = json_decode($addrBookJsonStr, true);
  135. if (isset($addrBook['addr_book'])) {
  136. $addrBook = $addrBook['addr_book'];
  137. }
  138. if (!isset($addrBook[0]['contact_name']) || !isset($addrBook[0]['mobile'])) {
  139. throw new UserFriendException('通讯录数据错误,或解析失败');
  140. }
  141. $mobiles = Arr::pluck($addrBook, 'mobile');
  142. return $mobiles;
  143. }
  144. /**
  145. * @param int $uid
  146. * @return array
  147. * @author JYF 2021/2/19 16:08
  148. */
  149. public static function getFriendAllByUid(int $uid)
  150. {
  151. return UserFriend::query()->where('user_id', $uid)->select('user_id', 'wh_group_id', 'friend_user_id')->get()->toArray();
  152. }
  153. /**
  154. * 获取好友关系
  155. * @param int $user_id
  156. * @param int $friend_user_id
  157. * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
  158. * @author JYF 2021/2/19 17:22
  159. */
  160. public static function getFriendRelation(int $user_id, int $friend_user_id)
  161. {
  162. $where = [];
  163. $where[] = ['user_id', '=', $user_id];
  164. $where[] = ['friend_user_id', '=', $friend_user_id];
  165. return UserFriend::query()->where($where)->first();
  166. }
  167. /**
  168. * @param int $uid
  169. * @return array
  170. * @author JYF 2021/2/19 20:02
  171. */
  172. public static function getFriendAllByFriendUserId(int $uid)
  173. {
  174. return UserFriend::query()->where('friend_user_id', $uid)->select('user_id', 'wh_group_id', 'friend_user_id')->get()->toArray();
  175. }
  176. }