优化代码
parent
296de89431
commit
8b96f05381
|
@ -223,7 +223,7 @@ class TalkController extends CController
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$user_id = $this->uid();
|
$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('暂不属于好友关系或群聊成员,无法查看聊天记录!');
|
return $this->response->fail('暂不属于好友关系或群聊成员,无法查看聊天记录!');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -278,7 +278,7 @@ class TalkController extends CController
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$user_id = $this->uid();
|
$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('暂不属于好友关系或群聊成员,无法查看聊天记录!');
|
return $this->response->fail('暂不属于好友关系或群聊成员,无法查看聊天记录!');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -13,6 +13,7 @@ use App\Model\UsersFriend;
|
||||||
use App\Service\SocketClientService;
|
use App\Service\SocketClientService;
|
||||||
use App\Service\UserFriendService;
|
use App\Service\UserFriendService;
|
||||||
use App\Support\MessageProducer;
|
use App\Support\MessageProducer;
|
||||||
|
use App\Support\UserRelation;
|
||||||
use Swoole\Http\Response;
|
use Swoole\Http\Response;
|
||||||
use Swoole\WebSocket\Frame;
|
use Swoole\WebSocket\Frame;
|
||||||
use Swoole\WebSocket\Server;
|
use Swoole\WebSocket\Server;
|
||||||
|
@ -57,14 +58,13 @@ class ReceiveHandleService
|
||||||
if (!in_array($data['talk_type'], TalkMode::getTypes())) return;
|
if (!in_array($data['talk_type'], TalkMode::getTypes())) return;
|
||||||
|
|
||||||
// 验证发送消息用户与接受消息用户之间是否存在好友或群聊关系
|
// 验证发送消息用户与接受消息用户之间是否存在好友或群聊关系
|
||||||
if ($data['talk_type'] == TalkMode::PRIVATE_CHAT) {
|
$isTrue = UserRelation::isFriendOrGroupMember($user_id, (int)$data['receiver_id'], (int)$data['talk_type']);
|
||||||
$isTrue = container()->get(UserFriendService::class)->isFriend((int)$data['sender_id'], (int)$data['receiver_id'], true);
|
if (!$isTrue) {
|
||||||
if (!$isTrue) return;
|
$server->push($frame->fd, json_encode(['event_error', [
|
||||||
} else if ($data['talk_type'] == TalkMode::GROUP_CHAT) {
|
'message' => '暂不属于好友关系或群聊成员,无法发送聊天消息!'
|
||||||
if (!Group::isMember((int)$data['receiver_id'], (int)$data['sender_id'])) {
|
]]));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
$result = TalkRecords::create([
|
$result = TalkRecords::create([
|
||||||
'talk_type' => $data['talk_type'],
|
'talk_type' => $data['talk_type'],
|
||||||
|
|
|
@ -425,7 +425,7 @@ class TalkService extends BaseService
|
||||||
$sqlObj = TalkRecords::whereIn('id', $records_ids);
|
$sqlObj = TalkRecords::whereIn('id', $records_ids);
|
||||||
|
|
||||||
if ($talk_type == TalkMode::PRIVATE_CHAT) {
|
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) {
|
$sqlObj = $sqlObj->where(function ($query) use ($user_id, $receiver_id) {
|
||||||
$query->where([
|
$query->where([
|
||||||
|
|
|
@ -30,22 +30,18 @@ class UserFriendService
|
||||||
*
|
*
|
||||||
* @param int $user_id 用户ID
|
* @param int $user_id 用户ID
|
||||||
* @param int $friend_id 好友ID
|
* @param int $friend_id 好友ID
|
||||||
* @param bool $is_cache 是否允许读取缓存
|
|
||||||
* @param bool $is_mutual 相互互为好友
|
* @param bool $is_mutual 相互互为好友
|
||||||
* @return bool
|
* @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}";
|
$isTrue1 = UsersFriend::where('user_id', $user_id)->where('friend_id', $friend_id)->where('status', 1)->exists();
|
||||||
if ($is_cache && redis()->get($cacheKey)) {
|
if ($is_mutual === false) {
|
||||||
return true;
|
return $isTrue1;
|
||||||
}
|
}
|
||||||
|
|
||||||
$isTrue = UsersFriend::query()->where('user_id', $user_id)->where('friend_id', $friend_id)->where('status', 1)->exists();
|
$isTrue2 = UsersFriend::where('user_id', $friend_id)->where('friend_id', $user_id)->where('status', 1)->exists();
|
||||||
if ($isTrue) {
|
|
||||||
redis()->setex($cacheKey, 60 * 5, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $isTrue;
|
return $isTrue1 && $isTrue2;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -125,7 +125,7 @@ class UserService extends BaseService
|
||||||
|
|
||||||
// 判断查询信息是否是自己
|
// 判断查询信息是否是自己
|
||||||
if ($friend_id != $me_user_id) {
|
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;
|
$info['friend_status'] = $is_friend ? 2 : 1;
|
||||||
if ($is_friend) {
|
if ($is_friend) {
|
||||||
|
|
Loading…
Reference in New Issue