优化代码

main
gzydong 2021-07-13 23:34:43 +08:00
parent 296de89431
commit 8b96f05381
5 changed files with 17 additions and 21 deletions

View File

@ -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('暂不属于好友关系或群聊成员,无法查看聊天记录!');
}

View File

@ -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,14 +58,13 @@ 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'])) {
$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([
'talk_type' => $data['talk_type'],

View File

@ -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([

View File

@ -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;
}
}

View File

@ -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) {