main
parent
031636f2f6
commit
44b2ddfd91
|
@ -11,6 +11,7 @@ use App\Constants\TalkModeConstant;
|
||||||
use App\Event\TalkEvent;
|
use App\Event\TalkEvent;
|
||||||
use App\Model\Talk\TalkRecords;
|
use App\Model\Talk\TalkRecords;
|
||||||
use App\Service\SocketClientService;
|
use App\Service\SocketClientService;
|
||||||
|
use App\Service\TalkMessageService;
|
||||||
use App\Support\UserRelation;
|
use App\Support\UserRelation;
|
||||||
use Swoole\Http\Response;
|
use Swoole\Http\Response;
|
||||||
use Swoole\WebSocket\Frame;
|
use Swoole\WebSocket\Frame;
|
||||||
|
@ -64,33 +65,12 @@ class ReceiveHandleService
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$result = TalkRecords::create([
|
di()->get(TalkMessageService::class)->insertTextMessage([
|
||||||
'talk_type' => $data['talk_type'],
|
'talk_type' => $data['talk_type'],
|
||||||
'user_id' => $data['sender_id'],
|
'user_id' => $data['sender_id'],
|
||||||
'receiver_id' => $data['receiver_id'],
|
'receiver_id' => $data['receiver_id'],
|
||||||
'msg_type' => TalkMessageType::TEXT_MESSAGE,
|
'content' => $data['text_message'],
|
||||||
'content' => htmlspecialchars($data['text_message']),
|
|
||||||
'created_at' => date('Y-m-d H:i:s'),
|
|
||||||
'updated_at' => date('Y-m-d H:i:s'),
|
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// 判断是否私信
|
|
||||||
if ($result->talk_type == TalkModeConstant::PRIVATE_CHAT) {
|
|
||||||
UnreadTalkCache::getInstance()->increment($result->user_id, $result->receiver_id);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 缓存最后一条聊天消息
|
|
||||||
LastMessage::getInstance()->save($result->talk_type, $result->user_id, $result->receiver_id, [
|
|
||||||
'text' => mb_substr($result->content, 0, 30),
|
|
||||||
'created_at' => date('Y-m-d H:i:s')
|
|
||||||
]);
|
|
||||||
|
|
||||||
event()->dispatch(new TalkEvent(TalkEventConstant::EVENT_TALK, [
|
|
||||||
'sender_id' => $result->user_id,
|
|
||||||
'receiver_id' => $result->receiver_id,
|
|
||||||
'talk_type' => $result->talk_type,
|
|
||||||
'record_id' => $result->id
|
|
||||||
]));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
namespace App\Service;
|
namespace App\Service;
|
||||||
|
|
||||||
use App\Cache\LastMessage;
|
use App\Cache\LastMessage;
|
||||||
|
use App\Cache\UnreadTalkCache;
|
||||||
use App\Cache\VoteCache;
|
use App\Cache\VoteCache;
|
||||||
use App\Cache\VoteStatisticsCache;
|
use App\Cache\VoteStatisticsCache;
|
||||||
use App\Constants\RobotConstant;
|
use App\Constants\RobotConstant;
|
||||||
|
@ -25,6 +26,43 @@ use Hyperf\DbConnection\Db;
|
||||||
|
|
||||||
class TalkMessageService
|
class TalkMessageService
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* 创建文本消息
|
||||||
|
*
|
||||||
|
* @param array $message
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function insertTextMessage(array $message)
|
||||||
|
{
|
||||||
|
$message['msg_type'] = TalkMessageType::TEXT_MESSAGE;
|
||||||
|
$message['content'] = htmlspecialchars($message['content']);
|
||||||
|
$message['created_at'] = date('Y-m-d H:i:s');
|
||||||
|
$message['updated_at'] = date('Y-m-d H:i:s');
|
||||||
|
|
||||||
|
$result = TalkRecords::create($message);
|
||||||
|
|
||||||
|
// 判断是否私信
|
||||||
|
if ($result->talk_type == TalkModeConstant::PRIVATE_CHAT) {
|
||||||
|
UnreadTalkCache::getInstance()->increment($result->user_id, $result->receiver_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 缓存最后一条聊天消息
|
||||||
|
LastMessage::getInstance()->save($result->talk_type, $result->user_id, $result->receiver_id, [
|
||||||
|
'text' => mb_substr($result->content, 0, 30),
|
||||||
|
'created_at' => date('Y-m-d H:i:s')
|
||||||
|
]);
|
||||||
|
|
||||||
|
event()->dispatch(new TalkEvent(TalkEventConstant::EVENT_TALK, [
|
||||||
|
'sender_id' => $result->user_id,
|
||||||
|
'receiver_id' => $result->receiver_id,
|
||||||
|
'talk_type' => $result->talk_type,
|
||||||
|
'record_id' => $result->id
|
||||||
|
]));
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建代码块消息
|
* 创建代码块消息
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in New Issue