优化代码

main
gzydong 2021-04-08 09:20:51 +08:00
parent 3d22010bec
commit 01f36b5fa8
1 changed files with 35 additions and 35 deletions

View File

@ -152,9 +152,9 @@ class ChatMessageConsumer extends ConsumerMessage
*/ */
public function onConsumeTalk(array $data, AMQPMessage $message): string public function onConsumeTalk(array $data, AMQPMessage $message): string
{ {
$source = $data['data']['source']; $source = $data['data']['source'];
$fds = $this->socketClientService->findUserFds($data['data']['sender']); $fds = $this->socketClientService->findUserFds($data['data']['sender']);
$group_name = ''; $groupInfo = null;
if ($source == 1) {// 私聊 if ($source == 1) {// 私聊
$fds = array_merge($fds, $this->socketClientService->findUserFds($data['data']['receive'])); $fds = array_merge($fds, $this->socketClientService->findUserFds($data['data']['receive']));
@ -164,7 +164,7 @@ class ChatMessageConsumer extends ConsumerMessage
$fds = array_merge($fds, $this->socketClientService->findUserFds((int)$uid)); $fds = array_merge($fds, $this->socketClientService->findUserFds((int)$uid));
} }
$group_name = Group::where('id', $data['data']['receive'])->value('group_name'); $groupInfo = Group::where('id', $data['data']['receive'])->first(['group_name', 'avatar']);
} }
// 客户端ID去重 // 客户端ID去重
@ -235,25 +235,24 @@ class ChatMessageConsumer extends ConsumerMessage
'receive_user' => $data['data']['receive'], 'receive_user' => $data['data']['receive'],
'source_type' => $data['data']['source'], 'source_type' => $data['data']['source'],
'data' => $this->formatTalkMessage([ 'data' => $this->formatTalkMessage([
'id' => $result->id, 'id' => $result->id,
'msg_type' => $result->msg_type, 'msg_type' => $result->msg_type,
'source' => $result->source, 'source' => $result->source,
'avatar' => $result->avatar, 'avatar' => $result->avatar,
'nickname' => $result->nickname, 'nickname' => $result->nickname,
'group_name' => $group_name, 'group_name' => $groupInfo ? $groupInfo->group_name : '',
"user_id" => $result->user_id, 'group_avatar' => $groupInfo ? $groupInfo->avatar : '',
"receive_id" => $result->receive_id, "user_id" => $result->user_id,
"created_at" => $result->created_at, "receive_id" => $result->receive_id,
"content" => $result->content, "created_at" => $result->created_at,
"file" => $file, "content" => $result->content,
"code_block" => $code_block, "file" => $file,
'forward' => $forward, "code_block" => $code_block,
'invite' => $invite 'forward' => $forward,
'invite' => $invite
]) ])
]; ];
unset($result, $file, $code_block, $forward, $invite);
$this->socketPushNotify($fds, json_encode([SocketConstants::EVENT_TALK, $notify])); $this->socketPushNotify($fds, json_encode([SocketConstants::EVENT_TALK, $notify]));
return Result::ACK; return Result::ACK;
@ -371,27 +370,28 @@ class ChatMessageConsumer extends ConsumerMessage
private function formatTalkMessage(array $data): array private function formatTalkMessage(array $data): array
{ {
$message = [ $message = [
"id" => 0,// 消息记录ID "id" => 0,// 消息记录ID
"source" => 1,// 消息来源[1:好友私信;2:群聊] "source" => 1,// 消息来源[1:好友私信;2:群聊]
"msg_type" => 1, "msg_type" => 1,
"user_id" => 0,// 发送者用户ID "user_id" => 0,// 发送者用户ID
"receive_id" => 0,// 接收者ID[好友ID或群ID] "receive_id" => 0,// 接收者ID[好友ID或群ID]
"content" => '',// 文本消息 "content" => '',// 文本消息
"is_revoke" => 0,// 消息是否撤销 "is_revoke" => 0,// 消息是否撤销
// 发送消息人的信息 // 发送消息人的信息
"nickname" => "",// 用户昵称 "nickname" => "",// 用户昵称
"avatar" => "",// 用户头像 "avatar" => "",// 用户头像
"group_name" => "",// 群组名称 "group_name" => "",// 群组名称
"group_avatar" => "",// 群组头像
// 不同的消息类型 // 不同的消息类型
"file" => [], "file" => [],
"code_block" => [], "code_block" => [],
"forward" => [], "forward" => [],
"invite" => [], "invite" => [],
// 消息创建时间 // 消息创建时间
"created_at" => "", "created_at" => "",
]; ];
return array_merge($message, array_intersect_key($data, $message)); return array_merge($message, array_intersect_key($data, $message));