hyperf-chat/app/Amqp/Consumer/ChatMessageConsumer.php

206 lines
6.2 KiB
PHP
Raw Normal View History

2020-11-02 22:45:37 +08:00
<?php
declare(strict_types=1);
namespace App\Amqp\Consumer;
2020-11-21 19:53:01 +08:00
use App\Helper\PushMessageHelper;
use App\Model\Chat\ChatRecord;
use App\Model\Chat\ChatRecordsCode;
use App\Model\Chat\ChatRecordsFile;
2020-11-21 22:47:21 +08:00
use App\Model\User;
use App\Model\Chat\ChatRecordsInvite;
2020-11-21 19:53:01 +08:00
use App\Service\SocketFDService;
use App\Service\SocketRoomService;
2020-11-02 22:45:37 +08:00
use Hyperf\Amqp\Result;
use Hyperf\Amqp\Annotation\Consumer;
use Hyperf\Amqp\Message\ConsumerMessage;
2020-11-08 17:10:05 +08:00
use Hyperf\Redis\Redis;
2020-11-02 22:45:37 +08:00
use PhpAmqpLib\Message\AMQPMessage;
use Hyperf\Amqp\Message\Type;
use Hyperf\Amqp\Builder\QueueBuilder;
/**
2020-11-07 22:57:10 +08:00
* @Consumer(name="聊天消息消费者",enable=true)
2020-11-02 22:45:37 +08:00
*/
2020-11-04 11:57:16 +08:00
class ChatMessageConsumer extends ConsumerMessage
2020-11-02 22:45:37 +08:00
{
/**
* 交换机名称
*
* @var string
*/
public $exchange = 'im.message.fanout';
/**
* 交换机类型
*
* @var string
*/
public $type = Type::FANOUT;
/**
* 路由key
*
* @var string
*/
public $routingKey = 'consumer:im:message';
2020-11-04 07:54:11 +08:00
/**
2020-11-21 19:53:01 +08:00
* @var SocketFDService
2020-11-04 07:54:11 +08:00
*/
2020-11-21 19:53:01 +08:00
private $socketFDService;
/**
* @var SocketRoomService
*/
private $socketRoomService;
/**
* ChatMessageConsumer constructor.
* @param SocketFDService $socketFDService
*/
public function __construct(SocketFDService $socketFDService, SocketRoomService $socketRoomService)
2020-11-03 17:12:57 +08:00
{
2020-11-21 19:53:01 +08:00
$this->socketFDService = $socketFDService;
$this->socketRoomService = $socketRoomService;
2020-11-20 19:17:11 +08:00
$this->setQueue('queue:im-message:' . SERVER_RUN_ID);
2020-11-03 17:12:57 +08:00
}
2020-11-02 22:45:37 +08:00
/**
* 重写创建队列生成类
*
* 注释:设置自动删除队列
*
* @return QueueBuilder
*/
public function getQueueBuilder(): QueueBuilder
{
return parent::getQueueBuilder()->setAutoDelete(true);
}
/**
* 消费队列消息
*
* @param $data
* @param AMQPMessage $message
* @return string
*/
public function consumeMessage($data, AMQPMessage $message): string
{
2020-11-21 19:53:01 +08:00
$redis = container()->get(Redis::class);
2020-11-08 17:10:05 +08:00
2020-11-21 19:53:01 +08:00
//[加锁]防止消息重复消费
$lockName = sprintf('ws:message-lock:%s:%s', SERVER_RUN_ID, $data['uuid']);
if (!$redis->rawCommand('SET', $lockName, 1, 'NX', 'EX', 60)) {
return Result::ACK;
}
2020-11-02 22:45:37 +08:00
2020-11-21 19:53:01 +08:00
$source = $data['source'];
$fids = $this->socketFDService->findUserFds($data['sender']);
if ($source == 1) {// 私聊
$fids = array_merge($fids, $this->socketFDService->findUserFds($data['receive']));
} else if ($source == 2) {//群聊
$userIds = $this->socketRoomService->getRoomMembers(strval($data['receive']));
foreach ($userIds as $uid) {
$fids = array_merge($fids, $this->socketFDService->findUserFds(intval($uid)));
2020-11-03 14:07:54 +08:00
}
2020-11-21 19:53:01 +08:00
}
// 去重
$fids = array_unique($fids);
if (empty($fids)) {
return Result::ACK;
}
2020-11-21 22:47:21 +08:00
/**
* @var ChatRecord
*/
2020-11-21 19:53:01 +08:00
$result = ChatRecord::leftJoin('users', 'users.id', '=', 'chat_records.user_id')
->where('chat_records.id', $data['record_id'])
->first([
'chat_records.id',
'chat_records.source',
'chat_records.msg_type',
'chat_records.user_id',
'chat_records.receive_id',
'chat_records.content',
'chat_records.is_revoke',
'chat_records.created_at',
2020-11-20 19:17:11 +08:00
2020-11-21 19:53:01 +08:00
'users.nickname',
'users.avatar as avatar',
]);
$file = [];
$code_block = [];
2020-11-21 22:47:21 +08:00
$forward = [];
$invite = [];
switch ($result->msg_type) {
case 2://文件消息
$file = ChatRecordsFile::where('record_id', $result->id)->first(['id', 'record_id', 'user_id', 'file_source', 'file_type', 'save_type', 'original_name', 'file_suffix', 'file_size', 'save_dir']);
$file = $file ? $file->toArray() : [];
if ($file) {
$file['file_url'] = get_media_url($file['save_dir']);
}
break;
case 3://入群消息/退群消息
$notifyInfo = ChatRecordsInvite::where('record_id', $result->id)->first([
'record_id', 'type', 'operate_user_id', 'user_ids'
]);
$userInfo = User::where('id', $notifyInfo->operate_user_id)->first(['nickname', 'id']);
$membersIds = explode(',', $notifyInfo->user_ids);
$invite = [
'type' => $notifyInfo->type,
'operate_user' => ['id' => $userInfo->id, 'nickname' => $userInfo->nickname],
'users' => User::select('id', 'nickname')->whereIn('id', $membersIds)->get()->toArray()
];
unset($notifyInfo, $userInfo, $membersIds);
break;
case 4://会话记录消息
break;
case 5://代码块消息
$code_block = ChatRecordsCode::where('record_id', $result->id)->first(['record_id', 'code_lang', 'code']);
$code_block = $code_block ? $code_block->toArray() : [];
break;
2020-11-21 19:53:01 +08:00
}
$msg = [
'send_user' => $data['sender'],
'receive_user' => $data['receive'],
'source_type' => $data['source'],
'data' => PushMessageHelper::formatTalkMsg([
'id' => $result->id,
'msg_type' => $result->msg_type,
'source' => $result->source,
'avatar' => $result->avatar,
'nickname' => $result->nickname,
"user_id" => $result->user_id,
"receive_id" => $result->receive_id,
"created_at" => $result->created_at,
"content" => $result->content,
"file" => $file,
2020-11-21 22:47:21 +08:00
"code_block" => $code_block,
'forward' => $forward,
'invite' => $invite
2020-11-21 19:53:01 +08:00
])
];
2020-11-20 19:17:11 +08:00
2020-11-21 19:53:01 +08:00
$server = server();
foreach ($fids as $fd) {
$fd = intval($fd);
if ($server->exist($fd)) {
$server->push($fd, json_encode(['chat_message', $msg]));
}
}
2020-11-03 14:07:54 +08:00
2020-11-21 19:53:01 +08:00
unset($fids, $result, $msg);
2020-11-08 17:10:05 +08:00
return Result::ACK;
2020-11-02 22:45:37 +08:00
}
}