hyperf-chat/app/Service/MessageHandleService.php

122 lines
3.6 KiB
PHP
Raw Normal View History

2020-11-22 23:10:00 +08:00
<?php
namespace App\Service;
use Hyperf\Di\Annotation\Inject;
2020-11-29 14:44:11 +08:00
use Hyperf\Amqp\Producer;
use Swoole\Http\Response;
use Swoole\WebSocket\Frame;
use Swoole\WebSocket\Server;
2020-11-22 23:10:00 +08:00
use App\Amqp\Producer\ChatMessageProducer;
use App\Cache\LastMsgCache;
use App\Cache\UnreadTalkCache;
use App\Model\Chat\ChatRecord;
use App\Model\Group\Group;
2020-11-22 23:10:00 +08:00
use App\Model\UsersFriend;
class MessageHandleService
{
/**
* @Inject
* @var Producer
*/
private $producer;
/**
* @inject
2020-11-29 14:44:11 +08:00
* @var SocketClientService
2020-11-22 23:10:00 +08:00
*/
2020-11-29 14:44:11 +08:00
private $socketClientService;
/**
* @Inject
* @var UnreadTalkCache
*/
private $unreadTalkCache;
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)
{
2020-11-29 14:44:11 +08:00
$user_id = $this->socketClientService->findFdUserId($frame->fd);
2020-11-22 23:10:00 +08:00
if ($user_id != $data['send_user']) {
return;
}
2021-04-22 16:54:01 +08:00
// 验证消息类型 私聊|群聊
2020-11-22 23:10:00 +08:00
if (!in_array($data['source_type'], [1, 2])) {
return;
}
2021-04-22 16:54:01 +08:00
// 验证发送消息用户与接受消息用户之间是否存在好友或群聊关系(后期走缓存)
if ($data['source_type'] == 1) {// 私信
// 判断发送者和接受者是否是好友关系
2020-12-01 22:50:43 +08:00
if (!UsersFriend::isFriend((int)$data['send_user'], (int)$data['receive_user'], true)) {
2020-11-22 23:10:00 +08:00
return;
}
2021-04-22 16:54:01 +08:00
} else if ($data['source_type'] == 2) {// 群聊
// 判断是否属于群成员
if (!Group::isMember((int)$data['receive_user'], (int)$data['send_user'])) {
2020-11-22 23:10:00 +08:00
return;
}
}
$result = ChatRecord::create([
2021-04-20 16:30:57 +08:00
'source' => $data['source_type'],
'msg_type' => 1,
'user_id' => $data['send_user'],
2020-11-22 23:10:00 +08:00
'receive_id' => $data['receive_user'],
2021-04-20 16:30:57 +08:00
'content' => htmlspecialchars($data['text_message']),
2020-11-22 23:10:00 +08:00
'created_at' => date('Y-m-d H:i:s'),
]);
2020-12-12 18:59:28 +08:00
if (!$result) return;
2020-11-22 23:10:00 +08:00
// 判断是否私聊
if ($data['source_type'] == 1) {
// 设置好友消息未读数
2020-11-29 14:44:11 +08:00
$this->unreadTalkCache->setInc(intval($result->receive_id), strval($result->user_id));
2020-11-22 23:10:00 +08:00
}
2020-12-12 18:59:28 +08:00
// 缓存最后一条消息
LastMsgCache::set([
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
], (int)$data['receive_user'],
$data['source_type'] == 1 ? (int)$data['send_user'] : 0
);
2020-11-22 23:10:00 +08:00
$this->producer->produce(
new ChatMessageProducer('event_talk', [
2021-04-22 16:54:01 +08:00
'sender' => intval($data['send_user']), // 发送者ID
'receive' => intval($data['receive_user']), // 接收者ID
'source' => intval($data['source_type']), // 接收者类型[1:好友;2:群组;]
2020-11-22 23:10:00 +08:00
'record_id' => $result->id
])
);
}
/**
* 键盘输入消息
*
* @param Response|Server $server
2021-04-20 16:30:57 +08:00
* @param Frame $frame
* @param array|string $data 解析后数据
2020-11-22 23:10:00 +08:00
*/
public function onKeyboard($server, Frame $frame, $data)
{
$this->producer->produce(
new ChatMessageProducer('event_keyboard', [
2021-04-22 16:54:01 +08:00
'send_user' => intval($data['send_user']), // 发送者ID
'receive_user' => intval($data['receive_user']), // 接收者ID
2020-11-22 23:10:00 +08:00
])
);
}
}