2020-11-22 23:10:00 +08:00
|
|
|
<?php
|
|
|
|
|
2021-07-08 19:09:06 +08:00
|
|
|
namespace App\Service\Message;
|
2020-11-22 23:10:00 +08:00
|
|
|
|
2021-07-08 19:09:06 +08:00
|
|
|
use App\Cache\LastMessage;
|
|
|
|
use App\Cache\UnreadTalk;
|
2021-07-08 19:30:03 +08:00
|
|
|
use App\Constants\TalkMessageEvent;
|
|
|
|
use App\Constants\TalkMessageType;
|
|
|
|
use App\Constants\TalkMode;
|
2021-07-09 22:57:19 +08:00
|
|
|
use App\Model\Talk\TalkRecords;
|
2021-03-25 17:32:36 +08:00
|
|
|
use App\Model\Group\Group;
|
2020-11-22 23:10:00 +08:00
|
|
|
use App\Model\UsersFriend;
|
2021-07-08 19:09:06 +08:00
|
|
|
use App\Service\SocketClientService;
|
2021-07-12 22:00:48 +08:00
|
|
|
use App\Service\UserFriendService;
|
2021-07-08 19:09:06 +08:00
|
|
|
use App\Support\MessageProducer;
|
|
|
|
use Swoole\Http\Response;
|
|
|
|
use Swoole\WebSocket\Frame;
|
|
|
|
use Swoole\WebSocket\Server;
|
2020-11-22 23:10:00 +08:00
|
|
|
|
2021-07-08 19:09:06 +08:00
|
|
|
class ReceiveHandleService
|
2020-11-22 23:10:00 +08:00
|
|
|
{
|
|
|
|
/**
|
2020-11-29 14:44:11 +08:00
|
|
|
* @var SocketClientService
|
2020-11-22 23:10:00 +08:00
|
|
|
*/
|
2021-07-09 19:40:43 +08:00
|
|
|
private $client;
|
2020-11-29 14:44:11 +08:00
|
|
|
|
2021-07-09 19:40:43 +08:00
|
|
|
// 消息事件绑定
|
|
|
|
const EVENTS = [
|
|
|
|
TalkMessageEvent::EVENT_TALK => 'onTalk',
|
|
|
|
TalkMessageEvent::EVENT_KEYBOARD => 'onKeyboard',
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ReceiveHandleService constructor.
|
|
|
|
*
|
|
|
|
* @param SocketClientService $client
|
|
|
|
*/
|
|
|
|
public function __construct(SocketClientService $client)
|
2021-07-08 19:09:06 +08:00
|
|
|
{
|
2021-07-09 19:40:43 +08:00
|
|
|
$this->client = $client;
|
2021-07-08 19:09:06 +08:00
|
|
|
}
|
|
|
|
|
2020-11-22 23:10:00 +08:00
|
|
|
/**
|
2021-07-09 19:40:43 +08:00
|
|
|
* 对话文本消息
|
2020-11-22 23:10:00 +08:00
|
|
|
*
|
|
|
|
* @param Response|Server $server
|
2021-04-20 16:30:57 +08:00
|
|
|
* @param Frame $frame
|
|
|
|
* @param array|string $data 解析后数据
|
2021-04-22 16:14:34 +08:00
|
|
|
* @return void
|
2020-11-22 23:10:00 +08:00
|
|
|
*/
|
|
|
|
public function onTalk($server, Frame $frame, $data)
|
|
|
|
{
|
2021-07-09 19:40:43 +08:00
|
|
|
$user_id = $this->client->findFdUserId($frame->fd);
|
|
|
|
if ($user_id != $data['sender_id']) return;
|
2020-11-22 23:10:00 +08:00
|
|
|
|
2021-07-09 19:40:43 +08:00
|
|
|
// 验证消息类型
|
|
|
|
if (!in_array($data['talk_type'], TalkMode::getTypes())) return;
|
2020-11-22 23:10:00 +08:00
|
|
|
|
2021-07-09 19:40:43 +08:00
|
|
|
// 验证发送消息用户与接受消息用户之间是否存在好友或群聊关系
|
2021-07-08 19:30:03 +08:00
|
|
|
if ($data['talk_type'] == TalkMode::PRIVATE_CHAT) {
|
2021-07-12 22:00:48 +08:00
|
|
|
$isTrue = container()->get(UserFriendService::class)->isFriend((int)$data['sender_id'], (int)$data['receiver_id'], true);
|
|
|
|
if (!$isTrue) return;
|
2021-07-08 19:30:03 +08:00
|
|
|
} else if ($data['talk_type'] == TalkMode::GROUP_CHAT) {
|
2021-07-05 21:52:44 +08:00
|
|
|
if (!Group::isMember((int)$data['receiver_id'], (int)$data['sender_id'])) {
|
2020-11-22 23:10:00 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-05 21:52:44 +08:00
|
|
|
$result = TalkRecords::create([
|
|
|
|
'talk_type' => $data['talk_type'],
|
|
|
|
'user_id' => $data['sender_id'],
|
|
|
|
'receiver_id' => $data['receiver_id'],
|
2021-07-08 19:30:03 +08:00
|
|
|
'msg_type' => TalkMessageType::TEXT_MESSAGE,
|
2021-07-05 21:52:44 +08:00
|
|
|
'content' => htmlspecialchars($data['text_message']),
|
|
|
|
'created_at' => date('Y-m-d H:i:s'),
|
|
|
|
'updated_at' => date('Y-m-d H:i:s'),
|
2020-11-22 23:10:00 +08:00
|
|
|
]);
|
|
|
|
|
2021-07-09 19:40:43 +08:00
|
|
|
// 判断是否私信
|
2021-07-08 19:30:03 +08:00
|
|
|
if ($result->talk_type == TalkMode::PRIVATE_CHAT) {
|
2021-07-05 21:52:44 +08:00
|
|
|
UnreadTalk::getInstance()->increment($result->user_id, $result->receiver_id);
|
2020-11-22 23:10:00 +08:00
|
|
|
}
|
|
|
|
|
2021-05-20 22:23:48 +08:00
|
|
|
// 缓存最后一条聊天消息
|
2021-07-05 21:52:44 +08:00
|
|
|
LastMessage::getInstance()->save($result->talk_type, $result->user_id, $result->receiver_id, [
|
2021-04-20 16:30:57 +08:00
|
|
|
'text' => mb_substr($result->content, 0, 30),
|
2020-12-12 18:59:28 +08:00
|
|
|
'created_at' => $result->created_at
|
2021-05-20 22:23:48 +08:00
|
|
|
]);
|
2020-12-12 18:59:28 +08:00
|
|
|
|
2021-07-09 19:40:43 +08:00
|
|
|
MessageProducer::publish(MessageProducer::create(TalkMessageEvent::EVENT_TALK, [
|
|
|
|
'sender_id' => $result->user_id,
|
|
|
|
'receiver_id' => $result->receiver_id,
|
|
|
|
'talk_type' => $result->talk_type,
|
|
|
|
'record_id' => $result->id
|
|
|
|
]));
|
2020-11-22 23:10:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 键盘输入消息
|
|
|
|
*
|
|
|
|
* @param Response|Server $server
|
2021-04-20 16:30:57 +08:00
|
|
|
* @param Frame $frame
|
|
|
|
* @param array|string $data 解析后数据
|
2021-07-05 21:52:44 +08:00
|
|
|
* @return false
|
2020-11-22 23:10:00 +08:00
|
|
|
*/
|
|
|
|
public function onKeyboard($server, Frame $frame, $data)
|
|
|
|
{
|
2021-07-09 19:40:43 +08:00
|
|
|
MessageProducer::publish(MessageProducer::create(TalkMessageEvent::EVENT_KEYBOARD, [
|
|
|
|
'sender_id' => (int)$data['sender_id'],
|
|
|
|
'receiver_id' => (int)$data['receiver_id'],
|
|
|
|
]));
|
2020-11-22 23:10:00 +08:00
|
|
|
}
|
|
|
|
}
|