2020-11-22 23:10:00 +08:00
|
|
|
<?php
|
2021-07-20 23:12:18 +08:00
|
|
|
declare(strict_types=1);
|
2020-11-22 23:10:00 +08:00
|
|
|
|
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;
|
2021-07-21 23:31:41 +08:00
|
|
|
use App\Cache\UnreadTalkCache;
|
2021-07-20 23:12:18 +08:00
|
|
|
use App\Constants\TalkEventConstant;
|
2021-07-08 19:30:03 +08:00
|
|
|
use App\Constants\TalkMessageType;
|
2021-07-20 23:12:18 +08:00
|
|
|
use App\Constants\TalkModeConstant;
|
|
|
|
use App\Event\TalkEvent;
|
2021-07-09 22:57:19 +08:00
|
|
|
use App\Model\Talk\TalkRecords;
|
2021-07-08 19:09:06 +08:00
|
|
|
use App\Service\SocketClientService;
|
2021-08-09 21:29:52 +08:00
|
|
|
use App\Service\TalkMessageService;
|
2021-07-13 23:34:43 +08:00
|
|
|
use App\Support\UserRelation;
|
2021-07-08 19:09:06 +08:00
|
|
|
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 = [
|
2021-07-20 23:12:18 +08:00
|
|
|
TalkEventConstant::EVENT_TALK => 'onTalk',
|
|
|
|
TalkEventConstant::EVENT_KEYBOARD => 'onKeyboard',
|
2021-07-09 19:40:43 +08:00
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
// 验证消息类型
|
2021-07-20 23:12:18 +08:00
|
|
|
if (!in_array($data['talk_type'], TalkModeConstant::getTypes())) return;
|
2020-11-22 23:10:00 +08:00
|
|
|
|
2021-07-09 19:40:43 +08:00
|
|
|
// 验证发送消息用户与接受消息用户之间是否存在好友或群聊关系
|
2021-07-13 23:34:43 +08:00
|
|
|
$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;
|
2020-11-22 23:10:00 +08:00
|
|
|
}
|
|
|
|
|
2021-08-09 21:29:52 +08:00
|
|
|
di()->get(TalkMessageService::class)->insertTextMessage([
|
2021-07-05 21:52:44 +08:00
|
|
|
'talk_type' => $data['talk_type'],
|
|
|
|
'user_id' => $data['sender_id'],
|
|
|
|
'receiver_id' => $data['receiver_id'],
|
2021-08-09 21:29:52 +08:00
|
|
|
'content' => $data['text_message'],
|
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-20 23:12:18 +08:00
|
|
|
event()->dispatch(new TalkEvent(TalkEventConstant::EVENT_KEYBOARD, [
|
2021-07-09 19:40:43 +08:00
|
|
|
'sender_id' => (int)$data['sender_id'],
|
|
|
|
'receiver_id' => (int)$data['receiver_id'],
|
|
|
|
]));
|
2020-11-22 23:10:00 +08:00
|
|
|
}
|
|
|
|
}
|