| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- <?php
- namespace App\Module\Friend\Services;
- use App\Module\Friend\Enums\ERROR_CODE;
- use App\Module\Friend\Enums\FRIEND_STATUS;
- use App\Module\Friend\Enums\READ_STATUS;
- use App\Module\Friend\Enums\REQUEST_STATUS;
- use App\Module\Friend\Logics\Friend as FriendLogic;
- use App\Module\Friend\Logics\Request as RequestLogic;
- use App\Module\Friend\Models\FriendRelation;
- use App\Module\Friend\Models\FriendRequest;
- use Illuminate\Pagination\LengthAwarePaginator;
- use Illuminate\Support\Collection;
- /**
- * 好友服务类
- *
- * 提供好友模块的对外服务接口,包括好友添加、删除、查询等功能。
- * 该类是好友模块与其他模块交互的桥梁,所有方法均为静态方法。
- */
- class FriendService
- {
- /**
- * 获取用户的好友列表
- *
- * @param int $userId 用户ID
- * @param int $page 页码
- * @param int $limit 每页数量
- * @param array $filters 过滤条件
- * @return array 好友列表数据
- */
- public static function getFriendList(int $userId, int $page = 1, int $limit = 20, array $filters = []): array
- {
- $logic = new FriendLogic();
- $result = $logic->getFriendList($userId, $page, $limit, $filters);
- return [
- 'code' => ERROR_CODE::SUCCESS,
- 'message' => '获取成功',
- 'data' => $result
- ];
- }
- /**
- * 搜索可添加为好友的用户
- *
- * @param int $userId 当前用户ID
- * @param string $keyword 搜索关键词
- * @param int $page 页码
- * @param int $limit 每页数量
- * @return array 搜索结果
- */
- public static function searchUsers(int $userId, string $keyword, int $page = 1, int $limit = 20): array
- {
- $logic = new FriendLogic();
- $result = $logic->searchUsers($userId, $keyword, $page, $limit);
- return [
- 'code' => ERROR_CODE::SUCCESS,
- 'message' => '搜索成功',
- 'data' => $result
- ];
- }
- /**
- * 发送好友申请
- *
- * @param int $senderId 发送者ID
- * @param int $receiverId 接收者ID
- * @param string|null $message 申请消息
- * @return array 操作结果
- */
- public static function sendFriendRequest(int $senderId, int $receiverId, ?string $message = null): array
- {
- // 参数验证
- if ($senderId === $receiverId) {
- return [
- 'code' => ERROR_CODE::PARAM_ERROR,
- 'message' => '不能添加自己为好友'
- ];
- }
- $logic = new RequestLogic();
- $result = $logic->sendFriendRequest($senderId, $receiverId, $message);
- if ($result['success']) {
- return [
- 'code' => ERROR_CODE::SUCCESS,
- 'message' => '申请发送成功'
- ];
- } else {
- return [
- 'code' => $result['code'],
- 'message' => $result['message']
- ];
- }
- }
- /**
- * 获取收到的好友申请列表
- *
- * @param int $userId 用户ID
- * @param int $page 页码
- * @param int $limit 每页数量
- * @return array 申请列表数据
- */
- public static function getReceivedRequests(int $userId, int $page = 1, int $limit = 20): array
- {
- $logic = new RequestLogic();
- $result = $logic->getReceivedRequests($userId, $page, $limit);
- return [
- 'code' => ERROR_CODE::SUCCESS,
- 'message' => '获取成功',
- 'data' => $result
- ];
- }
- /**
- * 获取发送的好友申请列表
- *
- * @param int $userId 用户ID
- * @param int $page 页码
- * @param int $limit 每页数量
- * @return array 申请列表数据
- */
- public static function getSentRequests(int $userId, int $page = 1, int $limit = 20): array
- {
- $logic = new RequestLogic();
- $result = $logic->getSentRequests($userId, $page, $limit);
- return [
- 'code' => ERROR_CODE::SUCCESS,
- 'message' => '获取成功',
- 'data' => $result
- ];
- }
- /**
- * 同意好友申请
- *
- * @param int $userId 当前用户ID
- * @param int $requestId 申请ID
- * @return array 操作结果
- */
- public static function acceptFriendRequest(int $userId, int $requestId): array
- {
- $logic = new RequestLogic();
- $result = $logic->acceptFriendRequest($userId, $requestId);
- if ($result['success']) {
- return [
- 'code' => ERROR_CODE::SUCCESS,
- 'message' => '已同意好友申请',
- 'data' => $result['data'] ?? null
- ];
- } else {
- return [
- 'code' => $result['code'],
- 'message' => $result['message']
- ];
- }
- }
- /**
- * 拒绝好友申请
- *
- * @param int $userId 当前用户ID
- * @param int $requestId 申请ID
- * @return array 操作结果
- */
- public static function rejectFriendRequest(int $userId, int $requestId): array
- {
- $logic = new RequestLogic();
- $result = $logic->rejectFriendRequest($userId, $requestId);
- if ($result['success']) {
- return [
- 'code' => ERROR_CODE::SUCCESS,
- 'message' => '已拒绝好友申请'
- ];
- } else {
- return [
- 'code' => $result['code'],
- 'message' => $result['message']
- ];
- }
- }
- /**
- * 删除好友
- *
- * @param int $userId 当前用户ID
- * @param int $friendId 好友ID
- * @return array 操作结果
- */
- public static function deleteFriend(int $userId, int $friendId): array
- {
- $logic = new FriendLogic();
- $result = $logic->deleteFriend($userId, $friendId);
- if ($result['success']) {
- return [
- 'code' => ERROR_CODE::SUCCESS,
- 'message' => '好友删除成功'
- ];
- } else {
- return [
- 'code' => $result['code'],
- 'message' => $result['message']
- ];
- }
- }
- /**
- * 更新好友备注
- *
- * @param int $userId 当前用户ID
- * @param int $friendId 好友ID
- * @param string $remark 备注名
- * @return array 操作结果
- */
- public static function updateFriendRemark(int $userId, int $friendId, string $remark): array
- {
- $logic = new FriendLogic();
- $result = $logic->updateFriendRemark($userId, $friendId, $remark);
- if ($result['success']) {
- return [
- 'code' => ERROR_CODE::SUCCESS,
- 'message' => '备注更新成功'
- ];
- } else {
- return [
- 'code' => $result['code'],
- 'message' => $result['message']
- ];
- }
- }
- /**
- * 检查是否是好友关系
- *
- * @param int $userId 用户ID
- * @param int $targetId 目标用户ID
- * @return bool 是否是好友
- */
- public static function isFriend(int $userId, int $targetId): bool
- {
- return FriendRelation::where('user_id', $userId)
- ->where('friend_id', $targetId)
- ->exists();
- }
- /**
- * 获取好友数量
- *
- * @param int $userId 用户ID
- * @return int 好友数量
- */
- public static function getFriendCount(int $userId): int
- {
- return FriendRelation::where('user_id', $userId)->count();
- }
- }
|