FriendService.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. namespace App\Module\Friend\Services;
  3. use App\Module\Friend\Enums\ERROR_CODE;
  4. use App\Module\Friend\Enums\FRIEND_STATUS;
  5. use App\Module\Friend\Enums\READ_STATUS;
  6. use App\Module\Friend\Enums\REQUEST_STATUS;
  7. use App\Module\Friend\Logics\Friend as FriendLogic;
  8. use App\Module\Friend\Logics\Request as RequestLogic;
  9. use App\Module\Friend\Models\FriendRelation;
  10. use App\Module\Friend\Models\FriendRequest;
  11. use Illuminate\Pagination\LengthAwarePaginator;
  12. use Illuminate\Support\Collection;
  13. /**
  14. * 好友服务类
  15. *
  16. * 提供好友模块的对外服务接口,包括好友添加、删除、查询等功能。
  17. * 该类是好友模块与其他模块交互的桥梁,所有方法均为静态方法。
  18. */
  19. class FriendService
  20. {
  21. /**
  22. * 获取用户的好友列表
  23. *
  24. * @param int $userId 用户ID
  25. * @param int $page 页码
  26. * @param int $limit 每页数量
  27. * @param array $filters 过滤条件
  28. * @return array 好友列表数据
  29. */
  30. public static function getFriendList(int $userId, int $page = 1, int $limit = 20, array $filters = []): array
  31. {
  32. $logic = new FriendLogic();
  33. $result = $logic->getFriendList($userId, $page, $limit, $filters);
  34. return [
  35. 'code' => ERROR_CODE::SUCCESS,
  36. 'message' => '获取成功',
  37. 'data' => $result
  38. ];
  39. }
  40. /**
  41. * 搜索可添加为好友的用户
  42. *
  43. * @param int $userId 当前用户ID
  44. * @param string $keyword 搜索关键词
  45. * @param int $page 页码
  46. * @param int $limit 每页数量
  47. * @return array 搜索结果
  48. */
  49. public static function searchUsers(int $userId, string $keyword, int $page = 1, int $limit = 20): array
  50. {
  51. $logic = new FriendLogic();
  52. $result = $logic->searchUsers($userId, $keyword, $page, $limit);
  53. return [
  54. 'code' => ERROR_CODE::SUCCESS,
  55. 'message' => '搜索成功',
  56. 'data' => $result
  57. ];
  58. }
  59. /**
  60. * 发送好友申请
  61. *
  62. * @param int $senderId 发送者ID
  63. * @param int $receiverId 接收者ID
  64. * @param string|null $message 申请消息
  65. * @return array 操作结果
  66. */
  67. public static function sendFriendRequest(int $senderId, int $receiverId, ?string $message = null): array
  68. {
  69. // 参数验证
  70. if ($senderId === $receiverId) {
  71. return [
  72. 'code' => ERROR_CODE::PARAM_ERROR,
  73. 'message' => '不能添加自己为好友'
  74. ];
  75. }
  76. $logic = new RequestLogic();
  77. $result = $logic->sendFriendRequest($senderId, $receiverId, $message);
  78. if ($result['success']) {
  79. return [
  80. 'code' => ERROR_CODE::SUCCESS,
  81. 'message' => '申请发送成功'
  82. ];
  83. } else {
  84. return [
  85. 'code' => $result['code'],
  86. 'message' => $result['message']
  87. ];
  88. }
  89. }
  90. /**
  91. * 获取收到的好友申请列表
  92. *
  93. * @param int $userId 用户ID
  94. * @param int $page 页码
  95. * @param int $limit 每页数量
  96. * @return array 申请列表数据
  97. */
  98. public static function getReceivedRequests(int $userId, int $page = 1, int $limit = 20): array
  99. {
  100. $logic = new RequestLogic();
  101. $result = $logic->getReceivedRequests($userId, $page, $limit);
  102. return [
  103. 'code' => ERROR_CODE::SUCCESS,
  104. 'message' => '获取成功',
  105. 'data' => $result
  106. ];
  107. }
  108. /**
  109. * 获取发送的好友申请列表
  110. *
  111. * @param int $userId 用户ID
  112. * @param int $page 页码
  113. * @param int $limit 每页数量
  114. * @return array 申请列表数据
  115. */
  116. public static function getSentRequests(int $userId, int $page = 1, int $limit = 20): array
  117. {
  118. $logic = new RequestLogic();
  119. $result = $logic->getSentRequests($userId, $page, $limit);
  120. return [
  121. 'code' => ERROR_CODE::SUCCESS,
  122. 'message' => '获取成功',
  123. 'data' => $result
  124. ];
  125. }
  126. /**
  127. * 同意好友申请
  128. *
  129. * @param int $userId 当前用户ID
  130. * @param int $requestId 申请ID
  131. * @return array 操作结果
  132. */
  133. public static function acceptFriendRequest(int $userId, int $requestId): array
  134. {
  135. $logic = new RequestLogic();
  136. $result = $logic->acceptFriendRequest($userId, $requestId);
  137. if ($result['success']) {
  138. return [
  139. 'code' => ERROR_CODE::SUCCESS,
  140. 'message' => '已同意好友申请',
  141. 'data' => $result['data'] ?? null
  142. ];
  143. } else {
  144. return [
  145. 'code' => $result['code'],
  146. 'message' => $result['message']
  147. ];
  148. }
  149. }
  150. /**
  151. * 拒绝好友申请
  152. *
  153. * @param int $userId 当前用户ID
  154. * @param int $requestId 申请ID
  155. * @return array 操作结果
  156. */
  157. public static function rejectFriendRequest(int $userId, int $requestId): array
  158. {
  159. $logic = new RequestLogic();
  160. $result = $logic->rejectFriendRequest($userId, $requestId);
  161. if ($result['success']) {
  162. return [
  163. 'code' => ERROR_CODE::SUCCESS,
  164. 'message' => '已拒绝好友申请'
  165. ];
  166. } else {
  167. return [
  168. 'code' => $result['code'],
  169. 'message' => $result['message']
  170. ];
  171. }
  172. }
  173. /**
  174. * 删除好友
  175. *
  176. * @param int $userId 当前用户ID
  177. * @param int $friendId 好友ID
  178. * @return array 操作结果
  179. */
  180. public static function deleteFriend(int $userId, int $friendId): array
  181. {
  182. $logic = new FriendLogic();
  183. $result = $logic->deleteFriend($userId, $friendId);
  184. if ($result['success']) {
  185. return [
  186. 'code' => ERROR_CODE::SUCCESS,
  187. 'message' => '好友删除成功'
  188. ];
  189. } else {
  190. return [
  191. 'code' => $result['code'],
  192. 'message' => $result['message']
  193. ];
  194. }
  195. }
  196. /**
  197. * 更新好友备注
  198. *
  199. * @param int $userId 当前用户ID
  200. * @param int $friendId 好友ID
  201. * @param string $remark 备注名
  202. * @return array 操作结果
  203. */
  204. public static function updateFriendRemark(int $userId, int $friendId, string $remark): array
  205. {
  206. $logic = new FriendLogic();
  207. $result = $logic->updateFriendRemark($userId, $friendId, $remark);
  208. if ($result['success']) {
  209. return [
  210. 'code' => ERROR_CODE::SUCCESS,
  211. 'message' => '备注更新成功'
  212. ];
  213. } else {
  214. return [
  215. 'code' => $result['code'],
  216. 'message' => $result['message']
  217. ];
  218. }
  219. }
  220. /**
  221. * 检查是否是好友关系
  222. *
  223. * @param int $userId 用户ID
  224. * @param int $targetId 目标用户ID
  225. * @return bool 是否是好友
  226. */
  227. public static function isFriend(int $userId, int $targetId): bool
  228. {
  229. return FriendRelation::where('user_id', $userId)
  230. ->where('friend_id', $targetId)
  231. ->exists();
  232. }
  233. /**
  234. * 获取好友数量
  235. *
  236. * @param int $userId 用户ID
  237. * @return int 好友数量
  238. */
  239. public static function getFriendCount(int $userId): int
  240. {
  241. return FriendRelation::where('user_id', $userId)->count();
  242. }
  243. }