diff --git a/app/Controller/Api/V1/TalkController.php b/app/Controller/Api/V1/TalkController.php index 8c80754..674e4da 100644 --- a/app/Controller/Api/V1/TalkController.php +++ b/app/Controller/Api/V1/TalkController.php @@ -223,7 +223,7 @@ class TalkController extends CController ]); $user_id = $this->uid(); - if (!UserRelation::isFriendOrGroupMember($user_id, $params['receiver_id'], $params['talk_type'])) { + if ($params['talk_type'] == TalkMode::GROUP_CHAT && !Group::isMember((int)$params['receiver_id'], $user_id)) { return $this->response->fail('暂不属于好友关系或群聊成员,无法查看聊天记录!'); } @@ -278,7 +278,7 @@ class TalkController extends CController ]); $user_id = $this->uid(); - if (!UserRelation::isFriendOrGroupMember($user_id, $params['receiver_id'], $params['talk_type'])) { + if ($params['talk_type'] == TalkMode::GROUP_CHAT && !Group::isMember((int)$params['receiver_id'], $user_id)) { return $this->response->fail('暂不属于好友关系或群聊成员,无法查看聊天记录!'); } diff --git a/app/Service/Message/ReceiveHandleService.php b/app/Service/Message/ReceiveHandleService.php index 141ca5b..40940ea 100644 --- a/app/Service/Message/ReceiveHandleService.php +++ b/app/Service/Message/ReceiveHandleService.php @@ -13,6 +13,7 @@ use App\Model\UsersFriend; use App\Service\SocketClientService; use App\Service\UserFriendService; use App\Support\MessageProducer; +use App\Support\UserRelation; use Swoole\Http\Response; use Swoole\WebSocket\Frame; use Swoole\WebSocket\Server; @@ -57,13 +58,12 @@ class ReceiveHandleService if (!in_array($data['talk_type'], TalkMode::getTypes())) return; // 验证发送消息用户与接受消息用户之间是否存在好友或群聊关系 - if ($data['talk_type'] == TalkMode::PRIVATE_CHAT) { - $isTrue = container()->get(UserFriendService::class)->isFriend((int)$data['sender_id'], (int)$data['receiver_id'], true); - if (!$isTrue) return; - } else if ($data['talk_type'] == TalkMode::GROUP_CHAT) { - if (!Group::isMember((int)$data['receiver_id'], (int)$data['sender_id'])) { - return; - } + $isTrue = UserRelation::isFriendOrGroupMember($user_id, (int)$data['receiver_id'], (int)$data['talk_type']); + if (!$isTrue) { + $server->push($frame->fd, json_encode(['event_error', [ + 'message' => '暂不属于好友关系或群聊成员,无法发送聊天消息!' + ]])); + return; } $result = TalkRecords::create([ diff --git a/app/Service/TalkService.php b/app/Service/TalkService.php index 54c1b3a..2e6d6a7 100644 --- a/app/Service/TalkService.php +++ b/app/Service/TalkService.php @@ -425,7 +425,7 @@ class TalkService extends BaseService $sqlObj = TalkRecords::whereIn('id', $records_ids); if ($talk_type == TalkMode::PRIVATE_CHAT) { - if (!container()->get(UserFriendService::class)->isFriend($user_id, $receiver_id)) return []; + if (!container()->get(UserFriendService::class)->isFriend($user_id, $receiver_id, true)) return []; $sqlObj = $sqlObj->where(function ($query) use ($user_id, $receiver_id) { $query->where([ diff --git a/app/Service/UserFriendService.php b/app/Service/UserFriendService.php index 7b42278..07d1902 100644 --- a/app/Service/UserFriendService.php +++ b/app/Service/UserFriendService.php @@ -30,22 +30,18 @@ class UserFriendService * * @param int $user_id 用户ID * @param int $friend_id 好友ID - * @param bool $is_cache 是否允许读取缓存 * @param bool $is_mutual 相互互为好友 * @return bool */ - public function isFriend(int $user_id, int $friend_id, bool $is_cache = false, $is_mutual = false) + public function isFriend(int $user_id, int $friend_id, $is_mutual = false) { - $cacheKey = "good_friends:{$user_id}_{$friend_id}"; - if ($is_cache && redis()->get($cacheKey)) { - return true; + $isTrue1 = UsersFriend::where('user_id', $user_id)->where('friend_id', $friend_id)->where('status', 1)->exists(); + if ($is_mutual === false) { + return $isTrue1; } - $isTrue = UsersFriend::query()->where('user_id', $user_id)->where('friend_id', $friend_id)->where('status', 1)->exists(); - if ($isTrue) { - redis()->setex($cacheKey, 60 * 5, 1); - } + $isTrue2 = UsersFriend::where('user_id', $friend_id)->where('friend_id', $user_id)->where('status', 1)->exists(); - return $isTrue; + return $isTrue1 && $isTrue2; } } diff --git a/app/Service/UserService.php b/app/Service/UserService.php index 7b070ea..84f9c13 100644 --- a/app/Service/UserService.php +++ b/app/Service/UserService.php @@ -125,7 +125,7 @@ class UserService extends BaseService // 判断查询信息是否是自己 if ($friend_id != $me_user_id) { - $is_friend = container()->get(UserFriendService::class)->isFriend($me_user_id, $friend_id, true, true); + $is_friend = container()->get(UserFriendService::class)->isFriend($me_user_id, $friend_id, true); $info['friend_status'] = $is_friend ? 2 : 1; if ($is_friend) {